// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, -0x30(%rbp)
movq %rsi, %r14
movq %rdi, %r15
movaps 0x5666(%rip), %xmm0     # 0xe650
leaq -0x50(%rbp), %rdi
movaps %xmm0, (%rdi)
callq 0x21a0
testq %rax, %rax
je 0x91a2
testq %r15, %r15
je 0x9103
movq -0x30(%rbp), %r13
movq %r13, -0x40(%rbp)
movq (%r15), %rdi
callq 0x2220
movq %rax, %rdi
callq 0x2140
movq %rax, %rbx
movq %rax, %rdi
movl $0x2e, %esi
callq 0x20d0
testq %rax, %rax
je 0x9038
movb $0x0, (%rax)
leaq -0x50(%rbp), %r12
movq %r12, %rdi
callq 0x20a0
movq %rax, %r13
movq %rbx, %rdi
callq 0x20a0
movq %rsp, -0x38(%rbp)
leaq (%rax,%r13), %rsi
addq $0x4, %rsi
addq %r13, %rax
addq $0x13, %rax
andq $-0x10, %rax
movq %rsp, %r13
subq %rax, %r13
movq %r13, %rsp
movq %r13, %rdi
leaq 0x5519(%rip), %rdx      # 0xe592
movq %r12, %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x20c0
movq %r13, %rdi
leaq 0x5532(%rip), %rsi      # 0xe5c2
callq 0x21c0
movq %rax, %rbx
testq %rax, %rax
je 0x90f9
movq (%r15), %rdi
callq 0x9bec
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4b49
movl $0x10, %edi
callq 0x2160
movq %rax, %r12
movq %r13, %rdi
callq 0x2220
movq %rax, (%r12)
leaq 0x8(%r12), %r13
movq $0x0, 0x8(%r12)
movq -0x40(%rbp), %rax
movq %r12, (%rax)
movq %rbx, %rdi
callq 0x2090
movq -0x38(%rbp), %rsp
movq 0x8(%r15), %r15
testq %r15, %r15
jne 0x900c
jmp 0x9103
movq %r13, %rdi
callq 0x21d0
jmp 0x909d
xorl %r15d, %r15d
testq %r14, %r14
je 0x911a
movq %r14, %rax
incq %r15
movq 0x8(%rax), %rax
testq %rax, %rax
jne 0x910e
leaq 0x8(,%r15,8), %rdi
callq 0x2160
movq %rax, %rbx
testq %r15, %r15
je 0x9144
xorl %eax, %eax
movq (%r14), %rcx
movq %rcx, (%rbx,%rax,8)
movq 0x8(%r14), %r14
incq %rax
cmpq %rax, %r15
jne 0x9131
movq $0x0, (%rbx,%r15,8)
callq 0x2250
testl %eax, %eax
js 0x91b3
movl %eax, %r14d
jne 0x916d
leaq 0x543e(%rip), %rdi      # 0xe59f
movq %rbx, %rsi
callq 0x21f0
testl %eax, %eax
js 0x91b8
movl %r14d, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x21b0
movq -0x30(%rbp), %rax
movq (%rax), %rbx
testq %rbx, %rbx
je 0x9193
movq (%rbx), %rdi
callq 0x2060
movq 0x8(%rbx), %rbx
jmp 0x9180
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x53e1(%rip), %rdi      # 0xe58a
callq 0x21d0
jmp 0x8fff
callq 0x22f6
callq 0x22e1