// File: /workspace/github_c_makefile_star2_O0/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
subq $0x650, %rsp            # imm = 0x650
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
leaq -0xa0(%rbp), %rsi
leaq 0x112c(%rip), %rdi      # 0x61cc
movl $0x80, %edx
callq 0x3a30
leaq -0xa0(%rbp), %rdi
callq 0x1070
cmpq $0x0, %rax
je 0x50eb
leaq -0x190(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
leaq 0x123e(%rip), %rsi      # 0x630f
movb $0x0, %al
callq 0x1150
leaq -0x190(%rbp), %rsi
leaq 0x10ae(%rip), %rdi      # 0x6194
callq 0x3d80
movq -0x10(%rbp), %rdi
callq 0x5000
cmpl $0x0, %eax
je 0x5182
leaq -0x400(%rbp), %rdi
leaq 0x1695(%rip), %rsi      # 0x67a0
movl $0x264, %edx            # imm = 0x264
callq 0x10e0
leaq -0x400(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1140
leaq -0x400(%rbp), %rdi
callq 0x52f0
cmpl $0x0, %eax
jne 0x515e
movq 0x3ebb(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x11d8(%rip), %rsi      # 0x6323
movb $0x0, %al
callq 0x10c0
movl $0x1, -0x4(%rbp)
jmp 0x52e0
leaq 0x1067(%rip), %rdi      # 0x61cc
leaq 0x1249(%rip), %rsi      # 0x63b5
callq 0x1f10
movq -0x10(%rbp), %rdi
callq 0x4e80
movl %eax, -0x4(%rbp)
jmp 0x52e0
movq -0x10(%rbp), %rax
movq %rax, -0x408(%rbp)
movq -0x408(%rbp), %rdi
callq 0x4cc0
cmpl $0x0, %eax
setge %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x40c(%rbp)
cmpl $0x0, -0x40c(%rbp)
je 0x51e4
cmpl $0x0, -0x14(%rbp)
je 0x51e4
movq 0x3e38(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
movq -0x408(%rbp), %rdx
leaq 0x1173(%rip), %rsi      # 0x6344
movb $0x0, %al
callq 0x10c0
movl $0x1, -0x4(%rbp)
jmp 0x52e0
cmpl $0x0, -0x40c(%rbp)
jne 0x521e
cmpl $0x0, -0x14(%rbp)
jne 0x521e
movq 0x3dfe(%rip), %rax      # 0x8ff8
movq (%rax), %rdi
movq -0x408(%rbp), %rdx
leaq 0x1162(%rip), %rsi      # 0x636d
movb $0x0, %al
callq 0x10c0
movl $0x1, -0x4(%rbp)
jmp 0x52e0
jmp 0x5220
leaq -0x610(%rbp), %rdi
leaq 0x17e2(%rip), %rsi      # 0x6a10
movl $0x200, %edx            # imm = 0x200
callq 0x10e0
leaq -0x610(%rbp), %rdi
movq -0x408(%rbp), %rsi
callq 0x1140
cmpl $0x0, -0x14(%rbp)
je 0x52a6
leaq 0xf22(%rip), %rdi       # 0x617a
leaq 0x112a(%rip), %rsi      # 0x6389
callq 0x1100
movq %rax, -0x618(%rbp)
movq -0x618(%rbp), %rdi
movq -0x408(%rbp), %rdx
leaq 0x1026(%rip), %rsi      # 0x62a6
movb $0x0, %al
callq 0x10c0
movq -0x618(%rbp), %rdi
callq 0x1060
leaq -0x610(%rbp), %rsi
leaq 0xef3(%rip), %rdi       # 0x6194
callq 0x3d80
movq -0x408(%rbp), %rsi
leaq 0xf18(%rip), %rdi       # 0x61cc
callq 0x1f10
leaq -0x610(%rbp), %rdi
leaq -0x650(%rbp), %rsi
movl $0x29, %edx
callq 0x3a30
leaq -0x650(%rbp), %rdi
callq 0x4e80
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x650, %rsp            # imm = 0x650
popq %rbp
retq
nopl (%rax)