// File: /workspace/github_c_makefile_star2_O2/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, %rbx
movq 0x8(%rdi), %r13
leaq 0x3407(%rip), %r14      # 0x7985
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x45c7
movq (%rbx), %rax
movl 0x5c20(%rip), %r15d     # 0xa1b0
leal 0x1(%r15), %ecx
movl %ecx, 0x5c16(%rip)      # 0xa1b0
movl %r15d, 0x10(%rax)
movq 0x5bfb(%rip), %r12      # 0xa1a0
movq 0x8(%rax), %rdi
callq 0x4145
movq %r12, %rdi
movq %r14, %rsi
movl %r15d, %edx
movq %rax, %rcx
xorl %eax, %eax
callq 0x1110
addq $0x8, %rbx
jmp 0x457e
leaq 0x32da(%rip), %r12      # 0x78a8
movq %r13, %r14
testq %r14, %r14
je 0x4a8b
movq (%r14), %r13
movl (%r13), %eax
cmpq $0x9, %rax
ja 0x4a82
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq 0x8(%r13), %r15
cmpl $0x0, (%r15)
jne 0x4a82
movq 0x8(%r15), %rdi
callq 0x4145
movq %rax, %rbx
movq 0x10(%r13), %rdi
callq 0x4a9a
movl %eax, %ecx
movq 0x8(%r15), %rax
cmpl $0x1, 0x4(%rax)
jne 0x4661
cmpl $0x0, (%rax)
jne 0x4661
movq %r14, %r12
movl 0x5b7e(%rip), %r14d     # 0xa1b0
leal 0x1(%r14), %eax
movl %eax, 0x5b74(%rip)      # 0xa1b0
movq 0x5b5d(%rip), %rdi      # 0xa1a0
leaq 0x336f(%rip), %rsi      # 0x79b9
movl %r14d, %edx
xorl %eax, %eax
callq 0x1110
movl %r14d, %ecx
movq %r12, %r14
leaq 0x3247(%rip), %r12      # 0x78a8
cmpl $0x0, 0x18(%r15)
movq 0x5b33(%rip), %rdi      # 0xa1a0
movq 0x20(%r15), %rax
movl 0x10(%rax), %r9d
je 0x4a1e
leaq 0x3317(%rip), %rsi      # 0x7999
jmp 0x4a25
movq 0x5b12(%rip), %rcx      # 0xa1a0
leaq 0x348e(%rip), %rdi      # 0x7b23
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x11d0
jmp 0x4a82
movl 0x5b19(%rip), %ebx      # 0xa1c4
movq %r14, -0x30(%rbp)
leal 0x1(%rbx), %r14d
leal 0x2(%rbx), %r15d
leal 0x3(%rbx), %eax
movl %eax, 0x5b04(%rip)      # 0xa1c4
movq 0x5ad9(%rip), %rdi      # 0xa1a0
leaq 0x386f(%rip), %rsi      # 0x7f3d
movl %ebx, %edx
xorl %eax, %eax
callq 0x1110
movq 0x5ac2(%rip), %rdi      # 0xa1a0
leaq 0x3852(%rip), %rsi      # 0x7f37
movl %ebx, %edx
xorl %eax, %eax
callq 0x1110
movq 0x8(%r13), %rdi
callq 0x4a9a
movl 0x5ab2(%rip), %r12d     # 0xa1b0
leal 0x1(%r12), %ecx
movl %ecx, 0x5aa7(%rip)      # 0xa1b0
movq 0x5a90(%rip), %rdi      # 0xa1a0
leaq 0x34fc(%rip), %rsi      # 0x7c13
movl %r12d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movq 0x5a76(%rip), %rdi      # 0xa1a0
leaq 0x37de(%rip), %rsi      # 0x7f0f
movl %r12d, %edx
leaq 0x316d(%rip), %r12      # 0x78a8
movl %r14d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x1110
movq 0x5a51(%rip), %rdi      # 0xa1a0
leaq 0x37e1(%rip), %rsi      # 0x7f37
movl %r14d, %edx
movq -0x30(%rbp), %r14
xorl %eax, %eax
callq 0x1110
movq 0x10(%r13), %rdi
callq 0x4562
movq 0x5a2c(%rip), %rdi      # 0xa1a0
leaq 0x37c2(%rip), %rsi      # 0x7f3d
movl %ebx, %edx
xorl %eax, %eax
callq 0x1110
movq 0x5a15(%rip), %rdi      # 0xa1a0
leaq 0x37a5(%rip), %rsi      # 0x7f37
movl %r15d, %edx
jmp 0x499b
movq 0x8(%r13), %rdi
callq 0x4a9a
movq 0x8(%r13), %rcx
movq 0x8(%rcx), %rcx
movl (%rcx), %edx
cmpl $0x1, %edx
je 0x49e7
testl %edx, %edx
jne 0x4a82
movl 0x4(%rcx), %ecx
cmpl $0x3, %ecx
je 0x4a34
cmpl $0x2, %ecx
jne 0x4a82
movl 0x59d7(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %ecx
movl %ecx, 0x59ce(%rip)      # 0xa1b0
movq 0x59b7(%rip), %rdi      # 0xa1a0
leaq 0x3204(%rip), %rsi      # 0x79f4
movl %eax, %ecx
jmp 0x4a7b
movq 0x8(%r13), %rdi
callq 0x4a9a
movq %r14, %r12
movl 0x59a6(%rip), %r14d     # 0xa1b0
leal 0x1(%r14), %ecx
movl %ecx, 0x599c(%rip)      # 0xa1b0
movq 0x5985(%rip), %rdi      # 0xa1a0
leaq 0x33f1(%rip), %rsi      # 0x7c13
movl %r14d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x598f(%rip), %r15d     # 0xa1c4
leal 0x1(%r15), %ebx
leal 0x2(%r15), %eax
movl %eax, 0x5981(%rip)      # 0xa1c4
movq 0x5956(%rip), %rdi      # 0xa1a0
leaq 0x36be(%rip), %rsi      # 0x7f0f
movl %r14d, %edx
movq %r12, %r14
leaq 0x304a(%rip), %r12      # 0x78a8
movl %r15d, %ecx
movl %ebx, %r8d
xorl %eax, %eax
callq 0x1110
movq 0x592e(%rip), %rdi      # 0xa1a0
leaq 0x36be(%rip), %rsi      # 0x7f37
movl %r15d, %edx
xorl %eax, %eax
callq 0x1110
movq 0x10(%r13), %rdi
callq 0x4562
movq 0x590d(%rip), %rdi      # 0xa1a0
leaq 0x36a3(%rip), %rsi      # 0x7f3d
jmp 0x4982
movq 0x8(%r13), %rdi
callq 0x4a9a
movl 0x5901(%rip), %r15d     # 0xa1b0
leal 0x1(%r15), %ecx
movl %ecx, 0x58f7(%rip)      # 0xa1b0
movq 0x58e0(%rip), %rdi      # 0xa1a0
leaq 0x334c(%rip), %rsi      # 0x7c13
movl %r15d, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x58ea(%rip), %r12d     # 0xa1c4
movq %r14, -0x30(%rbp)
leal 0x1(%r12), %r14d
leal 0x2(%r12), %ebx
leal 0x3(%r12), %eax
movl %eax, 0x58d1(%rip)      # 0xa1c4
movq 0x58a6(%rip), %rdi      # 0xa1a0
leaq 0x360e(%rip), %rsi      # 0x7f0f
movl %r15d, %edx
movl %r12d, %ecx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1110
movq 0x5888(%rip), %rdi      # 0xa1a0
leaq 0x3618(%rip), %rsi      # 0x7f37
movl %r12d, %edx
leaq 0x2f7f(%rip), %r12      # 0x78a8
xorl %eax, %eax
callq 0x1110
movq 0x10(%r13), %rdi
callq 0x4562
movq 0x5860(%rip), %rdi      # 0xa1a0
leaq 0x35f6(%rip), %r15      # 0x7f3d
movq %r15, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1110
movq 0x5846(%rip), %rdi      # 0xa1a0
leaq 0x35d6(%rip), %rsi      # 0x7f37
movl %r14d, %edx
movq -0x30(%rbp), %r14
xorl %eax, %eax
callq 0x1110
movq 0x18(%r13), %rdi
callq 0x4562
movq 0x5821(%rip), %rdi      # 0xa1a0
movq %r15, %rsi
movl %ebx, %edx
xorl %eax, %eax
callq 0x1110
movq 0x580e(%rip), %rdi      # 0xa1a0
leaq 0x359e(%rip), %rsi      # 0x7f37
movl %ebx, %edx
xorl %eax, %eax
callq 0x1110
jmp 0x4a82
movq 0x8(%r13), %rdi
callq 0x4a9a
movl %eax, %ebx
movq %r14, %r15
movq 0x57e4(%rip), %r14      # 0xa1a0
movq 0x8(%r13), %rax
movq 0x8(%rax), %rdi
callq 0x4145
movq %r14, %rdi
movq %r15, %r14
leaq 0x3158(%rip), %rsi      # 0x7b2e
movq %rax, %rdx
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1110
jmp 0x4a82
cmpl $0x1, 0x4(%rcx)
jne 0x4a82
movl 0x57b9(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %eax
movl %eax, 0x57b0(%rip)      # 0xa1b0
movq 0x5799(%rip), %rdi      # 0xa1a0
movq 0x57aa(%rip), %rcx      # 0xa1b8
leaq 0x3102(%rip), %rsi      # 0x7b17
xorl %eax, %eax
callq 0x1110
jmp 0x4a82
leaq 0x2fb4(%rip), %rsi      # 0x79d9
movq %rbx, %rdx
movq %rbx, %r8
xorl %eax, %eax
callq 0x1110
jmp 0x4a82
movl 0x5776(%rip), %ebx      # 0xa1b0
leal 0x1(%rbx), %ecx
movl %ecx, 0x576d(%rip)      # 0xa1b0
movq 0x5756(%rip), %rdi      # 0xa1a0
leaq 0x301f(%rip), %rsi      # 0x7a70
movl %ebx, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1110
movl 0x574e(%rip), %edx      # 0xa1b0
leal 0x1(%rdx), %eax
movl %eax, 0x5745(%rip)      # 0xa1b0
movq 0x572e(%rip), %rdi      # 0xa1a0
leaq 0x301d(%rip), %rsi      # 0x7a96
movl %ebx, %ecx
xorl %eax, %eax
callq 0x1110
movq 0x8(%r14), %r14
jmp 0x45d1
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq