// File: /workspace/github_c_makefile_star2_O0/thiagok00[P]INF1715-Compilers/GeraCodigo.c
// Function: gera_codigo_funcoes
static void gera_codigo_funcoes(Definicao *defs) {
  while (defs != NULL){
    if (defs->tag == DFunc){
        DefFunc *f = defs->u.f;
        ParametroL *params = defs->u.f->params, *paramsAux;

        char paramList[400] = "";

        paramsAux = params;
        while(paramsAux != NULL) {
          int idtemp = gen_id();
          char *strtipo = tipo_string(paramsAux->tipo);
          paramsAux->idtemp = idtemp;
          if(paramsAux->prox == NULL)
            sprintf(paramList,"%s%s %%t%d",paramList,strtipo,idtemp);
          else
            sprintf(paramList,"%s%s %%t%d, ",paramList,strtipo,idtemp);
          paramsAux = paramsAux->prox;
        }

        char* tiporetstr = tipo_string(f->tiporet);
        fprintf(output,"define %s @%s(%s) {\n",tiporetstr,f->id,paramList);

        while (params != NULL) {
          //TODO ARRAYS
          int lastid;
          int idtemp = gen_id();
          char *strtipo = tipo_string(params->tipo);

          lastid = params->idtemp;
          params->idtemp = idtemp;

          fprintf(output,"\t%%t%d = alloca %s\n",idtemp,strtipo);
          fprintf(output,"\tstore %s %%t%d, %s* %%t%d\n",strtipo,lastid,strtipo,idtemp);
          params = params->prox;
        }

        gera_codigo_bloco(f->bloco);
        fprintf(output, "\t}\n");
    }
    defs = defs->prox;
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
cmpq $0x0, -0x8(%rbp)
je 0x779d
movq -0x8(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x778c
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq %rax, -0x18(%rbp)
leaq -0x1b0(%rbp), %rdi
xorl %esi, %esi
movl $0x190, %edx            # imm = 0x190
callq 0x10c0
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x7686
callq 0x77b0
movl %eax, -0x1b4(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x7360
movq %rax, -0x1c0(%rbp)
movl -0x1b4(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x764b
leaq -0x1b0(%rbp), %rdi
leaq -0x1b0(%rbp), %rdx
movq -0x1c0(%rbp), %rcx
movl -0x1b4(%rbp), %r8d
leaq 0x3456(%rip), %rsi      # 0xaa98
movb $0x0, %al
callq 0x1180
jmp 0x7675
leaq -0x1b0(%rbp), %rdi
leaq -0x1b0(%rbp), %rdx
movq -0x1c0(%rbp), %rcx
movl -0x1b4(%rbp), %r8d
leaq 0x3435(%rip), %rsi      # 0xaaa3
movb $0x0, %al
callq 0x1180
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x75de
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x7360
movq %rax, -0x1c8(%rbp)
movq 0x6ae8(%rip), %rdi      # 0xe188
movq -0x1c8(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
leaq -0x1b0(%rbp), %r8
leaq 0x33f3(%rip), %rsi      # 0xaab0
movb $0x0, %al
callq 0x10e0
cmpq $0x0, -0x18(%rbp)
je 0x776a
callq 0x77b0
movl %eax, -0x1d0(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x7360
movq %rax, -0x1d8(%rbp)
movq -0x18(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x1cc(%rbp)
movl -0x1d0(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x10(%rax)
movq 0x6a7a(%rip), %rdi      # 0xe188
movl -0x1d0(%rbp), %edx
movq -0x1d8(%rbp), %rcx
leaq 0x33a3(%rip), %rsi      # 0xaac5
movb $0x0, %al
callq 0x10e0
movq 0x6a58(%rip), %rdi      # 0xe188
movq -0x1d8(%rbp), %rdx
movl -0x1cc(%rbp), %ecx
movq -0x1d8(%rbp), %r8
movl -0x1d0(%rbp), %r9d
leaq 0x3387(%rip), %rsi      # 0xaad9
movb $0x0, %al
callq 0x10e0
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0x76c4
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x77d0
movq 0x6a0a(%rip), %rdi      # 0xe188
leaq 0x3370(%rip), %rsi      # 0xaaf5
movb $0x0, %al
callq 0x10e0
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x8(%rbp)
jmp 0x758f
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
nopw %cs:(%rax,%rax)