#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 PATH_MAX;
int /*<<< orphan*/ assert(int);
scalar_t__ chdir(char const *);
int /*<<< orphan*/ free(char *);
char *getcwd(char *, int);
char *git__pipe_read(char *);
scalar_t__ strcmp(char *, char *);
char *git_current_branch(const char *path) {
  if (!path)
    return NULL;
  char pdir[PATH_MAX];
  if (getcwd(pdir, PATH_MAX) != pdir)
    return NULL;
  if (chdir(path) != 0)
    return NULL;
  char *branch =
      git__pipe_read("git rev-parse --symbolic-full-name --abbrev-ref HEAD");
  if (branch && (strcmp(branch, "HEAD") == 0)) {
    free(branch);
    branch = git__pipe_read("git rev-parse HEAD");
  }
  assert(chdir(pdir) == 0);
  return branch;
}


x86 assembly (clang, -O0):
<git_current_branch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1184 <git_current_branch+0x24>
movq   $0x0,-0x8(%rbp)
jmp    1284 <git_current_branch+0x124>
mov    0x2e65(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1060 <getcwd@plt>
mov    -0x38(%rbp),%rcx
cmp    %rcx,%rax
je     11df <git_current_branch+0x7f>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    127d <git_current_branch+0x11d>
mov    -0x10(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
je     1206 <git_current_branch+0xa6>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
jmp    127d <git_current_branch+0x11d>
lea    0xdf3(%rip),%rdi
call   1040 <git__pipe_read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1254 <git_current_branch+0xf4>
mov    -0x30(%rbp),%rdi
lea    0xe17(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1254 <git_current_branch+0xf4>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
lea    0xdea(%rip),%rdi
call   1040 <git__pipe_read@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <chdir@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<git_current_branch>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     11b3 <git_current_branch+0x53>
mov    %rdi,%r14
mov    %rsp,%r15
mov    0x2e74(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rbx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%rbx
mov    %rbx,%rsp
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <getcwd@plt>
cmp    %rbx,%rax
jne    11ab <git_current_branch+0x4b>
mov    %r14,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
je     11c4 <git_current_branch+0x64>
xor    %r14d,%r14d
mov    %r15,%rsp
jmp    11b6 <git_current_branch+0x56>
xor    %r14d,%r14d
mov    %r14,%rax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe35(%rip),%rdi
call   1040 <git__pipe_read@plt>
test   %rax,%rax
je     1205 <git_current_branch+0xa5>
mov    %rax,%r14
lea    0xe64(%rip),%rsi
mov    %rax,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1208 <git_current_branch+0xa8>
mov    %r14,%rdi
call   1030 <free@plt>
lea    0xe3a(%rip),%rdi
call   1040 <git__pipe_read@plt>
mov    %rax,%r14
jmp    1208 <git_current_branch+0xa8>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1050 <chdir@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1080 <assert@plt>
jmp    11ae <git_current_branch+0x4e>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [53 x i8] c"git rev-parse --symbolic-full-name --abbrev-ref HEAD\00"
@.str.1 = [5 x i8] c"HEAD\00"
@.str.2 = [19 x i8] c"git rev-parse HEAD\00"
define i8* @git_current_branch(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i8* null, i8** %2
br label %45
11: ; preds = %1
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %4
%15 = alloca i8, i64 %13
store i64 %13, i64* %5
%16 = load i32, i32* @PATH_MAX
%17 = call i8* @getcwd(i8* %15, i32 %16)
%18 = icmp ne i8* %17, %15
br i1 %18, label %19, label %20
19: ; preds = %11
store i8* null, i8** %2
store i32 1, i32* %6
br label %43
20: ; preds = %11
%21 = load i8*, i8** %3
%22 = call i64 @chdir(i8* %21)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %20
store i8* null, i8** %2
store i32 1, i32* %6
br label %43
25: ; preds = %20
%26 = call i8* @git__pipe_read(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = load i8*, i8** %7
%31 = call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i8*, i8** %7
%35 = call i32 @free(i8* %34)
%36 = call i8* @git__pipe_read(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i8* %36, i8** %7
br label %37
37: ; preds = %33, %29, %25
%38 = call i64 @chdir(i8* %15)
%39 = icmp eq i64 %38, 0
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = load i8*, i8** %7
store i8* %42, i8** %2
store i32 1, i32* %6
br label %43
43: ; preds = %37, %24, %19
%44 = load i8*, i8** %4
br label %45
45: ; preds = %43, %10
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [53 x i8] c"git rev-parse --symbolic-full-name --abbrev-ref HEAD\00"
@.str.1 = [5 x i8] c"HEAD\00"
@.str.2 = [19 x i8] c"git rev-parse HEAD\00"
define i8* @git_current_branch(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %31, label %3
3: ; preds = %1
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = load i32, i32* @PATH_MAX
%9 = call i8* @getcwd(i8* %7, i32 %8)
%10 = icmp eq i8* %9, %7
br i1 %10, label %11, label %29
11: ; preds = %3
%12 = call i64 @chdir(i8* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %11
%15 = call i8* @git__pipe_read(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i8* %15, null
br i1 %16, label %23, label %17
17: ; preds = %14
%18 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @free(i8* %15)
%22 = call i8* @git__pipe_read(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %20, %17, %14
%24 = phi i8* [ %22, %20 ], [ %15, %17 ], [ null, %14 ]
%25 = call i64 @chdir(i8* %7)
%26 = icmp eq i64 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
br label %29
29: ; preds = %11, %3, %23
%30 = phi i8* [ %24, %23 ], [ null, %3 ], [ null, %11 ]
br label %31
31: ; preds = %1, %29
%32 = phi i8* [ %30, %29 ], [ null, %1 ]
ret i8* %32
}