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

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ ft_pathbuilder(char *, char *);
scalar_t__ ft_strcmp(char const *, char const *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
char *validate_file(int /*<<< orphan*/, int /*<<< orphan*/);
char *find_bin(char *path, char *bin) {
  DIR *dirp;
  struct dirent *dirent;
  if (!(dirp = opendir(path)))
    return (NULL);
  while ((dirent = readdir(dirp)) != NULL) {
    if (ft_strcmp((const char *)dirent->d_name, (const char *)bin) == 0) {
      closedir(dirp);
      return (validate_file(ft_pathbuilder(path, bin), 0));
    }
  }
  closedir(dirp);
  return (NULL);
}


x86 assembly (clang, -O0):
<find_bin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1194 <find_bin+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1208 <find_bin+0xa8>
jmp    1199 <find_bin+0x39>
mov    -0x20(%rbp),%rdi
call   1060 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11f7 <find_bin+0x97>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <ft_strcmp@plt>
cmp    $0x0,%rax
jne    11f2 <find_bin+0x92>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <ft_pathbuilder@plt>
mov    %eax,%edi
xor    %esi,%esi
call   1070 <validate_file@plt>
mov    %rax,-0x8(%rbp)
jmp    1208 <find_bin+0xa8>
jmp    1199 <find_bin+0x39>
mov    -0x20(%rbp),%rdi
call   1050 <closedir@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_bin>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
call   1040 <opendir@plt>
test   %rax,%rax
je     11c6 <find_bin+0x66>
mov    %rax,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <readdir@plt>
test   %rax,%rax
je     11be <find_bin+0x5e>
mov    (%rax),%rdi
mov    %r15,%rsi
call   1080 <ft_strcmp@plt>
test   %rax,%rax
jne    1180 <find_bin+0x20>
mov    %rbx,%rdi
call   1050 <closedir@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <ft_pathbuilder@plt>
mov    %eax,%edi
xor    %esi,%esi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <validate_file@plt>
mov    %rbx,%rdi
call   1050 <closedir@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dirent = type { i64 }
define i8* @find_bin(i8* %0, i8* %1) {
%3 = alloca i8*
%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 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %2
store i8* null, i8** %3
br label %36
12: ; preds = %2
br label %13
13: ; preds = %32, %12
%14 = load i32*, i32** %6
%15 = call %struct.dirent* @readdir(i32* %14)
store %struct.dirent* %15, %struct.dirent** %7
%16 = icmp ne %struct.dirent* %15, null
br i1 %16, label %17, label %33
17: ; preds = %13
%18 = load %struct.dirent*, %struct.dirent** %7
%19 = getelementptr inbounds %struct.dirent, %struct.dirent* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to i8*
%22 = load i8*, i8** %5
%23 = call i64 @ft_strcmp(i8* %21, i8* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %32
25: ; preds = %17
%26 = load i32*, i32** %6
%27 = call i32 @closedir(i32* %26)
%28 = load i8*, i8** %4
%29 = load i8*, i8** %5
%30 = call i32 @ft_pathbuilder(i8* %28, i8* %29)
%31 = call i8* @validate_file(i32 %30, i32 0)
store i8* %31, i8** %3
br label %36
32: ; preds = %17
br label %13
33: ; preds = %13
%34 = load i32*, i32** %6
%35 = call i32 @closedir(i32* %34)
store i8* null, i8** %3
br label %36
36: ; preds = %33, %25, %11
%37 = load i8*, i8** %3
ret i8* %37
}

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