#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*/ new_line;
typedef int /*<<< orphan*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fs_cp(char *, char *);
int /*<<< orphan*/ fs_rm(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *, ...);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strtok(char *, char *);
int /*<<< orphan*/ write_string_to_file(char *, char const *);
int checkout_commit(const char *commit_id) {
  if (strcmp(commit_id, "0000000000000000000000000000000000000000") == 0) {
    write_string_to_file(".beargit/.index", "");
  } else {
    FILE *findex = fopen(".beargit/.index", "r");
    char line[128];
    while (fgets(line, sizeof(line), findex)) {
      strtok(line, "\n");
      fs_rm(line);
    }
    fclose(findex);
    char init_loc[150];
    sprintf(init_loc, "%s%s%s", ".beargit/", commit_id, "/.index");
    FILE *last = fopen(init_loc, "r");
    char new_line[128];
    while (fgets(new_line, sizeof(new_line), last)) {
      strtok(new_line, "\n");
      char final_place[200];
      sprintf(final_place, ".beargit/%s/%s", commit_id, new_line);
      fs_cp(final_place, new_line);
    }
    fs_cp(init_loc, ".beargit/.index");
    fclose(last);
  }
  write_string_to_file(".beargit/.prev", commit_id);
  return 0;
}


x86 assembly (clang, -O0):
<checkout_commit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x290,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11d1 <checkout_commit+0x41>
lea    0xe69(%rip),%rdi
lea    0xe75(%rip),%rsi
call   10b0 <write_string_to_file@plt>
jmp    1301 <checkout_commit+0x171>
lea    0xe51(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     122b <checkout_commit+0x9b>
lea    -0x90(%rbp),%rdi
lea    0xe26(%rip),%rsi
call   1080 <strtok@plt>
lea    -0x90(%rbp),%rdi
call   10a0 <fs_rm@plt>
jmp    11e8 <checkout_commit+0x58>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rcx
lea    0xdf7(%rip),%rsi
lea    0xdf7(%rip),%rdx
lea    0xddd(%rip),%r8
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x130(%rbp),%rdi
lea    0xdd0(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x138(%rbp)
lea    -0x1c0(%rbp),%rdi
mov    -0x138(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12e2 <checkout_commit+0x152>
lea    -0x1c0(%rbp),%rdi
lea    0xd96(%rip),%rsi
call   1080 <strtok@plt>
lea    -0x290(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    -0x1c0(%rbp),%rcx
lea    0xd8b(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x290(%rbp),%rdi
lea    -0x1c0(%rbp),%rsi
call   1060 <fs_cp@plt>
jmp    1275 <checkout_commit+0xe5>
lea    -0x130(%rbp),%rdi
lea    0xd39(%rip),%rsi
call   1060 <fs_cp@plt>
mov    -0x138(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%rsi
lea    0xd51(%rip),%rdi
call   10b0 <write_string_to_file@plt>
xor    %eax,%eax
add    $0x290,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkout_commit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x268,%rsp
mov    %rdi,%r14
lea    0xe55(%rip),%rsi
call   1050 <strcmp@plt>
lea    0xe72(%rip),%rdi
test   %rax,%rax
je     12fc <checkout_commit+0x16c>
lea    0xe72(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
lea    0x80(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1228 <checkout_commit+0x98>
lea    0xe4b(%rip),%r15
lea    0x80(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <strtok@plt>
mov    %rbp,%rdi
call   10a0 <fs_rm@plt>
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1200 <checkout_commit+0x70>
mov    %rbx,%rdi
call   1030 <fclose@plt>
lea    0xe06(%rip),%rsi
lea    0xe06(%rip),%rdx
lea    0xdec(%rip),%r8
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
lea    0xdd8(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%r12
mov    %rsp,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     12de <checkout_commit+0x14e>
lea    0xdb3(%rip),%r15
mov    %rsp,%rbx
lea    0xdbc(%rip),%r13
lea    0x1a0(%rsp),%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strtok@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <fs_cp@plt>
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r12,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    12a0 <checkout_commit+0x110>
lea    0xd44(%rip),%rsi
lea    0x100(%rsp),%rdi
call   1060 <fs_cp@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
jmp    1308 <checkout_commit+0x178>
lea    0xd39(%rip),%rsi
call   10b0 <write_string_to_file@plt>
lea    0xd4e(%rip),%rdi
mov    %r14,%rsi
call   10b0 <write_string_to_file@plt>
xor    %eax,%eax
add    $0x268,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"0000000000000000000000000000000000000000\00"
@.str.1 = [16 x i8] c".beargit/.index\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [7 x i8] c"%s%s%s\00"
@.str.6 = [10 x i8] c".beargit/\00"
@.str.7 = [8 x i8] c"/.index\00"
@.str.8 = [15 x i8] c".beargit/%s/%s\00"
@.str.9 = [15 x i8] c".beargit/.prev\00"
define i32 @checkout_commit(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [128 x i8]
%5 = alloca [150 x i8]
%6 = alloca i32*
%7 = alloca [128 x i8]
%8 = alloca [200 x i8]
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @write_string_to_file(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %54
14: ; preds = %1
%15 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %15, i32** %3
br label %16
16: ; preds = %21, %14
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%18 = load i32*, i32** %3
%19 = call i64 @fgets(i8* %17, i32 128, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %16
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%23 = call i32 @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%25 = call i32 @fs_rm(i8* %24)
br label %16
26: ; preds = %16
%27 = load i32*, i32** %3
%28 = call i32 @fclose(i32* %27)
%29 = getelementptr inbounds [150 x i8], [150 x i8]* %5, i64 0, i64 0
%30 = load i8*, i8** %2
%31 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%32 = getelementptr inbounds [150 x i8], [150 x i8]* %5, i64 0, i64 0
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %33, i32** %6
br label %34
34: ; preds = %39, %26
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%36 = load i32*, i32** %6
%37 = call i64 @fgets(i8* %35, i32 128, i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %49
39: ; preds = %34
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%41 = call i32 @strtok(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%42 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%43 = load i8*, i8** %2
%44 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%45 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %42, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %43, i8* %44)
%46 = getelementptr inbounds [200 x i8], [200 x i8]* %8, i64 0, i64 0
%47 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%48 = call i32 @fs_cp(i8* %46, i8* %47)
br label %34
49: ; preds = %34
%50 = getelementptr inbounds [150 x i8], [150 x i8]* %5, i64 0, i64 0
%51 = call i32 @fs_cp(i8* %50, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%52 = load i32*, i32** %6
%53 = call i32 @fclose(i32* %52)
br label %54
54: ; preds = %49, %12
%55 = load i8*, i8** %2
%56 = call i32 @write_string_to_file(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0), i8* %55)
ret i32 0
}

LLVM IR (-O2):
@.str = [41 x i8] c"0000000000000000000000000000000000000000\00"
@.str.1 = [16 x i8] c".beargit/.index\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [7 x i8] c"%s%s%s\00"
@.str.6 = [10 x i8] c".beargit/\00"
@.str.7 = [8 x i8] c"/.index\00"
@.str.8 = [15 x i8] c".beargit/%s/%s\00"
@.str.9 = [15 x i8] c".beargit/.prev\00"
define i32 @checkout_commit(i8* %0) {
%2 = alloca [128 x i8]
%3 = alloca [150 x i8]
%4 = alloca [128 x i8]
%5 = alloca [200 x i8]
%6 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = tail call i32 @write_string_to_file(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %39
10: ; preds = %1
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%13 = call i64 @fgets(i8* %12, i32 128, i32* %11)
%14 = icmp eq i64 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %10, %15
%16 = call i32 @strtok(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%17 = call i32 @fs_rm(i8* %12)
%18 = call i64 @fgets(i8* %12, i32 128, i32* %11)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %15
20: ; preds = %15, %10
%21 = call i32 @fclose(i32* %11)
%22 = getelementptr inbounds [150 x i8], [150 x i8]* %3, i64 0, i64 0
%23 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %22, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
%24 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%26 = call i64 @fgets(i8* %25, i32 128, i32* %24)
%27 = icmp eq i64 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %20
%29 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
br label %30
30: ; preds = %28, %30
%31 = call i32 @strtok(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %29, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i8* %0, i8* %25)
%33 = call i32 @fs_cp(i8* %29, i8* %25)
%34 = call i64 @fgets(i8* %25, i32 128, i32* %24)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %30
36: ; preds = %30, %20
%37 = call i32 @fs_cp(i8* %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @fclose(i32* %24)
br label %39
39: ; preds = %36, %8
%40 = call i32 @write_string_to_file(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.9, i64 0, i64 0), i8* %0)
ret i32 0
}