// File: /workspace/github_c_makefile_star2_O0/MrOneTwo[P]tinycc-dynamic-compilation/main.c
// Function: compile_program
static int
compile_program(script_t* script)
{
  TCCState* tcc = tcc_new();
  if (!tcc)
  {
    printf("[TCC:ERR] Failed to create tcc context!\n");
    return -1;
  }

  tcc_set_lib_path(tcc, "./tcc_local");
  tcc_add_include_path(tcc, "./tcc_local");

  tcc_set_error_func(tcc, 0x0, tcc_error);
  tcc_set_options(tcc, "-g");
  tcc_set_output_type(tcc, TCC_OUTPUT_MEMORY);

  int ret = tcc_add_file(tcc, script->path);
  if (ret < 0)
  {
    printf("[TCC:ERR] Failed to add tcc file!\n");
    tcc_delete(tcc);
    return -1;
  }

  // tcc_relocate called with NULL returns the size that's necessary for the added files.
  script->program = calloc(1, tcc_relocate(tcc, NULL));
  if (!script->program)
  {
    printf("[TCC:ERR] Failed to allocate memory for the program!\n");
    tcc_delete(tcc);
    return -1;
  }

  // Copy code to memory passed by the caller. This is where the compilation happens (I think...).
  ret = tcc_relocate(tcc, script->program);
  if (ret < 0)
  {
    printf("[TCC:ERR] Failed to allocate memory for the program!\n");
    tcc_delete(tcc);
    return -1;
  }

  script->hello = (script_hello)tcc_get_symbol(tcc, "target_hello");
  script->bye = (script_bye)tcc_get_symbol(tcc, "target_bye");
  
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
callq 0x3d00
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x2666
leaq 0x2f9b1(%rip), %rdi     # 0x32004
movb $0x0, %al
callq 0x2160
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27bb
movq -0x18(%rbp), %rdi
leaq 0x2f9bc(%rip), %rsi     # 0x3202d
callq 0x46d0
movq -0x18(%rbp), %rdi
leaq 0x2f9ac(%rip), %rsi     # 0x3202d
callq 0x3eb0
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
leaq 0x13b(%rip), %rdx       # 0x27d0
callq 0x3170
movq -0x18(%rbp), %rdi
leaq 0x2f994(%rip), %rsi     # 0x32039
callq 0x47f0
movq -0x18(%rbp), %rdi
movl $0x1, %esi
callq 0x43c0
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x4240
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x26f4
leaq 0x2f964(%rip), %rdi     # 0x3203c
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x3d90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27bb
movq -0x18(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x297d0
movslq %eax, %rsi
movl $0x1, %edi
callq 0x2260
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x2744
leaq 0x2f934(%rip), %rdi     # 0x3205f
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x3d90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27bb
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x297d0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x277e
leaq 0x2f8fa(%rip), %rdi     # 0x3205f
movb $0x0, %al
callq 0x2160
movq -0x18(%rbp), %rdi
callq 0x3d90
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27bb
movq -0x18(%rbp), %rdi
leaq 0x2f90c(%rip), %rsi     # 0x32095
callq 0x1fc90
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rdi
leaq 0x2f8fe(%rip), %rsi     # 0x320a2
callq 0x1fc90
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)