#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct stat {
  int /*<<< orphan*/ st_mode;
};
struct dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stat(char *, struct stat *);
scalar_t__ strcasecmp(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int search_file(char *lookfile, char *path, char *filepath) {
  DIR *dir;
  struct dirent *ptr;
  int i;
  dir = opendir(path);
  if (dir == NULL) {
    printf(" fail to open %s\n", path);
    exit(1);
  }
  while ((ptr = readdir(dir)) != NULL) {
    char pathname[1024];
    struct stat buf;
    sprintf(pathname, "%s/%s", path, ptr->d_name);
    stat(pathname, &buf);
    if (S_ISREG(buf.st_mode)) {
      if (strcasecmp(lookfile, ptr->d_name) == 0) {
        strcpy(filepath, path);
        strcpy(lookfile, ptr->d_name);
        closedir(dir);
        return 1;
      }
    }
    if (S_ISDIR(buf.st_mode)) {
      if (strcmp(".", ptr->d_name) != 0 && strcmp("..", ptr->d_name) != 0) {
        printf("%s: directory\n", ptr->d_name);
        if (search_file(lookfile, pathname, filepath) == 1) {
          closedir(dir);
          return 1;
        }
      }
    }
  }
  closedir(dir);
  return 0;
}


x86 assembly (clang, -O0):
<search_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1219 <search_file+0x49>
mov    -0x18(%rbp),%rsi
lea    0xdf6(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
jmp    121e <search_file+0x4e>
mov    -0x28(%rbp),%rdi
call   10c0 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     136f <search_file+0x19f>
lea    -0x440(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xdc4(%rip),%rsi
call   10d0 <sprintf@plt>
lea    -0x440(%rbp),%rdi
lea    -0x448(%rbp),%rsi
call   10a0 <stat@plt>
mov    -0x448(%rbp),%edi
call   10b0 <S_ISREG@plt>
cmp    $0x0,%rax
je     12cc <search_file+0xfc>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcasecmp@plt>
cmp    $0x0,%rax
jne    12c7 <search_file+0xf7>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    137f <search_file+0x1af>
jmp    12cc <search_file+0xfc>
mov    -0x448(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     136a <search_file+0x19a>
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
lea    0xd2a(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1365 <search_file+0x195>
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
lea    0xd0c(%rip),%rdi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1365 <search_file+0x195>
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
lea    0xcf2(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x440(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   10f0 <search_file@plt>
cmp    $0x1,%eax
jne    1360 <search_file+0x190>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x1,-0x4(%rbp)
jmp    137f <search_file+0x1af>
jmp    1365 <search_file+0x195>
jmp    136a <search_file+0x19a>
jmp    121e <search_file+0x4e>
mov    -0x28(%rbp),%rdi
call   1080 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdx,0x8(%rsp)
mov    %rsi,%r14
mov    %rdi,%r15
mov    %rsi,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1321 <search_file+0x151>
mov    %rax,%rbx
mov    %rax,%rdi
call   10c0 <readdir@plt>
test   %rax,%rax
je     12dd <search_file+0x10d>
mov    %rax,%r13
lea    0x10(%rsp),%rbp
jmp    1234 <search_file+0x64>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10c0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     12dd <search_file+0x10d>
mov    0x0(%r13),%rcx
mov    %rbp,%rdi
lea    0xdd0(%rip),%rsi
mov    %r14,%rdx
call   10d0 <sprintf@plt>
mov    %rbp,%rdi
mov    %rsp,%rsi
call   10a0 <stat@plt>
mov    (%rsp),%r12d
mov    %r12d,%edi
call   10b0 <S_ISREG@plt>
test   %rax,%rax
je     1277 <search_file+0xa7>
mov    0x0(%r13),%rsi
mov    %r15,%rdi
call   1030 <strcasecmp@plt>
test   %rax,%rax
je     12e9 <search_file+0x119>
mov    %r12d,%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
je     1220 <search_file+0x50>
mov    0x0(%r13),%rsi
lea    0xd8a(%rip),%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <search_file+0x50>
mov    0x0(%r13),%rsi
lea    0xd74(%rip),%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1220 <search_file+0x50>
mov    0x0(%r13),%rsi
lea    0xd5e(%rip),%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
mov    %rbp,%rsi
mov    0x8(%rsp),%rdx
call   10f0 <search_file@plt>
cmp    $0x1,%eax
jne    1220 <search_file+0x50>
jmp    1302 <search_file+0x132>
mov    %rbx,%rdi
call   1080 <closedir@plt>
xor    %eax,%eax
jmp    130f <search_file+0x13f>
mov    0x8(%rsp),%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    0x0(%r13),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1080 <closedir@plt>
mov    $0x1,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd8(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.dirent = type { i8* }
%struct.stat = type { i32 }
@.str = [18 x i8] c" fail to open %s\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [15 x i8] c"%s: directory\0A\00"
define i32 @search_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca %struct.stat
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %6
%14 = call i32* @opendir(i8* %13)
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %3
br label %22
22: ; preds = %90, %21
%23 = load i32*, i32** %8
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %9
%25 = icmp ne %struct.dirent* %24, null
br i1 %25, label %26, label %91
26: ; preds = %22
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%28 = load i8*, i8** %6
%29 = load %struct.dirent*, %struct.dirent** %9
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %28, i8* %31)
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%34 = call i32 @stat(i8* %33, %struct.stat* %12)
%35 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @S_ISREG(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %58
39: ; preds = %26
%40 = load i8*, i8** %5
%41 = load %struct.dirent*, %struct.dirent** %9
%42 = getelementptr inbounds %struct.dirent, %struct.dirent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = call i64 @strcasecmp(i8* %40, i8* %43)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %39
%47 = load i8*, i8** %7
%48 = load i8*, i8** %6
%49 = call i32 @strcpy(i8* %47, i8* %48)
%50 = load i8*, i8** %5
%51 = load %struct.dirent*, %struct.dirent** %9
%52 = getelementptr inbounds %struct.dirent, %struct.dirent* %51, i32 0, i32 0
%53 = load i8*, i8** %52
%54 = call i32 @strcpy(i8* %50, i8* %53)
%55 = load i32*, i32** %8
%56 = call i32 @closedir(i32* %55)
store i32 1, i32* %4
br label %94
57: ; preds = %39
br label %58
58: ; preds = %57, %26
%59 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i64 @S_ISDIR(i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %90
63: ; preds = %58
%64 = load %struct.dirent*, %struct.dirent** %9
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %89
69: ; preds = %63
%70 = load %struct.dirent*, %struct.dirent** %9
%71 = getelementptr inbounds %struct.dirent, %struct.dirent* %70, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %89
75: ; preds = %69
%76 = load %struct.dirent*, %struct.dirent** %9
%77 = getelementptr inbounds %struct.dirent, %struct.dirent* %76, i32 0, i32 0
%78 = load i8*, i8** %77
%79 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %78)
%80 = load i8*, i8** %5
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%82 = load i8*, i8** %7
%83 = call i32 @search_file(i8* %80, i8* %81, i8* %82)
%84 = icmp eq i32 %83, 1
br i1 %84, label %85, label %88
85: ; preds = %75
%86 = load i32*, i32** %8
%87 = call i32 @closedir(i32* %86)
store i32 1, i32* %4
br label %94
88: ; preds = %75
br label %89
89: ; preds = %88, %69, %63
br label %90
90: ; preds = %89, %58
br label %22
91: ; preds = %22
%92 = load i32*, i32** %8
%93 = call i32 @closedir(i32* %92)
store i32 0, i32* %4
br label %94
94: ; preds = %91, %85, %46
%95 = load i32, i32* %4
ret i32 %95
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i8* }
@.str = [18 x i8] c" fail to open %s\0A\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [15 x i8] c"%s: directory\0A\00"
define i32 @search_file(i8* %0, i8* %1, i8* %2) {
%4 = alloca [1024 x i8]
%5 = alloca %struct.stat
%6 = tail call i32* @opendir(i8* %1)
%7 = icmp eq i32* %6, null
br i1 %7, label %15, label %8
8: ; preds = %3
%9 = call %struct.dirent* @readdir(i32* %6)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %57, label %11
11: ; preds = %8
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%13 = bitcast %struct.stat* %5 to i8*
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
br label %18
15: ; preds = %3
%16 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %1)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11, %54
%19 = phi %struct.dirent* [ %9, %11 ], [ %55, %54 ]
%20 = getelementptr inbounds %struct.dirent, %struct.dirent* %19, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %21)
%23 = call i32 @stat(i8* %12, %struct.stat* %5)
%24 = load i32, i32* %14
%25 = call i64 @S_ISREG(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %18
%28 = load i8*, i8** %20
%29 = call i64 @strcasecmp(i8* %0, i8* %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %27
%32 = call i32 @strcpy(i8* %2, i8* %1)
%33 = load i8*, i8** %20
%34 = call i32 @strcpy(i8* %0, i8* %33)
%35 = call i32 @closedir(i32* %6)
br label %59
36: ; preds = %27, %18
%37 = call i64 @S_ISDIR(i32 %24)
%38 = icmp eq i64 %37, 0
br i1 %38, label %54, label %39
39: ; preds = %36
%40 = load i8*, i8** %20
%41 = call i64 @strcmp(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = icmp eq i64 %41, 0
br i1 %42, label %54, label %43
43: ; preds = %39
%44 = load i8*, i8** %20
%45 = call i64 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %54, label %47
47: ; preds = %43
%48 = load i8*, i8** %20
%49 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %48)
%50 = call i32 @search_file(i8* %0, i8* %12, i8* %2)
%51 = icmp eq i32 %50, 1
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = call i32 @closedir(i32* %6)
br label %59
54: ; preds = %36, %47, %43, %39
%55 = call %struct.dirent* @readdir(i32* %6)
%56 = icmp eq %struct.dirent* %55, null
br i1 %56, label %57, label %18
57: ; preds = %54, %8
%58 = call i32 @closedir(i32* %6)
br label %59
59: ; preds = %31, %52, %57
%60 = phi i32 [ 0, %57 ], [ 1, %52 ], [ 1, %31 ]
ret i32 %60
}