// File: /workspace/github_c_makefile_star2_O0/ejunjsh[P]mycc/cg.c
// Function: cgloadvar
int cgloadvar(struct symtable *sym, int op) {
  int r, postreg, offset = 1;

  // 分配一个新的寄存器
  r = cgallocreg();

  // 如果是指针，用指针指向的类型大小作为++或--的大小
  // 如果不是，那大小就是1
  if (ptrtype(sym->type))
    offset = typesize(value_at(sym->type), sym->ctype);

  // 如果是--，就把它变成负数
  if (op == A_PREDEC || op == A_POSTDEC)
    offset = -offset;

  // 如果是++i
  if (op == A_PREINC || op == A_PREDEC) {
    // 获取变量地址
    if (sym->class == C_LOCAL || sym->class == C_PARAM)
      fprintf(Outfile, "\tleaq\t%d(%%rbp), %s\n", sym->stu.st_posn, reglist[r]);
    else
      fprintf(Outfile, "\tleaq\t%s(%%rip), %s\n", sym->name, reglist[r]);

    // 改变那个地址里面的值
    switch (sym->size) {
    case 1:
      fprintf(Outfile, "\taddb\t$%d,(%s)\n", offset, reglist[r]);
      break;
    case 4:
      fprintf(Outfile, "\taddl\t$%d,(%s)\n", offset, reglist[r]);
      break;
    case 8:
      fprintf(Outfile, "\taddq\t$%d,(%s)\n", offset, reglist[r]);
      break;
    }
  }

  // 加载变量值到寄存器
  if (sym->class == C_LOCAL || sym->class == C_PARAM) {
    switch (sym->size) {
    case 1:
      fprintf(Outfile, "\tmovzbq\t%d(%%rbp), %s\n", sym->stu.st_posn, reglist[r]);
      break;
    case 4:
      fprintf(Outfile, "\tmovslq\t%d(%%rbp), %s\n", sym->stu.st_posn, reglist[r]);
      break;
    case 8:
      fprintf(Outfile, "\tmovq\t%d(%%rbp), %s\n", sym->stu.st_posn, reglist[r]);
    }
  } else {
    switch (sym->size) {
    case 1:
      fprintf(Outfile, "\tmovzbq\t%s(%%rip), %s\n", sym->name, reglist[r]);
      break;
    case 4:
      fprintf(Outfile, "\tmovslq\t%s(%%rip), %s\n", sym->name, reglist[r]);
      break;
    case 8:
      fprintf(Outfile, "\tmovq\t%s(%%rip), %s\n", sym->name, reglist[r]);
    }
  }

  // 如果是i++
  if (op == A_POSTINC || op == A_POSTDEC) {
    postreg = cgallocreg(); // 分配一个新寄存器

    // 获取变量地址
    if (sym->class == C_LOCAL || sym->class == C_PARAM)
      fprintf(Outfile, "\tleaq\t%d(%%rbp), %s\n", sym->stu.st_posn,
	      reglist[postreg]);
    else
      fprintf(Outfile, "\tleaq\t%s(%%rip), %s\n", sym->name,
	      reglist[postreg]);

    // 改变那个地址里面的值
    switch (sym->size) {
    case 1:
      fprintf(Outfile, "\taddb\t$%d,(%s)\n", offset, reglist[postreg]);
      break;
    case 4:
      fprintf(Outfile, "\taddl\t$%d,(%s)\n", offset, reglist[postreg]);
      break;
    case 8:
      fprintf(Outfile, "\taddq\t$%d,(%s)\n", offset, reglist[postreg]);
      break;
    }

    // 最后释放这个寄存器
    cgfreereg(postreg);
  }

  // 返回带有值的寄存器
  return (r);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl $0x1, -0x18(%rbp)
callq 0x23f0
movl %eax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl 0x10(%rax), %edi
callq 0xb720
cmpl $0x0, %eax
je 0x2a2d
movq -0x8(%rbp), %rax
movl 0x10(%rax), %edi
callq 0xb780
movl %eax, %edi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
callq 0xb7c0
movl %eax, -0x18(%rbp)
cmpl $0x28, -0xc(%rbp)
je 0x2a39
cmpl $0x2a, -0xc(%rbp)
jne 0x2a41
xorl %eax, %eax
subl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
cmpl $0x27, -0xc(%rbp)
je 0x2a51
cmpl $0x28, -0xc(%rbp)
jne 0x2b6f
movq -0x8(%rbp), %rax
cmpl $0x2, 0x24(%rax)
je 0x2a65
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
jne 0x2a94
leaq 0xfaac(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl (%rax), %edx
movslq -0x10(%rbp), %rcx
leaq 0xf650(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x9837(%rip), %rsi      # 0xc2c2
movb $0x0, %al
callq 0x20e0
jmp 0x2ac3
leaq 0xfa7d(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdx
movslq -0x10(%rbp), %rcx
leaq 0xf61f(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x981b(%rip), %rsi      # 0xc2d7
movb $0x0, %al
callq 0x20e0
movq -0x8(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x1c(%rbp)
subl $0x1, %eax
je 0x2aeb
jmp 0x2ad4
movl -0x1c(%rbp), %eax
subl $0x4, %eax
je 0x2b17
jmp 0x2ade
movl -0x1c(%rbp), %eax
subl $0x8, %eax
je 0x2b43
jmp 0x2b6d
leaq 0xfa26(%rip), %rax      # 0x12518
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movslq -0x10(%rbp), %rcx
leaq 0xf5cd(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x97de(%rip), %rsi      # 0xc2ec
movb $0x0, %al
callq 0x20e0
jmp 0x2b6d
leaq 0xf9fa(%rip), %rax      # 0x12518
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movslq -0x10(%rbp), %rcx
leaq 0xf5a1(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x97c2(%rip), %rsi      # 0xc2fc
movb $0x0, %al
callq 0x20e0
jmp 0x2b6d
leaq 0xf9ce(%rip), %rax      # 0x12518
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movslq -0x10(%rbp), %rcx
leaq 0xf575(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x97a6(%rip), %rsi      # 0xc30c
movb $0x0, %al
callq 0x20e0
jmp 0x2b6f
movq -0x8(%rbp), %rax
cmpl $0x2, 0x24(%rax)
je 0x2b87
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
jne 0x2c3f
movq -0x8(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x20(%rbp)
subl $0x1, %eax
je 0x2baf
jmp 0x2b98
movl -0x20(%rbp), %eax
subl $0x4, %eax
je 0x2bde
jmp 0x2ba2
movl -0x20(%rbp), %eax
subl $0x8, %eax
je 0x2c0d
jmp 0x2c3a
leaq 0xf962(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl (%rax), %edx
movslq -0x10(%rbp), %rcx
leaq 0xf506(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x9747(%rip), %rsi      # 0xc31c
movb $0x0, %al
callq 0x20e0
jmp 0x2c3a
leaq 0xf933(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl (%rax), %edx
movslq -0x10(%rbp), %rcx
leaq 0xf4d7(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x972f(%rip), %rsi      # 0xc333
movb $0x0, %al
callq 0x20e0
jmp 0x2c3a
leaq 0xf904(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl (%rax), %edx
movslq -0x10(%rbp), %rcx
leaq 0xf4a8(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x9717(%rip), %rsi      # 0xc34a
movb $0x0, %al
callq 0x20e0
jmp 0x2cfa
movq -0x8(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x24(%rbp)
subl $0x1, %eax
je 0x2c67
jmp 0x2c50
movl -0x24(%rbp), %eax
subl $0x4, %eax
je 0x2c98
jmp 0x2c5a
movl -0x24(%rbp), %eax
subl $0x8, %eax
je 0x2cc9
jmp 0x2cf8
leaq 0xf8aa(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdx
movslq -0x10(%rbp), %rcx
leaq 0xf44c(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x96d0(%rip), %rsi      # 0xc35f
movb $0x0, %al
callq 0x20e0
jmp 0x2cf8
leaq 0xf879(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdx
movslq -0x10(%rbp), %rcx
leaq 0xf41b(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x96b6(%rip), %rsi      # 0xc376
movb $0x0, %al
callq 0x20e0
jmp 0x2cf8
leaq 0xf848(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdx
movslq -0x10(%rbp), %rcx
leaq 0xf3ea(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x969c(%rip), %rsi      # 0xc38d
movb $0x0, %al
callq 0x20e0
jmp 0x2cfa
cmpl $0x29, -0xc(%rbp)
je 0x2d0a
cmpl $0x2a, -0xc(%rbp)
jne 0x2e36
callq 0x23f0
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
cmpl $0x2, 0x24(%rax)
je 0x2d26
movq -0x8(%rbp), %rax
cmpl $0x3, 0x24(%rax)
jne 0x2d55
leaq 0xf7eb(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movl (%rax), %edx
movslq -0x14(%rbp), %rcx
leaq 0xf38f(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x9576(%rip), %rsi      # 0xc2c2
movb $0x0, %al
callq 0x20e0
jmp 0x2d84
leaq 0xf7bc(%rip), %rax      # 0x12518
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdx
movslq -0x14(%rbp), %rcx
leaq 0xf35e(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x955a(%rip), %rsi      # 0xc2d7
movb $0x0, %al
callq 0x20e0
movq -0x8(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x28(%rbp)
subl $0x1, %eax
je 0x2dac
jmp 0x2d95
movl -0x28(%rbp), %eax
subl $0x4, %eax
je 0x2dd8
jmp 0x2d9f
movl -0x28(%rbp), %eax
subl $0x8, %eax
je 0x2e04
jmp 0x2e2e
leaq 0xf765(%rip), %rax      # 0x12518
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movslq -0x14(%rbp), %rcx
leaq 0xf30c(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x951d(%rip), %rsi      # 0xc2ec
movb $0x0, %al
callq 0x20e0
jmp 0x2e2e
leaq 0xf739(%rip), %rax      # 0x12518
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movslq -0x14(%rbp), %rcx
leaq 0xf2e0(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x9501(%rip), %rsi      # 0xc2fc
movb $0x0, %al
callq 0x20e0
jmp 0x2e2e
leaq 0xf70d(%rip), %rax      # 0x12518
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movslq -0x14(%rbp), %rcx
leaq 0xf2b4(%rip), %rax      # 0x120d0
movq (%rax,%rcx,8), %rcx
leaq 0x94e5(%rip), %rsi      # 0xc30c
movb $0x0, %al
callq 0x20e0
movl -0x14(%rbp), %edi
callq 0x24c0
movl -0x10(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop