Index: lib/Frontend/InitHeaderSearch.cpp
===================================================================
--- lib/Frontend/InitHeaderSearch.cpp
+++ lib/Frontend/InitHeaderSearch.cpp
@@ -526,7 +526,7 @@
     if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
       // Find the dir that this is the same of.
       unsigned FirstDir;
-      for (FirstDir = 0; ; ++FirstDir) {
+      for (FirstDir = First;; ++FirstDir) {
         assert(FirstDir != i && "Didn't find dupe?");
 
         const DirectoryLookup &SearchEntry = SearchList[FirstDir];
Index: test/Frontend/include-duplicate-removal.c
===================================================================
--- /dev/null
+++ test/Frontend/include-duplicate-removal.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -v -I%S/Inputs -iquote %S/Inputs/SystemHeaderPrefix -isystem %S/Inputs/SystemHeaderPrefix -isystem %S/Inputs/SystemHeaderPrefix %s 2>&1 | FileCheck %s
+
+#include <test.h>
+
+// CHECK: ignoring duplicate directory
+// CHECK-SAME: Inputs/SystemHeaderPrefix"{{$}}
+
+// CHECK:      #include "..."
+// CHECK-NEXT: {{.*}}Inputs/SystemHeaderPrefix{{$}}
+// CHECK-NEXT: #include <...>
+// CHECK-NEXT: {{.*}}Inputs{{$}}
+// CHECK-NEXT: {{.*}}Inputs/SystemHeaderPrefix{{$}}