// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq (%rdi), %r12
movq 0x8(%rdi), %r13
testq %r12, %r12
je 0x51a4
leaq 0x2cdc(%rip), %rbx      # 0x798d
movq (%r12), %rax
movl 0x54f4(%rip), %r14d     # 0xa1b0
leal 0x1(%r14), %ecx
movl %ecx, 0x54ea(%rip)      # 0xa1b0
movl %r14d, 0x10(%rax)
movq 0x54cf(%rip), %r15      # 0xa1a0
movq 0x8(%rax), %rdi
callq 0x4809
movq %r15, %rdi
movq %rbx, %rsi
movl %r14d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1110
movq 0x8(%r12), %r12
testq %r12, %r12
jne 0x4cb1
jmp 0x51a4
movq (%r13), %rbx
movl (%rbx), %eax
cmpq $0x9, %rax
ja 0x51a0
leaq 0x2b9d(%rip), %rcx      # 0x78b0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rbx), %r12
cmpl $0x0, (%r12)
jne 0x51a0
movq 0x8(%r12), %rdi
callq 0x4809
movq %rax, %r15
movq 0x10(%rbx), %rdi
callq 0x51bc
movl %eax, %ecx
movq 0x8(%r12), %rax
cmpl $0x1, 0x4(%rax)
jne 0x4d7f
cmpl $0x0, (%rax)
jne 0x4d7f
movl 0x5456(%rip), %r14d     # 0xa1b0
leal 0x1(%r14), %eax
movl %eax, 0x544c(%rip)      # 0xa1b0
movq 0x5435(%rip), %rdi      # 0xa1a0
leaq 0x2c4f(%rip), %rsi      # 0x79c1
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movl %r14d, %ecx
cmpl $0x0, 0x18(%r12)
movq 0x5414(%rip), %rdi      # 0xa1a0
movq 0x20(%r12), %rax
movl 0x10(%rax), %r9d
je 0x518c
leaq 0x2bff(%rip), %rsi      # 0x79a1
jmp 0x5193
movq 0x53f2(%rip), %rcx      # 0xa1a0
movl $0xa, %esi
movl $0x1, %edx
leaq 0x2d6c(%rip), %rdi      # 0x7b2b
callq 0x11d0
jmp 0x51a0
movl 0x53f4(%rip), %r15d     # 0xa1c4
leal 0x1(%r15), %r14d
leal 0x2(%r15), %eax
movl %eax, -0x2c(%rbp)
leal 0x3(%r15), %eax
movl %eax, 0x53df(%rip)      # 0xa1c4
movq 0x53b4(%rip), %rdi      # 0xa1a0
leaq 0x3152(%rip), %rsi      # 0x7f45
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x539c(%rip), %rdi      # 0xa1a0
leaq 0x3134(%rip), %rsi      # 0x7f3f
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x8(%rbx), %rdi
callq 0x51bc
movl 0x538b(%rip), %r12d     # 0xa1b0
leal 0x1(%r12), %ecx
movl %ecx, 0x5380(%rip)      # 0xa1b0
movq 0x5369(%rip), %rdi      # 0xa1a0
leaq 0x2ddd(%rip), %rsi      # 0x7c1b
movl %r12d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movq 0x534f(%rip), %rdi      # 0xa1a0
leaq 0x30bf(%rip), %rsi      # 0x7f17
movl %r12d, %edx
movl %r14d, %ecx
movl -0x2c(%rbp), %r12d
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1110
movq 0x532d(%rip), %rdi      # 0xa1a0
leaq 0x30c5(%rip), %rsi      # 0x7f3f
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x10(%rbx), %rdi
callq 0x4c8c
movq 0x530c(%rip), %rdi      # 0xa1a0
leaq 0x30aa(%rip), %rsi      # 0x7f45
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x52f4(%rip), %rdi      # 0xa1a0
leaq 0x308c(%rip), %rsi      # 0x7f3f
jmp 0x4fc3
movq 0x8(%rbx), %rdi
callq 0x51bc
movq 0x8(%rbx), %rcx
movq 0x8(%rcx), %rcx
movl (%rcx), %edx
testl %edx, %edx
je 0x5103
cmpl $0x1, %edx
jne 0x51a0
cmpl $0x1, 0x4(%rcx)
jne 0x51a0
movl 0x52c4(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %eax
movl %eax, 0x52bb(%rip)      # 0xa1b0
movq 0x52a4(%rip), %rdi      # 0xa1a0
movq 0x52b5(%rip), %rcx      # 0xa1b8
leaq 0x2c15(%rip), %rsi      # 0x7b1f
xorl %eax, %eax
callq 0x1110
jmp 0x51a0
movq 0x8(%rbx), %rdi
callq 0x51bc
movl 0x528a(%rip), %r14d     # 0xa1b0
leal 0x1(%r14), %ecx
movl %ecx, 0x5280(%rip)      # 0xa1b0
movq 0x5269(%rip), %rdi      # 0xa1a0
leaq 0x2cdd(%rip), %rsi      # 0x7c1b
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x5273(%rip), %r15d     # 0xa1c4
leal 0x1(%r15), %r12d
leal 0x2(%r15), %eax
movl %eax, 0x5265(%rip)      # 0xa1c4
movq 0x523a(%rip), %rdi      # 0xa1a0
leaq 0x2faa(%rip), %rsi      # 0x7f17
movl %r14d, %edx
movl %r15d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1110
movq 0x521c(%rip), %rdi      # 0xa1a0
leaq 0x2fb4(%rip), %r14      # 0x7f3f
movq %r14, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x10(%rbx), %rdi
callq 0x4c8c
movq 0x51f8(%rip), %rdi      # 0xa1a0
leaq 0x2f96(%rip), %rsi      # 0x7f45
movl %r12d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x51e0(%rip), %rdi      # 0xa1a0
movq %r14, %rsi
movl %r12d, %edx
jmp 0x50bb
movq 0x8(%rbx), %rdi
callq 0x51bc
movl 0x51d5(%rip), %r15d     # 0xa1b0
leal 0x1(%r15), %ecx
movl %ecx, 0x51cb(%rip)      # 0xa1b0
movq 0x51b4(%rip), %rdi      # 0xa1a0
leaq 0x2c28(%rip), %rsi      # 0x7c1b
movl %r15d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x51be(%rip), %r12d     # 0xa1c4
leal 0x1(%r12), %r8d
movl %r8d, -0x2c(%rbp)
leal 0x2(%r12), %r14d
leal 0x3(%r12), %eax
movl %eax, 0x51a5(%rip)      # 0xa1c4
movq 0x517a(%rip), %rdi      # 0xa1a0
leaq 0x2eea(%rip), %rsi      # 0x7f17
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1110
movq 0x515f(%rip), %rdi      # 0xa1a0
leaq 0x2ef7(%rip), %rsi      # 0x7f3f
movl %r12d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x10(%rbx), %rdi
callq 0x4c8c
movq 0x513e(%rip), %rdi      # 0xa1a0
leaq 0x2edc(%rip), %r15      # 0x7f45
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x5123(%rip), %rdi      # 0xa1a0
leaq 0x2ebb(%rip), %r12      # 0x7f3f
movq %r12, %rsi
movl -0x2c(%rbp), %edx
xorl %eax, %eax
callq 0x1110
movq 0x18(%rbx), %rdi
callq 0x4c8c
movq 0x50ff(%rip), %rdi      # 0xa1a0
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x50eb(%rip), %rdi      # 0xa1a0
movq %r12, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
jmp 0x51a0
movq 0x8(%rbx), %rdi
callq 0x51bc
movl %eax, %r15d
movq 0x50c6(%rip), %r14      # 0xa1a0
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rdi
callq 0x4809
movq %r14, %rdi
leaq 0x2a45(%rip), %rsi      # 0x7b36
movq %rax, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1110
jmp 0x51a0
cmpl $0x2, 0x4(%rcx)
jne 0x5131
movl 0x50a1(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %ecx
movl %ecx, 0x5098(%rip)      # 0xa1b0
movq 0x5081(%rip), %rdi      # 0xa1a0
leaq 0x28d6(%rip), %rsi      # 0x79fc
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
jmp 0x51a0
testl %edx, %edx
jne 0x4ed3
cmpl $0x3, 0x4(%rcx)
jne 0x4ed3
movl 0x5067(%rip), %ebx      # 0xa1b0
leal 0x1(%rbx), %ecx
movl %ecx, 0x505e(%rip)      # 0xa1b0
movq 0x5047(%rip), %rdi      # 0xa1a0
leaq 0x2918(%rip), %rsi      # 0x7a78
movl %ebx, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x503f(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %eax
movl %eax, 0x5036(%rip)      # 0xa1b0
movq 0x501f(%rip), %rdi      # 0xa1a0
leaq 0x2916(%rip), %rsi      # 0x7a9e
movl %ebx, %ecx
jmp 0x5128
leaq 0x284e(%rip), %rsi      # 0x79e1
movq %r15, %rdx
movq %r15, %r8
xorl %eax, %eax
callq 0x1110
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x4cfc
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq