#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*/ F_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
scalar_t__ chdir(char *);
char *getcwd(char *, int);
const char *chdir_to_the_wuffs_root_directory() {
  int n;
  for (n = 0; n < 64; n++) {
    if (access("wuffs-root-directory.txt", F_OK) == 0) {
      return NULL;
    }
    char cwd_buffer[4];
    char *cwd = getcwd(cwd_buffer, 4);
    if ((cwd != NULL) && (cwd[0] == '/') && (cwd[1] == '\x00')) {
      break;
    }
    if (chdir("..")) {
      return "could not chdir(\"..\")";
    }
  }
  return "could not find Wuffs root directory; chdir there before running this "
         "program";
}


x86 assembly (clang, -O0):
<chdir_to_the_wuffs_root_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0xc(%rbp)
cmpl   $0x40,-0xc(%rbp)
jge    11f1 <chdir_to_the_wuffs_root_directory+0xc1>
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xea7(%rip),%rdi
call   1050 <access@plt>
cmp    $0x0,%rax
jne    1175 <chdir_to_the_wuffs_root_directory+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11fc <chdir_to_the_wuffs_root_directory+0xcc>
lea    -0x10(%rbp),%rdi
mov    $0x4,%esi
call   1040 <getcwd@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b8 <chdir_to_the_wuffs_root_directory+0x88>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11b8 <chdir_to_the_wuffs_root_directory+0x88>
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
jne    11b8 <chdir_to_the_wuffs_root_directory+0x88>
jmp    11f1 <chdir_to_the_wuffs_root_directory+0xc1>
lea    0xe5a(%rip),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
je     11de <chdir_to_the_wuffs_root_directory+0xae>
lea    0xe47(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11fc <chdir_to_the_wuffs_root_directory+0xcc>
jmp    11e3 <chdir_to_the_wuffs_root_directory+0xb3>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113f <chdir_to_the_wuffs_root_directory+0xf>
lea    0xe3a(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chdir_to_the_wuffs_root_directory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x40,%ebp
mov    0x2e91(%rip),%r13
lea    0xeb2(%rip),%r12
lea    0x4(%rsp),%r15
lea    0xebf(%rip),%rbx
lea    0xed1(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1050 <access@plt>
test   %rax,%rax
je     11b2 <chdir_to_the_wuffs_root_directory+0x82>
mov    %r15,%rdi
mov    $0x4,%esi
call   1040 <getcwd@plt>
test   %rax,%rax
je     119e <chdir_to_the_wuffs_root_directory+0x6e>
cmpb   $0x2f,(%rax)
jne    119e <chdir_to_the_wuffs_root_directory+0x6e>
cmpb   $0x0,0x1(%rax)
je     11be <chdir_to_the_wuffs_root_directory+0x8e>
mov    %rbx,%rdi
call   1030 <chdir@plt>
test   %rax,%rax
jne    11b7 <chdir_to_the_wuffs_root_directory+0x87>
add    $0xffffffff,%ebp
jne    1170 <chdir_to_the_wuffs_root_directory+0x40>
jmp    11be <chdir_to_the_wuffs_root_directory+0x8e>
xor    %r14d,%r14d
jmp    11be <chdir_to_the_wuffs_root_directory+0x8e>
lea    0xe5e(%rip),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"wuffs-root-directory.txt\00"
@F_OK = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [22 x i8] c"could not chdir(\22..\22)\00"
@.str.3 = [77 x i8] c"could not find Wuffs root directory; chdir there before running this program\00"
define i8* @chdir_to_the_wuffs_root_directory() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca [4 x i8]
%4 = alloca i8*
store i32 0, i32* %2
br label %5
5: ; preds = %36, %0
%6 = load i32, i32* %2
%7 = icmp slt i32 %6, 64
br i1 %7, label %8, label %39
8: ; preds = %5
%9 = load i32, i32* @F_OK
%10 = call i64 @access(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
store i8* null, i8** %1
br label %40
13: ; preds = %8
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%15 = call i8* @getcwd(i8* %14, i32 4)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %31
18: ; preds = %13
%19 = load i8*, i8** %4
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 47
br i1 %23, label %24, label %31
24: ; preds = %18
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
br label %39
31: ; preds = %24, %18, %13
%32 = call i64 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8** %1
br label %40
35: ; preds = %31
br label %36
36: ; preds = %35
%37 = load i32, i32* %2
%38 = add nsw i32 %37, 1
store i32 %38, i32* %2
br label %5
39: ; preds = %30, %5
store i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.3, i64 0, i64 0), i8** %1
br label %40
40: ; preds = %39, %34, %12
%41 = load i8*, i8** %1
ret i8* %41
}

LLVM IR (-O2):
@.str = [25 x i8] c"wuffs-root-directory.txt\00"
@F_OK = global i32 0
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [22 x i8] c"could not chdir(\22..\22)\00"
@.str.3 = [77 x i8] c"could not find Wuffs root directory; chdir there before running this program\00"
define i8* @chdir_to_the_wuffs_root_directory() {
%1 = alloca [4 x i8]
%2 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %22
%4 = phi i32 [ 0, %0 ], [ %23, %22 ]
%5 = load i32, i32* @F_OK
%6 = call i64 @access(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %25, label %8
8: ; preds = %3
%9 = call i8* @getcwd(i8* %2, i32 4)
%10 = icmp eq i8* %9, null
br i1 %10, label %18, label %11
11: ; preds = %8
%12 = load i8, i8* %9
%13 = icmp eq i8 %12, 47
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %9, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %14, %11, %8
%19 = call i64 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %22, label %25
21: ; preds = %14
br label %25
22: ; preds = %18
%23 = add nuw nsw i32 %4, 1
%24 = icmp eq i32 %23, 64
br i1 %24, label %25, label %3
25: ; preds = %22, %3, %18, %21
%26 = phi i8* [ getelementptr inbounds ([77 x i8], [77 x i8]* @.str.3, i64 0, i64 0), %21 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %18 ], [ getelementptr inbounds ([77 x i8], [77 x i8]* @.str.3, i64 0, i64 0), %22 ], [ null, %3 ]
ret i8* %26
}