// File: /workspace/github_c_makefile_star2_O0/ryvnf[P]czc/main.c
// Function: invoke_gcc
void invoke_gcc(struct arg_list *src_files, struct arg_list *gcc_args,
        struct arg_list **gcc_args_tailp)
{
    // The beginning of the temporary file arguments passed to GCC (used to
    // remove them after compilation).
    struct arg_list **tmp_files = gcc_args_tailp;

    char tmp_dir[] = "/tmp/czc.XXXXXX";
    if (mkdtemp(tmp_dir) == NULL)
        perror("mkdtemp");

    for (struct arg_list *i = src_files; i != NULL; i = i->next) {
        // TODO: This does will not work if compiling dir0/a.zc and dir1/a.zc in
        //       the same command.  A better solution should be implemented in
        //       the future.
        char *arg = basename(strdup(i->arg));
        char *arg_ext = get_file_ext(arg);
        if (arg_ext != NULL)
            arg_ext[-1] = '\0';

        size_t n = strlen(tmp_dir) + 1 + strlen(arg) + 2;
        char c_file_name[n + 1];
        snprintf(c_file_name, sizeof c_file_name, "%s/%s.c", tmp_dir, arg);

        FILE *fp;
        if ((fp = fopen(c_file_name, "w")) == NULL)
            perror(c_file_name);

        gen_input_file(i->arg, fp);

        *gcc_args_tailp = arg_list_new(c_file_name);
        gcc_args_tailp = &(*gcc_args_tailp)->next;

        fclose(fp);
    }

    // Count number of arguments.
    size_t gcc_argc = 0;
    for (struct arg_list *i = gcc_args; i != NULL; i = i->next)
        gcc_argc++;

    char **gcc_argv = malloc((gcc_argc + 1) * sizeof *gcc_argv);

    // Initialize the arguments.
    struct arg_list *i = gcc_args;
    for (size_t j = 0; j < gcc_argc; ++j) {
        gcc_argv[j] = i->arg;
        i = i->next;
    }
    gcc_argv[gcc_argc] = NULL;

    // Execute GCC.
    pid_t pid = fork();
    if (pid < 0) {
        perror("fork");
        exit(1);
    } else if (pid == 0) {
        if (execvp("gcc", gcc_argv) < 0) {
            perror("execvp");
            exit(1);
        }
    }

    waitpid(pid, NULL, 0);

    // Remove the created temporary files.
    for (struct arg_list *i = *tmp_files; i != NULL; i = i->next)
        unlink(i->arg);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movq 0x7d4a(%rip), %rax      # 0x14000
movq %rax, -0x30(%rbp)
movq 0x7d47(%rip), %rax      # 0x14008
movq %rax, -0x28(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x21a0
cmpq $0x0, %rax
jne 0xc2e0
leaq 0x7c65(%rip), %rdi      # 0x13f40
callq 0x21d0
movq -0x8(%rbp), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0xc415
movq -0x38(%rbp), %rax
movq (%rax), %rdi
callq 0x2210
movq %rax, %rdi
callq 0x2150
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
callq 0xc010
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0xc327
movq -0x48(%rbp), %rax
movb $0x0, -0x1(%rax)
leaq -0x30(%rbp), %rdi
callq 0x20a0
movq %rax, -0xb0(%rbp)
movq -0x40(%rbp), %rdi
callq 0x20a0
movq %rax, %rcx
movq -0xb0(%rbp), %rax
leaq 0x3(%rax,%rcx), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, %rsi
incq %rsi
movq %rsp, %rcx
movq %rcx, -0x58(%rbp)
addq $0x10, %rax
andq $-0x10, %rax
movq %rsp, %rdi
subq %rax, %rdi
movq %rdi, -0xa8(%rbp)
movq %rdi, %rsp
movq %rsi, -0x60(%rbp)
leaq -0x30(%rbp), %rcx
movq -0x40(%rbp), %r8
leaq 0x7bb9(%rip), %rdx      # 0x13f48
movb $0x0, %al
callq 0x20c0
movq -0xa8(%rbp), %rdi
leaq 0x7bd4(%rip), %rsi      # 0x13f78
callq 0x21c0
movq %rax, -0x68(%rbp)
cmpq $0x0, %rax
jne 0xc3bf
movq -0xa8(%rbp), %rdi
callq 0x21d0
movq -0x38(%rbp), %rax
movq (%rax), %rdi
movq -0x68(%rbp), %rsi
callq 0xc0f0
movq -0xa8(%rbp), %rdi
callq 0xc0a0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rax
addq $0x8, %rax
movq %rax, -0x18(%rbp)
movq -0x68(%rbp), %rdi
callq 0x2090
movq -0x58(%rbp), %rax
movq %rax, %rsp
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0xc2e8
movq $0x0, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
je 0xc446
movq -0x70(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x78(%rbp)
jmp 0xc425
movq -0x70(%rbp), %rdi
addq $0x1, %rdi
shlq $0x3, %rdi
callq 0x2170
movq %rax, -0x80(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x70(%rbp), %rax
jae 0xc4bd
movq -0x88(%rbp), %rax
movq (%rax), %rdx
movq -0x80(%rbp), %rax
movq -0x90(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x88(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0xc471
movq -0x80(%rbp), %rax
movq -0x70(%rbp), %rcx
movq $0x0, (%rax,%rcx,8)
callq 0x2240
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x94(%rbp)
jge 0xc4f7
leaq 0x7a68(%rip), %rdi      # 0x13f50
callq 0x21d0
movl $0x1, %edi
callq 0x2200
cmpl $0x0, -0x94(%rbp)
jne 0xc52d
movq -0x80(%rbp), %rsi
leaq 0x7a4a(%rip), %rdi      # 0x13f55
callq 0x21f0
cmpl $0x0, %eax
jge 0xc52b
leaq 0x7a3d(%rip), %rdi      # 0x13f59
callq 0x21d0
movl $0x1, %edi
callq 0x2200
jmp 0xc52d
jmp 0xc52f
movl -0x94(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x21b0
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
je 0xc57b
movq -0xa0(%rbp), %rax
movq (%rax), %rdi
callq 0x2060
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xa0(%rbp)
jmp 0xc54e
movq %rbp, %rsp
popq %rbp
retq