// File: /workspace/github_c_makefile_star2_O2/hifi[P]whatever/whatever.c
// Function: main
int main(int argc, char **argv)
{
    pid_t pid;

    if (argc < 2)
        die("usage: %s <program> [args]\n", argv[0]);

    pid = fork();

    if (pid < 0)
        die("%s: failed to fork()\n", argv[0]);

    if (pid == 0) {
        /* child */
        ptrace(PTRACE_TRACEME, 0, NULL, NULL);
        execv(argv[1], argv+1);
        die("%s: failed to execv()", argv[0]);
    } else {
        /* parent */
        waitpid(pid, NULL, 0);
        ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACESYSGOOD|PTRACE_O_TRACEFORK|PTRACE_O_TRACEEXEC);
        ptrace(PTRACE_SYSCALL, pid, NULL, NULL);

        while (1) {
            int status;
            pid_t trace_pid = waitpid(-1, &status, 0);

            if (trace_pid < 0) {
                kill(pid, SIGKILL);
                return 1;
            }

            if (WIFEXITED(status)) {
                if (trace_pid == pid)
                    break;

                continue;
            }

            if (WSTOPSIG(status) == (SIGTRAP|0x80)) {
                int syscall = ptrace(PTRACE_PEEKUSER, trace_pid, 8*ORIG_RAX, NULL);

                switch (syscall) {
                    case SYS_setresuid:
                    {
                        struct user_regs_struct regs;
                        ptrace(PTRACE_GETREGS, trace_pid, 0, &regs);

                        if (regs.rdi >= 0)
                            processes[trace_pid].ruid = regs.rdi;
                        if (regs.rsi >= 0)
                            processes[trace_pid].euid = regs.rsi;
                        if (regs.rdx >= 0)
                            processes[trace_pid].suid = regs.rdx;

                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, 0);
                        break;
                    }

                    case SYS_setresgid:
                    {
                        struct user_regs_struct regs;
                        ptrace(PTRACE_GETREGS, trace_pid, 0, &regs);

                        if (regs.rdi >= 0)
                            processes[trace_pid].rgid = regs.rdi;
                        if (regs.rsi >= 0)
                            processes[trace_pid].egid = regs.rsi;
                        if (regs.rdx >= 0)
                            processes[trace_pid].sgid = regs.rdx;

                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, 0);
                        break;
                    }

                    case SYS_getresuid:
                    {
                        struct user_regs_struct regs;
                        ptrace(PTRACE_GETREGS, trace_pid, 0, &regs);

                        if (regs.rdi > 0)
                            poke_short(trace_pid, regs.rdi, processes[trace_pid].ruid);

                        if (regs.rsi > 0)
                            poke_short(trace_pid, regs.rsi, processes[trace_pid].euid);

                        if (regs.rdx > 0)
                            poke_short(trace_pid, regs.rdx, processes[trace_pid].suid);

                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, 0);
                        break;
                    }

                    case SYS_getresgid:
                    {
                        struct user_regs_struct regs;
                        ptrace(PTRACE_GETREGS, trace_pid, 0, &regs);

                        if (regs.rdi > 0)
                            poke_short(trace_pid, regs.rdi, processes[trace_pid].rgid);

                        if (regs.rsi > 0)
                            poke_short(trace_pid, regs.rsi, processes[trace_pid].egid);

                        if (regs.rdx > 0)
                            poke_short(trace_pid, regs.rdx, processes[trace_pid].sgid);

                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, 0);
                        break;
                    }

                    case SYS_getuid:
                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, processes[trace_pid].ruid);
                        break;
                    case SYS_getgid:
                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, processes[trace_pid].rgid);
                        break;
                    case SYS_geteuid:
                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, processes[trace_pid].euid);
                        break;
                    case SYS_getegid:
                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, processes[trace_pid].egid);
                        break;

                    case SYS_getgroups:
                    case SYS_setgroups:
                    case SYS_chown:
                    case SYS_fchown:
                    case SYS_lchown:
                    case SYS_fchownat:
                    case SYS_setreuid:
                    case SYS_setregid:
                    case SYS_setuid:
                    case SYS_setgid:
                        ptrace(PTRACE_POKEUSER, trace_pid, 8*RAX, 0);
                    default:
                        break;
                }
            } else {
                /* fork */
                pid_t child_pid;
                ptrace(PTRACE_GETEVENTMSG, trace_pid, 0, &child_pid);
                /* blindly forcing all children to think they are root initially */
                memset(&processes[child_pid], 0, sizeof(struct who));
                ptrace(PTRACE_SETOPTIONS, child_pid, NULL, PTRACE_O_TRACESYSGOOD|PTRACE_O_TRACEFORK|PTRACE_O_TRACEEXEC);
            }

            ptrace(PTRACE_SYSCALL, trace_pid, NULL, NULL);
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, %r14
cmpl $0x1, %edi
jle 0x401976
callq 0x44eb40
testl %eax, %eax
js 0x401982
je 0x40198e
movl %eax, %ebx
movl %eax, %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x44ea80
pushq $0x13
popq %rcx
movl $0x4200, %edi           # imm = 0x4200
movl %ebx, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x450b90
pushq $0x18
popq %rdi
movl %ebx, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x450b90
pushq $-0x1
popq %r15
leaq 0xc(%rsp), %r12
leaq 0xc9bf8(%rip), %r13     # 0x4cb2f0
movl %r15d, %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x44ea80
testl %eax, %eax
js 0x401953
movl %eax, %ebp
movl 0xc(%rsp), %eax
testb $0x7f, %al
je 0x40175e
andl $0xff00, %eax           # imm = 0xFF00
cmpl $0x8500, %eax           # imm = 0x8500
jne 0x401767
pushq $0x3
popq %rdi
movl %ebp, %esi
pushq $0x78
popq %rdx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x450b90
leal -0x5c(%rax), %ecx
cmpl $0x1c, %ecx
ja 0x4017a9
leaq 0x978dd(%rip), %rdx     # 0x499020
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
movl %ebp, %r14d
jmpq *%rax
pushq $0x6
popq %rdi
movl %ebp, %esi
pushq $0x50
popq %rdx
xorl %ecx, %ecx
jmp 0x401937
cmpl %ebx, %ebp
jne 0x4016f8
jmp 0x401962
movl $0x4201, %edi           # imm = 0x4201
movl %ebp, %esi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x450b90
movslq 0x10(%rsp), %rsi
imulq $0x18, %rsi, %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax,%r13)
andq $0x0, 0x10(%rax,%r13)
movl $0x4200, %edi           # imm = 0x4200
xorl %edx, %edx
pushq $0x13
popq %rcx
xorl %eax, %eax
callq 0x450b90
jmp 0x40193e
cmpl $0x104, %eax            # imm = 0x104
je 0x40174f
jmp 0x40193e
pushq $0xc
popq %rdi
movl %ebp, %esi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x450b90
movl 0x80(%rsp), %eax
imulq $0x18, %r14, %rcx
movl %eax, 0xc(%rcx,%r13)
movl 0x78(%rsp), %eax
movl %eax, 0x10(%rcx,%r13)
movl 0x70(%rsp), %eax
movl %eax, 0x14(%rcx,%r13)
jmp 0x40174f
pushq $0xc
popq %rdi
movl %ebp, %esi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x450b90
movq 0x80(%rsp), %rsi
testq %rsi, %rsi
je 0x401820
imulq $0x18, %r14, %rax
movswl 0xc(%rax,%r13), %edx
movl %ebp, %edi
callq 0x401a70
movq 0x78(%rsp), %rsi
testq %rsi, %rsi
je 0x40183b
imulq $0x18, %r14, %rax
movswl 0x10(%rax,%r13), %edx
movl %ebp, %edi
callq 0x401a70
movq 0x70(%rsp), %rsi
testq %rsi, %rsi
je 0x40174f
imulq $0x18, %r14, %rax
movswl 0x14(%rax,%r13), %edx
jmp 0x40190f
imulq $0x18, %r14, %rax
movl 0x4(%rax,%r13), %ecx
jmp 0x40192f
pushq $0xc
popq %rdi
movl %ebp, %esi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x450b90
movl 0x80(%rsp), %eax
imulq $0x18, %r14, %rcx
movl %eax, (%rcx,%r13)
movl 0x78(%rsp), %eax
movl %eax, 0x4(%rcx,%r13)
movl 0x70(%rsp), %eax
movl %eax, 0x8(%rcx,%r13)
jmp 0x40174f
imulq $0x18, %r14, %rax
movl (%rax,%r13), %ecx
jmp 0x40192f
pushq $0xc
popq %rdi
movl %ebp, %esi
xorl %edx, %edx
leaq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x450b90
movq 0x80(%rsp), %rsi
testq %rsi, %rsi
je 0x4018dc
imulq $0x18, %r14, %rax
movswl (%rax,%r13), %edx
movl %ebp, %edi
callq 0x401a70
movq 0x78(%rsp), %rsi
testq %rsi, %rsi
je 0x4018f7
imulq $0x18, %r14, %rax
movswl 0x4(%rax,%r13), %edx
movl %ebp, %edi
callq 0x401a70
movq 0x70(%rsp), %rsi
testq %rsi, %rsi
je 0x40174f
imulq $0x18, %r14, %rax
movswl 0x8(%rax,%r13), %edx
movl %ebp, %edi
callq 0x401a70
jmp 0x40174f
imulq $0x18, %r14, %rax
movl 0x10(%rax,%r13), %ecx
jmp 0x40192f
imulq $0x18, %r14, %rax
movl 0xc(%rax,%r13), %ecx
pushq $0x6
popq %rdi
movl %ebp, %esi
pushq $0x50
popq %rdx
xorl %eax, %eax
callq 0x450b90
pushq $0x18
popq %rdi
movl %ebp, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x450b90
jmp 0x4016f8
pushq $0x9
popq %rsi
movl %ebx, %edi
callq 0x40a280
pushq $0x1
popq %rax
jmp 0x401964
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r14), %rsi
leaq 0x97714(%rip), %rdi     # 0x499094
jmp 0x4019b4
movq (%r14), %rsi
leaq 0x97724(%rip), %rdi     # 0x4990b0
jmp 0x4019b4
xorl %edi, %edi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x450b90
leaq 0x8(%r14), %rsi
movq 0x8(%r14), %rdi
callq 0x44f540
movq (%r14), %rsi
leaq 0x97712(%rip), %rdi     # 0x4990c6
xorl %eax, %eax
callq 0x4019bb