// File: /workspace/github_c_makefile_star2_O3/MicBrain[P]Git-Sample/beargit.c
// Function: beargit_checkout
int beargit_checkout(const char* arg, int new_branch) {
  char current_branch[BRANCHNAME_SIZE];
  read_string_from_file(".beargit/.current_branch", current_branch, BRANCHNAME_SIZE);
  if (strlen(current_branch)) {
    char current_branch_file[BRANCHNAME_SIZE+100];
    sprintf(current_branch_file, ".beargit/.branch_%s", current_branch);
    fs_cp(".beargit/.prev", current_branch_file);
  }
  if (is_it_a_commit_id(arg)) {
    char commit_dir[FILENAME_SIZE + 100] = ".beargit/";
    strcat(commit_dir, arg);
    if (!fs_check_dir_exists(commit_dir)) {
      fprintf(stderr, "ERROR: Commit %s does not exist\n", arg);
      return 1;
    }
    write_string_to_file(".beargit/.current_branch", "");
    return checkout_commit(arg);
  }
  const char* branch_name = arg;
  int branch_exists = (get_branch_number(branch_name) >= 0);
  if (branch_exists && new_branch) {
    fprintf(stderr, "ERROR: A branch named %s already exists\n", branch_name);
    return 1;
  } else if (!branch_exists && !new_branch) {
    fprintf(stderr, "ERROR: No branch %s exists\n", branch_name);
    return 1;
  }
  char branch_file[FILENAME_SIZE] = ".beargit/.branch_";
  strcat(branch_file, branch_name);
  if (new_branch) {
    FILE* fbranches = fopen(".beargit/.branches", "a");
    fprintf(fbranches, "%s\n", branch_name);
    fclose(fbranches);
    fs_cp(".beargit/.prev", branch_file); 
  }
  write_string_to_file(".beargit/.current_branch", branch_name);
  char branch_head_commit_id[COMMIT_ID_SIZE];
  read_string_from_file(branch_file, branch_head_commit_id, COMMIT_ID_SIZE);
  return checkout_commit(branch_head_commit_id);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x388, %rsp            # imm = 0x388
movl %esi, %r14d
movq %rdi, %rbx
leaq 0x6f5(%rip), %rdi       # 0x41c6
leaq -0x130(%rbp), %r15
movq %r15, %rsi
movl $0x80, %edx
callq 0x328e
cmpb $0x0, (%r15)
je 0x3b19
leaq 0x817(%rip), %rsi       # 0x4309
leaq -0x3a0(%rbp), %r15
leaq -0x130(%rbp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x1170
leaq 0x67d(%rip), %rdi       # 0x418e
movq %r15, %rsi
callq 0x3363
movq %rbx, %rdi
callq 0x3a6b
testl %eax, %eax
je 0x3b91
leaq 0xc64(%rip), %rsi       # 0x4790
leaq -0x3a0(%rbp), %r14
movl $0x264, %edx            # imm = 0x264
movq %r14, %rdi
callq 0x10f0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1160
leaq -0xb0(%rbp), %rsi
movq %r14, %rdi
callq 0x10e0
cmpl $-0x1, %eax
je 0x3c71
movl $0xf000, %eax           # imm = 0xF000
andl -0x98(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x3c71
leaq 0x646(%rip), %rdi       # 0x41c6
leaq 0x828(%rip), %rsi       # 0x43af
callq 0x2caf
jmp 0x3c59
movq %rbx, %rdi
callq 0x373d
testl %eax, %eax
sets %cl
testl %r14d, %r14d
sete %dl
orb %cl, %dl
je 0x3c84
testl %r14d, %r14d
setne %cl
testl %eax, %eax
setns %al
orb %cl, %al
je 0x3c97
leaq 0xe3a(%rip), %rsi       # 0x4a00
leaq -0x3a0(%rbp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
callq 0x10f0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x1160
testl %r14d, %r14d
je 0x3c2f
leaq 0x583(%rip), %rdi       # 0x4174
leaq 0x78b(%rip), %rsi       # 0x4383
callq 0x1120
movq %rax, %r14
leaq 0x699(%rip), %rsi       # 0x42a0
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10d0
movq %r14, %rdi
callq 0x1070
leaq 0x56b(%rip), %rdi       # 0x418e
leaq -0x3a0(%rbp), %rsi
callq 0x3363
leaq 0x590(%rip), %rdi       # 0x41c6
movq %rbx, %rsi
callq 0x2caf
leaq -0x3a0(%rbp), %rdi
leaq -0xb0(%rbp), %rbx
movq %rbx, %rsi
movl $0x29, %edx
callq 0x328e
movq %rbx, %rdi
callq 0x38d6
xorl %eax, %eax
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x3380(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x69b(%rip), %rsi       # 0x431d
jmp 0x3ca8
movq 0x336d(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x6a9(%rip), %rsi       # 0x433e
jmp 0x3ca8
movq 0x335a(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x6bf(%rip), %rsi       # 0x4367
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10d0
movl $0x1, %eax
jmp 0x3c63
nopl (%rax)