// File: /workspace/github_c_makefile_star2_O0/thiagok00[P]INF1715-Compilers/GeraCodigo.c
// Function: gera_codigo_bloco
static void gera_codigo_bloco(Bloco *b) {

  DefVarL* defsvar = b->declVars;
  CMDL *cmds = b->cmds;
  while (defsvar != NULL){
    DefVar *v = defsvar->v;
    int idTemp = gen_id();
    v->idTemp = idTemp;

    fprintf(output,"\t%%t%d = alloca %s\n",v->idTemp,tipo_string(v->tipo));

    defsvar = defsvar->prox;
  }
  while (cmds != NULL) {
      int idtemp = -1;
      Var *varatr = NULL;

      CMD *c = cmds->c;
      switch(c->tag){
        case CMD_ATR:
          varatr = c->u.atr.var;
          if (varatr->tag == vVar) {
            char *varstrtipo = tipo_string(varatr->tipo);
            int valexp = gera_codigo_expressao(c->u.atr.exp);

            if(varatr->tipo->tipo_base == bChar && varatr->tipo->tag == base){
              idtemp = gen_id();
              //%2 = trunc i32 %1 to i8
              fprintf(output,"\t%%t%d = trunc i32 %%t%d to i8\n",idtemp,valexp);
            }
            else {
              idtemp = valexp;
            }
            //store i32 %2, i32* %lol
            if (varatr->u.vvar.escopo == EscopoGlobal)
              fprintf(output, "\tstore %s %%t%d, %s* @t%d\n",varstrtipo,idtemp,varstrtipo,varatr->u.vvar.d.def->idTemp);
            else
              fprintf(output, "\tstore %s %%t%d, %s* %%t%d\n",varstrtipo,idtemp,varstrtipo,varatr->u.vvar.d.def->idTemp);

          }
          //TODO ARRAY
        break;
        case CMD_PRINT:
          idtemp = gera_codigo_expressao(c->u.e);
          if (c->u.e->tipo->tag == base && c->u.e->tipo->tipo_base == bInt) {
            int lastid = idtemp;
            idtemp = gen_id();
            //%2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i32 %1)
            fprintf(output,"\t%%t%d = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4  x i8], [4 x i8]* @.intprint, i32 0, i32 0), i32 %%t%d)",idtemp,lastid);
        }
          else if (c->u.e->tipo->tag == base && c->u.e->tipo->tipo_base == bFloat) {
            int lastid = idtemp;
            idtemp = gen_id();
            //%2 = fpext float %1 to double
            fprintf(output,"\t%%t%d = fpext float %%t%d to double\n",idtemp,lastid);
            lastid = idtemp;
            idtemp = gen_id();
            //%3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), double %2)
            fprintf(output,"\t%%t%d = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.floatprint, i32 0, i32 0), double %%t%d)\n",idtemp,lastid);
          }
        else if (c->u.e->tipo->tag == array && c->u.e->tipo->tipo_base == bChar){
           int idTemp = gen_id();
           fprintf(output,"\t%%t%d = %s",idTemp,lastStringToPrint);
        }
        break;
        case CMD_RETURNVOID:
          fprintf(output,"\tret void\n");
        break;
        case CMD_RETURN:
          idtemp = gera_codigo_expressao(c->u.e);
          fprintf(output,"\tret %s %%t%d\n",tipo_string(c->u.e->tipo),idtemp);
        break;
        case CMD_IF:
          gera_codigo_if(c);
        break;
        case CMD_IFELSE:
          gera_codigo_ifelse(c);
        break;
        case CMD_WHILE:
          gera_codigo_while(c);
          break;
        default:
        break;
      }
      cmds = cmds->prox;
  }/* fim while */

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x10(%rbp)
je 0x785f
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
callq 0x77b0
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq 0x696a(%rip), %rax      # 0xe188
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7360
movq -0x60(%rbp), %rdi
movl -0x54(%rbp), %edx
movq %rax, %rcx
leaq 0x327b(%rip), %rsi      # 0xaac5
movb $0x0, %al
callq 0x10e0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x10(%rbp)
jmp 0x77f3
jmp 0x7861
cmpq $0x0, -0x18(%rbp)
je 0x7b55
movl $0xffffffff, -0x28(%rbp) # imm = 0xFFFFFFFF
movq $0x0, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x68(%rbp)
subq $0x9, %rax
ja 0x7b42
movq -0x68(%rbp), %rax
leaq 0x310b(%rip), %rcx      # 0xa9b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x7994
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7360
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x7b60
movl %eax, -0x44(%rbp)
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x1, 0x4(%rax)
jne 0x7928
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x0, (%rax)
jne 0x7928
callq 0x77b0
movl %eax, -0x28(%rbp)
movq 0x6876(%rip), %rdi      # 0xe188
movl -0x28(%rbp), %edx
movl -0x44(%rbp), %ecx
leaq 0x31da(%rip), %rsi      # 0xaaf9
movb $0x0, %al
callq 0x10e0
jmp 0x792e
movl -0x44(%rbp), %eax
movl %eax, -0x28(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x7966
movq 0x6849(%rip), %rdi      # 0xe188
movq -0x40(%rbp), %rdx
movl -0x28(%rbp), %ecx
movq -0x40(%rbp), %r8
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x10(%rax), %r9d
leaq 0x31bc(%rip), %rsi      # 0xab19
movb $0x0, %al
callq 0x10e0
jmp 0x7992
movq 0x681b(%rip), %rdi      # 0xe188
movq -0x40(%rbp), %rdx
movl -0x28(%rbp), %ecx
movq -0x40(%rbp), %r8
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movl 0x10(%rax), %r9d
leaq 0x314e(%rip), %rsi      # 0xaad9
movb $0x0, %al
callq 0x10e0
jmp 0x7994
jmp 0x7b44
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7b60
movl %eax, -0x28(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x0, (%rax)
jne 0x79fa
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x2, 0x4(%rax)
jne 0x79fa
movl -0x28(%rbp), %eax
movl %eax, -0x48(%rbp)
callq 0x77b0
movl %eax, -0x28(%rbp)
movq 0x67a7(%rip), %rdi      # 0xe188
movl -0x28(%rbp), %edx
movl -0x48(%rbp), %ecx
leaq 0x3146(%rip), %rsi      # 0xab34
movb $0x0, %al
callq 0x10e0
jmp 0x7abf
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x0, (%rax)
jne 0x7a71
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x3, 0x4(%rax)
jne 0x7a71
movl -0x28(%rbp), %eax
movl %eax, -0x4c(%rbp)
callq 0x77b0
movl %eax, -0x28(%rbp)
movq 0x6756(%rip), %rdi      # 0xe188
movl -0x28(%rbp), %edx
movl -0x4c(%rbp), %ecx
leaq 0x3171(%rip), %rsi      # 0xabb0
movb $0x0, %al
callq 0x10e0
movl -0x28(%rbp), %eax
movl %eax, -0x4c(%rbp)
callq 0x77b0
movl %eax, -0x28(%rbp)
movq 0x672d(%rip), %rdi      # 0xe188
movl -0x28(%rbp), %edx
movl -0x4c(%rbp), %ecx
leaq 0x316e(%rip), %rsi      # 0xabd6
movb $0x0, %al
callq 0x10e0
jmp 0x7abd
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x1, (%rax)
jne 0x7abb
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rax
cmpl $0x1, 0x4(%rax)
jne 0x7abb
callq 0x77b0
movl %eax, -0x50(%rbp)
movq 0x66e5(%rip), %rdi      # 0xe188
movl -0x50(%rbp), %edx
movq 0x66f3(%rip), %rcx      # 0xe1a0
leaq 0x31a3(%rip), %rsi      # 0xac57
movb $0x0, %al
callq 0x10e0
jmp 0x7abd
jmp 0x7abf
jmp 0x7b44
movq 0x66bd(%rip), %rdi      # 0xe188
leaq 0x3191(%rip), %rsi      # 0xac63
movb $0x0, %al
callq 0x10e0
jmp 0x7b44
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x7b60
movl %eax, -0x28(%rbp)
movq 0x6696(%rip), %rax      # 0xe188
movq %rax, -0x70(%rbp)
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x7360
movq -0x70(%rbp), %rdi
movq %rax, %rdx
movl -0x28(%rbp), %ecx
leaq 0x3156(%rip), %rsi      # 0xac6e
movb $0x0, %al
callq 0x10e0
jmp 0x7b44
movq -0x38(%rbp), %rdi
callq 0x7c40
jmp 0x7b44
movq -0x38(%rbp), %rdi
callq 0x7d10
jmp 0x7b44
movq -0x38(%rbp), %rdi
callq 0x7e20
jmp 0x7b44
jmp 0x7b44
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x7861
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)