#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 */
typedef int /*<<< orphan*/ dir;

/* Variables and functions */
int PATH_MAX;
char *find_top_from(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ get_my_path(char *, int);
char *getcwd(char *, int);
char *getenv(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ top_works(char *);
char *find_top(char path_buf[PATH_MAX]) {
  char *top = getenv("ANDROID_BUILD_TOP");
  if (top != NULL && top[0] != '\0') {
    if (!top_works(top)) {
      fprintf(stderr, "adb: bad ANDROID_BUILD_TOP value \"%s\"\n", top);
      return NULL;
    }
  } else {
    top = getenv("TOP");
    if (top != NULL && top[0] != '\0') {
      if (!top_works(top)) {
        fprintf(stderr, "adb: bad TOP value \"%s\"\n", top);
        return NULL;
      }
    } else {
      top = NULL;
    }
  }
  if (top != NULL) {
    strcpy(path_buf, top);
    return path_buf;
  }
  char dir[PATH_MAX];
  top = find_top_from(getcwd(dir, sizeof(dir)), path_buf);
  if (top == NULL) {
    get_my_path(dir, PATH_MAX);
    top = find_top_from(dir, path_buf);
  }
  return top;
}


x86 assembly (clang, -O0):
<find_top>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe7d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e4 <find_top+0x74>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11e4 <find_top+0x74>
mov    -0x18(%rbp),%rdi
call   1060 <top_works@plt>
cmp    $0x0,%eax
jne    11df <find_top+0x6f>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe45(%rip),%rsi
call   1080 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12fe <find_top+0x18e>
jmp    1259 <find_top+0xe9>
lea    0xe23(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     124c <find_top+0xdc>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     124c <find_top+0xdc>
mov    -0x18(%rbp),%rdi
call   1060 <top_works@plt>
cmp    $0x0,%eax
jne    1247 <find_top+0xd7>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe04(%rip),%rsi
call   1080 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12fe <find_top+0x18e>
jmp    1254 <find_top+0xe4>
movq   $0x0,-0x18(%rbp)
jmp    1259 <find_top+0xe9>
cmpq   $0x0,-0x18(%rbp)
je     127e <find_top+0x10e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12fe <find_top+0x18e>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    %eax,%esi
call   1050 <getcwd@plt>
mov    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1070 <find_top_from@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12ef <find_top+0x17f>
mov    -0x30(%rbp),%rdi
mov    0x2d19(%rip),%rax
mov    (%rax),%esi
call   1090 <get_my_path@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <find_top_from@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_top>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
lea    0xe7b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11b8 <find_top+0x48>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     11b8 <find_top+0x48>
mov    %rbx,%rdi
call   1060 <top_works@plt>
test   %eax,%eax
jne    11dd <find_top+0x6d>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
jmp    1253 <find_top+0xe3>
lea    0xe4f(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ea <find_top+0x7a>
mov    %rax,%rbx
cmpb   $0x0,(%rax)
je     11ea <find_top+0x7a>
mov    %rbx,%rdi
call   1060 <top_works@plt>
test   %eax,%eax
je     1243 <find_top+0xd3>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
jmp    125e <find_top+0xee>
mov    %rsp,%r15
mov    0x2dfc(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1050 <getcwd@plt>
mov    %rax,%rdi
mov    %r14,%rsi
call   1070 <find_top_from@plt>
test   %rax,%rax
jne    123b <find_top+0xcb>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1090 <get_my_path@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <find_top_from@plt>
mov    %r15,%rsp
mov    %rax,%r14
jmp    125e <find_top+0xee>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [18 x i8] c"ANDROID_BUILD_TOP\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"adb: bad ANDROID_BUILD_TOP value \22%s\22\0A\00"
@.str.2 = [4 x i8] c"TOP\00"
@.str.3 = [25 x i8] c"adb: bad TOP value \22%s\22\0A\00"
define i8* @find_top(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %3
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = call i8* @getenv(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %27
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = call i32 @top_works(i8* %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %4
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %24)
store i8* null, i8** %2
br label %75
26: ; preds = %18
br label %48
27: ; preds = %12, %1
%28 = call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i8* %28, i8** %4
%29 = load i8*, i8** %4
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %46
31: ; preds = %27
%32 = load i8*, i8** %4
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %31
%38 = load i8*, i8** %4
%39 = call i32 @top_works(i8* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %4
%44 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %43)
store i8* null, i8** %2
br label %75
45: ; preds = %37
br label %47
46: ; preds = %31, %27
store i8* null, i8** %4
br label %47
47: ; preds = %46, %45
br label %48
48: ; preds = %47, %26
%49 = load i8*, i8** %4
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i8*, i8** %3
%53 = load i8*, i8** %4
%54 = call i32 @strcpy(i8* %52, i8* %53)
%55 = load i8*, i8** %3
store i8* %55, i8** %2
br label %75
56: ; preds = %48
%57 = load i32, i32* @PATH_MAX
%58 = zext i32 %57 to i64
%59 = call i8* @llvm.stacksave()
store i8* %59, i8** %5
%60 = alloca i8, i64 %58
store i64 %58, i64* %6
%61 = trunc i64 %58 to i32
%62 = call i8* @getcwd(i8* %60, i32 %61)
%63 = load i8*, i8** %3
%64 = call i8* @find_top_from(i8* %62, i8* %63)
store i8* %64, i8** %4
%65 = load i8*, i8** %4
%66 = icmp eq i8* %65, null
br i1 %66, label %67, label %72
67: ; preds = %56
%68 = load i32, i32* @PATH_MAX
%69 = call i32 @get_my_path(i8* %60, i32 %68)
%70 = load i8*, i8** %3
%71 = call i8* @find_top_from(i8* %60, i8* %70)
store i8* %71, i8** %4
br label %72
72: ; preds = %67, %56
%73 = load i8*, i8** %4
store i8* %73, i8** %2
%74 = load i8*, i8** %5
br label %75
75: ; preds = %72, %51, %41, %22
%76 = load i8*, i8** %2
ret i8* %76
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [18 x i8] c"ANDROID_BUILD_TOP\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"adb: bad ANDROID_BUILD_TOP value \22%s\22\0A\00"
@.str.2 = [4 x i8] c"TOP\00"
@.str.3 = [25 x i8] c"adb: bad TOP value \22%s\22\0A\00"
define i8* @find_top(i8* %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %13, label %4
4: ; preds = %1
%5 = load i8, i8* %2
%6 = icmp eq i8 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %4
%8 = tail call i32 @top_works(i8* %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %25
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %42
13: ; preds = %4, %1
%14 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %28, label %16
16: ; preds = %13
%17 = load i8, i8* %14
%18 = icmp eq i8 %17, 0
br i1 %18, label %28, label %19
19: ; preds = %16
%20 = tail call i32 @top_works(i8* %14)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = tail call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %42
25: ; preds = %7, %19
%26 = phi i8* [ %2, %7 ], [ %14, %19 ]
%27 = tail call i32 @strcpy(i8* %0, i8* %26)
br label %42
28: ; preds = %16, %13
%29 = load i32, i32* @PATH_MAX
%30 = zext i32 %29 to i64
%31 = tail call i8* @llvm.stacksave()
%32 = alloca i8, i64 %30
%33 = call i8* @getcwd(i8* %32, i32 %29)
%34 = call i8* @find_top_from(i8* %33, i8* %0)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %40
36: ; preds = %28
%37 = load i32, i32* @PATH_MAX
%38 = call i32 @get_my_path(i8* %32, i32 %37)
%39 = call i8* @find_top_from(i8* %32, i8* %0)
br label %40
40: ; preds = %36, %28
%41 = phi i8* [ %39, %36 ], [ %34, %28 ]
br label %42
42: ; preds = %40, %25, %22, %10
%43 = phi i8* [ %0, %25 ], [ %41, %40 ], [ null, %10 ], [ null, %22 ]
ret i8* %43
}