// File: /workspace/github_c_makefile_star2_O3/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 0x4f38
leaq 0x2f3d(%rip), %rbx      # 0x7985
movq (%r12), %rax
movl 0x575d(%rip), %r14d     # 0xa1b0
leal 0x1(%r14), %ecx
movl %ecx, 0x5753(%rip)      # 0xa1b0
movl %r14d, 0x10(%rax)
movq 0x5738(%rip), %r15      # 0xa1a0
movq 0x8(%rax), %rdi
callq 0x45a9
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 0x4a48
jmp 0x4f38
movq (%r13), %rbx
movl (%rbx), %eax
cmpq $0x9, %rax
ja 0x4f34
leaq 0x2dfe(%rip), %rcx      # 0x78a8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x8(%rbx), %r12
cmpl $0x0, (%r12)
jne 0x4f34
movq 0x8(%r12), %rdi
callq 0x45a9
movq %rax, %r15
movq 0x10(%rbx), %rdi
callq 0x4f50
movl %eax, %ecx
movq 0x8(%r12), %rax
cmpl $0x1, 0x4(%rax)
jne 0x4b16
cmpl $0x0, (%rax)
jne 0x4b16
movl 0x56bf(%rip), %r14d     # 0xa1b0
leal 0x1(%r14), %eax
movl %eax, 0x56b5(%rip)      # 0xa1b0
movq 0x569e(%rip), %rdi      # 0xa1a0
leaq 0x2eb0(%rip), %rsi      # 0x79b9
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movl %r14d, %ecx
cmpl $0x0, 0x18(%r12)
movq 0x567d(%rip), %rdi      # 0xa1a0
movq 0x20(%r12), %rax
movl 0x10(%rax), %r9d
je 0x4ed0
leaq 0x2e60(%rip), %rsi      # 0x7999
jmp 0x4ed7
movq 0x565b(%rip), %rcx      # 0xa1a0
movl $0xa, %esi
movl $0x1, %edx
leaq 0x2fcd(%rip), %rdi      # 0x7b23
callq 0x11d0
jmp 0x4f34
movl 0x565d(%rip), %r15d     # 0xa1c4
leal 0x1(%r15), %r14d
leal 0x2(%r15), %eax
movl %eax, -0x2c(%rbp)
leal 0x3(%r15), %eax
movl %eax, 0x5648(%rip)      # 0xa1c4
movq 0x561d(%rip), %rdi      # 0xa1a0
leaq 0x33b3(%rip), %rsi      # 0x7f3d
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x5605(%rip), %rdi      # 0xa1a0
leaq 0x3395(%rip), %rsi      # 0x7f37
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x8(%rbx), %rdi
callq 0x4f50
movl 0x55f4(%rip), %r12d     # 0xa1b0
leal 0x1(%r12), %ecx
movl %ecx, 0x55e9(%rip)      # 0xa1b0
movq 0x55d2(%rip), %rdi      # 0xa1a0
leaq 0x303e(%rip), %rsi      # 0x7c13
movl %r12d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movq 0x55b8(%rip), %rdi      # 0xa1a0
leaq 0x3320(%rip), %rsi      # 0x7f0f
movl %r12d, %edx
movl %r14d, %ecx
movl -0x2c(%rbp), %r12d
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1110
movq 0x5596(%rip), %rdi      # 0xa1a0
leaq 0x3326(%rip), %rsi      # 0x7f37
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x10(%rbx), %rdi
callq 0x4a23
movq 0x5575(%rip), %rdi      # 0xa1a0
leaq 0x330b(%rip), %rsi      # 0x7f3d
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x555d(%rip), %rdi      # 0xa1a0
leaq 0x32ed(%rip), %rsi      # 0x7f37
jmp 0x4d59
movq 0x8(%rbx), %rdi
callq 0x4f50
movq 0x8(%rbx), %rcx
movq 0x8(%rcx), %rcx
movl (%rcx), %edx
cmpl $0x1, %edx
je 0x4e99
testl %edx, %edx
jne 0x4f34
movl 0x4(%rcx), %ecx
cmpl $0x3, %ecx
je 0x4ee6
cmpl $0x2, %ecx
jne 0x4f34
movl 0x5522(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %ecx
movl %ecx, 0x5519(%rip)      # 0xa1b0
movq 0x5502(%rip), %rdi      # 0xa1a0
leaq 0x2d4f(%rip), %rsi      # 0x79f4
movl %eax, %ecx
jmp 0x4f2d
movq 0x8(%rbx), %rdi
callq 0x4f50
movl 0x54f4(%rip), %r14d     # 0xa1b0
leal 0x1(%r14), %ecx
movl %ecx, 0x54ea(%rip)      # 0xa1b0
movq 0x54d3(%rip), %rdi      # 0xa1a0
leaq 0x2f3f(%rip), %rsi      # 0x7c13
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x54dd(%rip), %r15d     # 0xa1c4
leal 0x1(%r15), %r12d
leal 0x2(%r15), %eax
movl %eax, 0x54cf(%rip)      # 0xa1c4
movq 0x54a4(%rip), %rdi      # 0xa1a0
leaq 0x320c(%rip), %rsi      # 0x7f0f
movl %r14d, %edx
movl %r15d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x1110
movq 0x5486(%rip), %rdi      # 0xa1a0
leaq 0x3216(%rip), %r14      # 0x7f37
movq %r14, %rsi
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x10(%rbx), %rdi
callq 0x4a23
movq 0x5462(%rip), %rdi      # 0xa1a0
leaq 0x31f8(%rip), %rsi      # 0x7f3d
movl %r12d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x544a(%rip), %rdi      # 0xa1a0
movq %r14, %rsi
movl %r12d, %edx
jmp 0x4e51
movq 0x8(%rbx), %rdi
callq 0x4f50
movl 0x543f(%rip), %r15d     # 0xa1b0
leal 0x1(%r15), %ecx
movl %ecx, 0x5435(%rip)      # 0xa1b0
movq 0x541e(%rip), %rdi      # 0xa1a0
leaq 0x2e8a(%rip), %rsi      # 0x7c13
movl %r15d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x5428(%rip), %r12d     # 0xa1c4
leal 0x1(%r12), %r8d
movl %r8d, -0x2c(%rbp)
leal 0x2(%r12), %r14d
leal 0x3(%r12), %eax
movl %eax, 0x540f(%rip)      # 0xa1c4
movq 0x53e4(%rip), %rdi      # 0xa1a0
leaq 0x314c(%rip), %rsi      # 0x7f0f
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1110
movq 0x53c9(%rip), %rdi      # 0xa1a0
leaq 0x3159(%rip), %rsi      # 0x7f37
movl %r12d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x10(%rbx), %rdi
callq 0x4a23
movq 0x53a8(%rip), %rdi      # 0xa1a0
leaq 0x313e(%rip), %r15      # 0x7f3d
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x538d(%rip), %rdi      # 0xa1a0
leaq 0x311d(%rip), %r12      # 0x7f37
movq %r12, %rsi
movl -0x2c(%rbp), %edx
xorl %eax, %eax
callq 0x1110
movq 0x18(%rbx), %rdi
callq 0x4a23
movq 0x5369(%rip), %rdi      # 0xa1a0
movq %r15, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x5355(%rip), %rdi      # 0xa1a0
movq %r12, %rsi
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
jmp 0x4f34
movq 0x8(%rbx), %rdi
callq 0x4f50
movl %eax, %r15d
movq 0x5330(%rip), %r14      # 0xa1a0
movq 0x8(%rbx), %rax
movq 0x8(%rax), %rdi
callq 0x45a9
movq %r14, %rdi
leaq 0x2ca7(%rip), %rsi      # 0x7b2e
movq %rax, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1110
jmp 0x4f34
cmpl $0x1, 0x4(%rcx)
jne 0x4f34
movl 0x5307(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %eax
movl %eax, 0x52fe(%rip)      # 0xa1b0
movq 0x52e7(%rip), %rdi      # 0xa1a0
movq 0x52f8(%rip), %rcx      # 0xa1b8
leaq 0x2c50(%rip), %rsi      # 0x7b17
xorl %eax, %eax
callq 0x1110
jmp 0x4f34
leaq 0x2b02(%rip), %rsi      # 0x79d9
movq %r15, %rdx
movq %r15, %r8
xorl %eax, %eax
callq 0x1110
jmp 0x4f34
movl 0x52c4(%rip), %ebx      # 0xa1b0
leal 0x1(%rbx), %ecx
movl %ecx, 0x52bb(%rip)      # 0xa1b0
movq 0x52a4(%rip), %rdi      # 0xa1a0
leaq 0x2b6d(%rip), %rsi      # 0x7a70
movl %ebx, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x529c(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %eax
movl %eax, 0x5293(%rip)      # 0xa1b0
movq 0x527c(%rip), %rdi      # 0xa1a0
leaq 0x2b6b(%rip), %rsi      # 0x7a96
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1110
movq 0x8(%r13), %r13
testq %r13, %r13
jne 0x4a93
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq