// File: /workspace/github_c_makefile_star2_O1/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 0x6a1(%rip), %rdi       # 0x41cc
leaq -0x130(%rbp), %r15
movq %r15, %rsi
movl $0x80, %edx
callq 0x32e8
cmpb $0x0, (%r15)
je 0x3b73
leaq 0x7c3(%rip), %rsi       # 0x430f
leaq -0x3a0(%rbp), %r15
leaq -0x130(%rbp), %rdx
movq %r15, %rdi
xorl %eax, %eax
callq 0x1170
leaq 0x629(%rip), %rdi       # 0x4194
movq %r15, %rsi
callq 0x33bd
movq %rbx, %rdi
callq 0x3ac5
testl %eax, %eax
je 0x3beb
leaq 0xc1a(%rip), %rsi       # 0x47a0
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 0x3ccb
movl $0xf000, %eax           # imm = 0xF000
andl -0x98(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x3ccb
leaq 0x5f2(%rip), %rdi       # 0x41cc
leaq 0x7d4(%rip), %rsi       # 0x43b5
callq 0x2d63
jmp 0x3cb3
movq %rbx, %rdi
callq 0x3797
testl %eax, %eax
sets %cl
testl %r14d, %r14d
sete %dl
orb %cl, %dl
je 0x3cde
testl %r14d, %r14d
setne %cl
testl %eax, %eax
setns %al
orb %cl, %al
je 0x3cf1
leaq 0xdf0(%rip), %rsi       # 0x4a10
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 0x3c89
leaq 0x52f(%rip), %rdi       # 0x417a
leaq 0x737(%rip), %rsi       # 0x4389
callq 0x1120
movq %rax, %r14
leaq 0x645(%rip), %rsi       # 0x42a6
movq %rax, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10d0
movq %r14, %rdi
callq 0x1070
leaq 0x517(%rip), %rdi       # 0x4194
leaq -0x3a0(%rbp), %rsi
callq 0x33bd
leaq 0x53c(%rip), %rdi       # 0x41cc
movq %rbx, %rsi
callq 0x2d63
leaq -0x3a0(%rbp), %rdi
leaq -0xb0(%rbp), %rbx
movq %rbx, %rsi
movl $0x29, %edx
callq 0x32e8
movq %rbx, %rdi
callq 0x3930
xorl %eax, %eax
addq $0x388, %rsp            # imm = 0x388
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x3326(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x647(%rip), %rsi       # 0x4323
jmp 0x3d02
movq 0x3313(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x655(%rip), %rsi       # 0x4344
jmp 0x3d02
movq 0x3300(%rip), %rax      # 0x6ff8
movq (%rax), %rdi
leaq 0x66b(%rip), %rsi       # 0x436d
movq %rbx, %rdx
xorl %eax, %eax
callq 0x10d0
movl $0x1, %eax
jmp 0x3cbd
nop