#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*/ asprintf(char **, char *, char *, char *);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ dirname(char *);
int /*<<< orphan*/ exit(int);
scalar_t__ file_exists(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *getenv(char *);
int /*<<< orphan*/ stderr;
char *strdup(char const *);
int strlen(char *);
char *find_chroot_base(const char *exepath) {
  char *ret, *dir = strdup(exepath);
  while (1) {
    char *tfname = NULL;
    dirname(dir);
    asprintf(&tfname, "%s/%s", dir, ".fakechroot-base");
    assert(tfname != NULL);
    if (file_exists(tfname)) {
      free(tfname);
      break;
    }
    if (strlen(dir) == 1) {
      fprintf(stderr, "wrap: Cannot find fakechroot base -- aborting\n");
      exit(1);
    }
    free(tfname);
  }
  if (getenv("WRAP_DEBUG") != NULL)
    fprintf(stderr, "wrap: found base: %s\n", dir);
  ret = strdup(dir);
  free(dir);
  return ret;
}


x86 assembly (clang, -O0):
<find_chroot_base>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <dirname@plt>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    0xe27(%rip),%rsi
lea    0xe26(%rip),%rcx
call   10a0 <asprintf@plt>
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x20(%rbp),%rdi
call   1050 <file_exists@plt>
cmp    $0x0,%rax
je     1218 <find_chroot_base+0x78>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
jmp    1259 <find_chroot_base+0xb9>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x1,%eax
jne    124b <find_chroot_base+0xab>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
jmp    11b9 <find_chroot_base+0x19>
lea    0xde6(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     128a <find_chroot_base+0xea>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_chroot_base>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
call   10c0 <strdup@plt>
mov    %rax,%rbx
lea    0xe49(%rip),%r14
lea    0xe48(%rip),%r15
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movq   $0x0,(%rsp)
mov    %rbx,%rdi
call   1070 <dirname@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   10a0 <asprintf@plt>
xor    %edi,%edi
cmpq   $0x0,(%rsp)
setne  %dil
call   1080 <assert@plt>
mov    (%rsp),%rdi
call   1050 <file_exists@plt>
test   %rax,%rax
jne    1227 <find_chroot_base+0x87>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cmp    $0x1,%eax
je     127d <find_chroot_base+0xdd>
mov    (%rsp),%rdi
call   1040 <free@plt>
jmp    11d0 <find_chroot_base+0x30>
mov    (%rsp),%rdi
call   1040 <free@plt>
lea    0xe0f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     125b <find_chroot_base+0xbb>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   10c0 <strdup@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [17 x i8] c".fakechroot-base\00"
@stderr = global i32 0
@.str.2 = [47 x i8] c"wrap: Cannot find fakechroot base -- aborting\0A\00"
@.str.3 = [11 x i8] c"WRAP_DEBUG\00"
@.str.4 = [22 x i8] c"wrap: found base: %s\0A\00"
define i8* @find_chroot_base(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8* @strdup(i8* %6)
store i8* %7, i8** %4
br label %8
8: ; preds = %1, %31
store i8* null, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @dirname(i8* %9)
%11 = load i8*, i8** %4
%12 = call i32 @asprintf(i8** %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %11, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load i8*, i8** %5
%18 = call i64 @file_exists(i8* %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %8
%21 = load i8*, i8** %5
%22 = call i32 @free(i8* %21)
br label %34
23: ; preds = %8
%24 = load i8*, i8** %4
%25 = call i32 @strlen(i8* %24)
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %23
%32 = load i8*, i8** %5
%33 = call i32 @free(i8* %32)
br label %8
34: ; preds = %20
%35 = call i32* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %4
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %37, %34
%42 = load i8*, i8** %4
%43 = call i8* @strdup(i8* %42)
store i8* %43, i8** %3
%44 = load i8*, i8** %4
%45 = call i32 @free(i8* %44)
%46 = load i8*, i8** %3
ret i8* %46
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [17 x i8] c".fakechroot-base\00"
@stderr = global i32 0
@.str.2 = [47 x i8] c"wrap: Cannot find fakechroot base -- aborting\0A\00"
@.str.3 = [11 x i8] c"WRAP_DEBUG\00"
@.str.4 = [22 x i8] c"wrap: found base: %s\0A\00"
define noalias i8* @find_chroot_base(i8* nocapture %0) {
%2 = alloca i8*
%3 = tail call i8* @strdup(i8* %0)
%4 = bitcast i8** %2 to i8*
br label %5
5: ; preds = %22, %1
store i8* null, i8** %2
%6 = call i32 @dirname(i8* %3)
%7 = call i32 @asprintf(i8** %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %3, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%8 = load i8*, i8** %2
%9 = icmp ne i8* %8, null
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i8*, i8** %2
%13 = call i64 @file_exists(i8* %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %5
%16 = call i32 @strlen(i8* %3)
%17 = icmp eq i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %15
%23 = load i8*, i8** %2
%24 = call i32 @free(i8* %23)
br label %5
25: ; preds = %5
%26 = load i8*, i8** %2
%27 = call i32 @free(i8* %26)
%28 = call i32* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%29 = icmp eq i32* %28, null
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %3)
br label %33
33: ; preds = %30, %25
%34 = call i8* @strdup(i8* %3)
%35 = call i32 @free(i8* %3)
ret i8* %34
}