#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ R_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *getcwd(char *, int);
int open(char *, int /*<<< orphan*/);
int chdir_to_db(const char *fn) {
  char buf[2];
  int startdir;
  if (fn == NULL)
    return 1;
  startdir = open(".", O_RDONLY);
  int fchdir(int);
  while (1) {
    if (access(fn, R_OK) == 0)
      break;
    if (getcwd(buf, 2) != NULL) {
      fchdir(startdir);
      close(startdir);
      return 1;
    }
    chdir("..");
  }
  close(startdir);
  return 0;
}


x86 assembly (clang, -O0):
<chdir_to_db>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1183 <chdir_to_db+0x23>
movl   $0x1,-0x4(%rbp)
jmp    1210 <chdir_to_db+0xb0>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
cmp    $0x0,%rax
jne    11bc <chdir_to_db+0x5c>
jmp    1201 <chdir_to_db+0xa1>
lea    -0x12(%rbp),%rdi
mov    $0x2,%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
je     11f0 <chdir_to_db+0x90>
mov    -0x18(%rbp),%edi
call   1060 <fchdir@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1210 <chdir_to_db+0xb0>
lea    0xe09(%rip),%rdi
call   1030 <chdir@plt>
jmp    119b <chdir_to_db+0x3b>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chdir_to_db>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11ed <chdir_to_db+0x8d>
mov    %rdi,%rbx
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
lea    0xe7e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%r14d
mov    0x2e46(%rip),%r13
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     1203 <chdir_to_db+0xa3>
lea    0x6(%rsp),%r12
lea    0xe4e(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x2,%esi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11f5 <chdir_to_db+0x95>
mov    %rbp,%rdi
call   1030 <chdir@plt>
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1080 <access@plt>
test   %rax,%rax
jne    11c0 <chdir_to_db+0x60>
jmp    1203 <chdir_to_db+0xa3>
mov    $0x1,%r15d
jmp    120b <chdir_to_db+0xab>
mov    %r14d,%edi
call   1060 <fchdir@plt>
mov    $0x1,%r15d
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@R_OK = global i32 0
@.str.1 = [3 x i8] c"..\00"
define i32 @chdir_to_db(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [2 x i8]
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 1, i32* %2
br label %32
9: ; preds = %1
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %5
br label %12
12: ; preds = %9, %27
%13 = load i8*, i8** %3
%14 = load i32, i32* @R_OK
%15 = call i64 @access(i8* %13, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %12
br label %29
18: ; preds = %12
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%20 = call i32* @getcwd(i8* %19, i32 2)
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* %5
%24 = call i32 @fchdir(i32 %23)
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
store i32 1, i32* %2
br label %32
27: ; preds = %18
%28 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %12
29: ; preds = %17
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
store i32 0, i32* %2
br label %32
32: ; preds = %29, %22, %8
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@R_OK = global i32 0
@.str.1 = [3 x i8] c"..\00"
define i32 @chdir_to_db(i8* %0) {
%2 = alloca [2 x i8]
%3 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %24, label %5
5: ; preds = %1
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @R_OK
%9 = call i64 @access(i8* %0, i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %21, label %11
11: ; preds = %5, %16
%12 = call i32* @getcwd(i8* %3, i32 2)
%13 = icmp eq i32* %12, null
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @fchdir(i32 %7)
br label %21
16: ; preds = %11
%17 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @R_OK
%19 = call i64 @access(i8* %0, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %11
21: ; preds = %16, %5, %14
%22 = phi i32 [ 1, %14 ], [ 0, %5 ], [ 0, %16 ]
%23 = call i32 @close(i32 %7)
br label %24
24: ; preds = %21, %1
%25 = phi i32 [ 1, %1 ], [ %22, %21 ]
ret i32 %25
}