#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*/ assert(int);
scalar_t__ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  DIR *dir = NULL;
  struct dirent *entry;
  int found = 0;
  if (2 != argc) {
    printf("Usage: sel_ldr test_dir.nexe directory/containing/test_dir.nexe\n");
    return 1;
  }
  dir = opendir(argv[1]);
  assert(NULL != dir);
  for (; NULL != (entry = readdir(dir));) {
    if (0 == strcmp("test_dir.nexe", entry->d_name)) {
      found = 1;
    }
  }
  assert(1 == found);
  assert(0 == closedir(dir));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11ab <main+0x4b>
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1250 <main+0xf0>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
je     1216 <main+0xb6>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
call   1060 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    1211 <main+0xb1>
movl   $0x1,-0x24(%rbp)
jmp    11cf <main+0x6f>
mov    $0x1,%eax
cmp    -0x24(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1050 <closedir@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x2,%edi
jne    11f4 <main+0x94>
mov    0x8(%rsi),%rdi
call   1040 <opendir@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1080 <readdir@plt>
xor    %r14d,%r14d
mov    $0x0,%edi
test   %rax,%rax
je     11d7 <main+0x77>
xor    %ebp,%ebp
lea    0xe89(%rip),%r15
mov    $0x1,%r12d
nop
mov    (%rax),%esi
mov    %r15,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
cmove  %r12d,%ebp
mov    %rbx,%rdi
call   1080 <readdir@plt>
test   %rax,%rax
jne    11b0 <main+0x50>
xor    %edi,%edi
cmp    $0x1,%ebp
sete   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
jmp    1206 <main+0xa6>
lea    0xe05(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [65 x i8] c"Usage: sel_ldr test_dir.nexe directory/containing/test_dir.nexe\0A\00"
@.str.1 = [14 x i8] c"test_dir.nexe\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.dirent*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
store i32 0, i32* %8
%9 = load i32, i32* %4
%10 = icmp ne i32 2, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %44
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp ne i32* null, %18
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
br label %22
22: ; preds = %33, %13
%23 = load i32*, i32** %6
%24 = call %struct.dirent* @readdir(i32* %23)
store %struct.dirent* %24, %struct.dirent** %7
%25 = icmp ne %struct.dirent* null, %24
br i1 %25, label %26, label %34
26: ; preds = %22
%27 = load %struct.dirent*, %struct.dirent** %7
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @strcmp(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = icmp eq i64 0, %30
br i1 %31, label %32, label %33
32: ; preds = %26
store i32 1, i32* %8
br label %33
33: ; preds = %32, %26
br label %22
34: ; preds = %22
%35 = load i32, i32* %8
%36 = icmp eq i32 1, %35
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = load i32*, i32** %6
%40 = call i64 @closedir(i32* %39)
%41 = icmp eq i64 0, %40
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
store i32 0, i32* %3
br label %44
44: ; preds = %34, %11
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str.1 = [14 x i8] c"test_dir.nexe\00"
@str = [64 x i8] c"Usage: sel_ldr test_dir.nexe directory/containing/test_dir.nexe\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str, i64 0, i64 0))
br label %35
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @opendir(i8* %8)
%10 = icmp ne i32* %9, null
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = tail call %struct.dirent* @readdir(i32* %9)
%14 = icmp eq %struct.dirent* %13, null
br i1 %14, label %28, label %15
15: ; preds = %6, %15
%16 = phi %struct.dirent* [ %23, %15 ], [ %13, %6 ]
%17 = phi i32 [ %22, %15 ], [ 0, %6 ]
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %16, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i64 @strcmp(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = icmp eq i64 %20, 0
%22 = select i1 %21, i32 1, i32 %17
%23 = tail call %struct.dirent* @readdir(i32* %9)
%24 = icmp eq %struct.dirent* %23, null
br i1 %24, label %25, label %15
25: ; preds = %15
%26 = icmp eq i32 %22, 1
%27 = zext i1 %26 to i32
br label %28
28: ; preds = %25, %6
%29 = phi i32 [ 0, %6 ], [ %27, %25 ]
%30 = tail call i32 @assert(i32 %29)
%31 = tail call i64 @closedir(i32* %9)
%32 = icmp eq i64 %31, 0
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
br label %35
35: ; preds = %28, %4
%36 = phi i32 [ 1, %4 ], [ 0, %28 ]
ret i32 %36
}