#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 stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ asprintf(char **, char *, char *);
int chdir(char *);
char *getwd(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int main(int argc, const char *argv[]) {
  int ret;
  char *dir_name = NULL;
  struct stat info;
  for (;;) {
    dir_name = getwd(NULL);
    if (dir_name == NULL) {
      perror("getwd");
      return -1;
    }
    if (strcmp(dir_name, "/") == 0) {
      return 1;
    }
    asprintf(&dir_name, "%s/.hg", dir_name);
    ret = stat(dir_name, &info);
    if (ret == 0) {
      if (S_ISDIR(info.st_mode)) {
        return 0;
      }
    }
    ret = chdir("..");
    if (ret < 0) {
      perror("chdir");
      return -1;
    }
  }
  return 1;
}


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,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <getwd@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11be <main+0x4e>
lea    0xe53(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <main+0xfb>
mov    -0x20(%rbp),%rdi
lea    0xe3d(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11e4 <main+0x74>
movl   $0x1,-0x4(%rbp)
jmp    126b <main+0xfb>
mov    -0x20(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1080 <asprintf@plt>
mov    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1235 <main+0xc5>
mov    -0x28(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1230 <main+0xc0>
movl   $0x0,-0x4(%rbp)
jmp    126b <main+0xfb>
jmp    1235 <main+0xc5>
lea    0xdd3(%rip),%rdi
call   1030 <chdir@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1266 <main+0xf6>
lea    0xdbd(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126b <main+0xfb>
jmp    118e <main+0x1e>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x8(%rsp)
lea    0xe72(%rip),%r14
lea    0xe71(%rip),%r13
lea    0xe6c(%rip),%r15
lea    0x8(%rsp),%r12
lea    0x10(%rsp),%rbx
lea    0xe62(%rip),%rbp
jmp    11bc <main+0x4c>
nop
mov    %rbp,%rdi
call   1030 <chdir@plt>
test   %eax,%eax
js     1224 <main+0xb4>
xor    %edi,%edi
call   1070 <getwd@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     122b <main+0xbb>
mov    %rax,%rdi
mov    %r13,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1210 <main+0xa0>
mov    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <asprintf@plt>
mov    0x8(%rsp),%rdi
mov    %rbx,%rsi
call   1060 <stat@plt>
test   %eax,%eax
jne    11b0 <main+0x40>
mov    0x10(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
je     11b0 <main+0x40>
xor    %eax,%eax
jmp    1215 <main+0xa5>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde7(%rip),%r14
mov    %r14,%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    1215 <main+0xa5>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"getwd\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [7 x i8] c"%s/.hg\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [6 x i8] c"chdir\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
br label %9
9: ; preds = %40, %2
%10 = call i8* @getwd(i32* null)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
15: ; preds = %9
%16 = load i8*, i8** %7
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 1, i32* %3
br label %41
20: ; preds = %15
%21 = load i8*, i8** %7
%22 = call i32 @asprintf(i8** %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i8*, i8** %7
%24 = call i32 @stat(i8* %23, %struct.stat* %8)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %20
%28 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i64 @S_ISDIR(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
store i32 0, i32* %3
br label %41
33: ; preds = %27
br label %34
34: ; preds = %33, %20
%35 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
40: ; preds = %34
br label %9
41: ; preds = %38, %32, %19, %13
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"getwd\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [7 x i8] c"%s/.hg\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [6 x i8] c"chdir\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast %struct.stat* %4 to i8*
%7 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %8
8: ; preds = %24, %2
%9 = call i8* @getwd(i32* null)
store i8* %9, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %27, label %11
11: ; preds = %8
%12 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %30, label %14
14: ; preds = %11
%15 = load i8*, i8** %3
%16 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %3
%18 = call i32 @stat(i8* %17, %struct.stat* %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* %7
%22 = call i64 @S_ISDIR(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %20, %14
%25 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %8
27: ; preds = %24, %8
%28 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %8 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), %24 ]
%29 = call i32 @perror(i8* %28)
br label %30
30: ; preds = %20, %11, %27
%31 = phi i32 [ -1, %27 ], [ 0, %20 ], [ 1, %11 ]
ret i32 %31
}