// File: /workspace/github_c_makefile_star2_O2/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 0xd20(%rip), %rdi       # 0x41c6
leaq -0x130(%rbp), %r15
movq %r15, %rsi
movl $0x80, %edx
callq 0x26fd
cmpb $0x0, (%r15)
je 0x34ee
leaq 0xe42(%rip), %rsi       # 0x4309
leaq -0x3a0(%rbp), %r15
leaq -0x130(%rbp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x1170
leaq 0xca8(%rip), %rdi       # 0x418e
movq %r15, %rsi
callq 0x28e9
movq %rbx, %rdi
callq 0x3442
testl %eax, %eax
je 0x3566
leaq 0x128f(%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 0x3644
movl $0xf000, %eax           # imm = 0xF000
andl -0x98(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x3644
leaq 0xc71(%rip), %rdi       # 0x41c6
leaq 0xe53(%rip), %rsi       # 0x43af
callq 0x1a45
jmp 0x362c
movq %rbx, %rdi
callq 0x3171
testl %eax, %eax
sets %cl
testl %r14d, %r14d
sete %dl
orb %cl, %dl
je 0x3657
testl %r14d, %r14d
setne %cl
testl %eax, %eax
setns %al
orb %cl, %al
je 0x366a
leaq 0x1465(%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 0x3604
leaq 0xbae(%rip), %rdi       # 0x4174
leaq 0xdb6(%rip), %rsi       # 0x4383
callq 0x1120
movq %rax, %r14
leaq 0xcc4(%rip), %rsi       # 0x42a0
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10d0
movq %r14, %rdi
callq 0x1070
leaq 0xb96(%rip), %rdi       # 0x418e
leaq -0x3a0(%rbp), %rsi
callq 0x28e9
leaq 0xbbb(%rip), %rdi       # 0x41c6
movq %rbx, %rsi
callq 0x1a45
leaq -0x3a0(%rbp), %rdi
leaq -0xb0(%rbp), %rbx
pushq $0x29
popq %rdx
movq %rbx, %rsi
callq 0x26fd
movq %rbx, %rdi
callq 0x32da
xorl %eax, %eax
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x39ad(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0xcc8(%rip), %rsi       # 0x431d
jmp 0x367b
movq 0x399a(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0xcd6(%rip), %rsi       # 0x433e
jmp 0x367b
movq 0x3987(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0xcec(%rip), %rsi       # 0x4367
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10d0
pushq $0x1
popq %rax
jmp 0x3636
nop