#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 dirent {
  int /*<<< orphan*/ d_name;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int contains_file(char *dir, char *name) {
  DIR *dirp = opendir(dir);
  struct dirent *dp = readdir(dirp);
  while (dp != NULL) {
    if (strcmp(dp->d_name, name) == 0)
      return 1;
    dp = readdir(dirp);
  }
  closedir(dirp);
  return 0;
}


x86 assembly (clang, -O0):
<contains_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ac <contains_file+0x6c>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    119a <contains_file+0x5a>
movl   $0x1,-0x4(%rbp)
jmp    11bc <contains_file+0x7c>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
jmp    116a <contains_file+0x2a>
mov    -0x20(%rbp),%rdi
call   1040 <closedir@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<contains_file>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
call   1030 <opendir@plt>
mov    %rax,%rbx
mov    %rax,%rdi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1060 <readdir@plt>
test   %rax,%rax
je     117e <contains_file+0x3e>
mov    (%rax),%edi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     118a <contains_file+0x4a>
mov    %rbx,%rdi
jmp    1160 <contains_file+0x20>
mov    %rbx,%rdi
call   1040 <closedir@plt>
xor    %eax,%eax
jmp    118f <contains_file+0x4f>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
define i32 @contains_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca %struct.dirent*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = call i32* @opendir(i8* %8)
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = call %struct.dirent* @readdir(i32* %10)
store %struct.dirent* %11, %struct.dirent** %7
br label %12
12: ; preds = %23, %2
%13 = load %struct.dirent*, %struct.dirent** %7
%14 = icmp ne %struct.dirent* %13, null
br i1 %14, label %15, label %26
15: ; preds = %12
%16 = load %struct.dirent*, %struct.dirent** %7
%17 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i8*, i8** %5
%20 = call i64 @strcmp(i32 %18, i8* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 1, i32* %3
br label %29
23: ; preds = %15
%24 = load i32*, i32** %6
%25 = call %struct.dirent* @readdir(i32* %24)
store %struct.dirent* %25, %struct.dirent** %7
br label %12
26: ; preds = %12
%27 = load i32*, i32** %6
%28 = call i32 @closedir(i32* %27)
store i32 0, i32* %3
br label %29
29: ; preds = %26, %22
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
define i32 @contains_file(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @opendir(i8* %0)
%4 = tail call %struct.dirent* @readdir(i32* %3)
%5 = icmp eq %struct.dirent* %4, null
br i1 %5, label %15, label %6
6: ; preds = %2, %12
%7 = phi %struct.dirent* [ %13, %12 ], [ %4, %2 ]
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %7, i64 0, i32 0
%9 = load i32, i32* %8
%10 = tail call i64 @strcmp(i32 %9, i8* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = tail call %struct.dirent* @readdir(i32* %3)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %15, label %6
15: ; preds = %12, %2
%16 = tail call i32 @closedir(i32* %3)
br label %17
17: ; preds = %6, %15
%18 = phi i32 [ 0, %15 ], [ 1, %6 ]
ret i32 %18
}