// File: /workspace/github_c_makefile_star2_O1/muayyad-alsadi[P]oneway/oneway.c
// Function: main
int main(int argc, char * argv[]) {
    int is_one_way=0, running_uid, uid, gid;
    char *user_name=NULL;
    char *home=NULL, *shell=NULL;
    if (argc<4) usage();
    if (strcasecmp(argv[1], "-n")) usage();
    is_one_way = (argv[1][1]=='n');
    user_name = argv[2];
    uid = name_to_uid(user_name, &gid, &home, &shell);
    if (uid<0) {
        return -1;
    }
    setenv("HOME", home, 1);
    setenv("SHELL", shell, 1);
    printf("setting uid=%d (%s) gid=%d\n", uid, argv[2], gid);
    running_uid = getuid();
    if (running_uid == 0) {
        /* process is running as root, drop privileges */
        if (setgid(gid) != 0) {
            perror("Unable to drop group privileges");
            return -1;
        }
        int ngroups=256;
        gid_t *list=malloc(sizeof(gid_t)*ngroups);
        getgrouplist(user_name, gid, list, &ngroups);
        if (setgroups(ngroups, list) !=0 ) {
            perror("Unable to drop supplementary groups");
            return -1;
        }
        if (setuid(uid) != 0) {
            perror("Unable to drop user privileges");
            return -1;
        }

    } else {
        printf("already not privileged\n");
        if (running_uid!=uid) {
            fprintf(stderr, "WARNING: running as %d, did not change to %s[%d]\n", running_uid, user_name, uid);
        }
    }
    if (is_one_way && prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
        perror("failed calling prctl(NO_NEW_PRIVS)");
        return -1;
    }
    return execvp(argv[3], &argv[3]);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x10(%rsp)
cmpl $0x3, %edi
jle 0x14b2
movq %rsi, %rbx
movq 0x8(%rsi), %r14
leaq 0xd21(%rip), %rsi       # 0x201e
movq %r14, %rdi
callq 0x1030
testl %eax, %eax
jne 0x14b2
movb 0x1(%r14), %r13b
movq 0x10(%rbx), %r14
leaq 0xc(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
callq 0x123c
movl %eax, %ebp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testl %ebp, %ebp
js 0x1461
movq 0x18(%rsp), %rsi
leaq 0xcda(%rip), %rdi       # 0x2021
movl $0x1, %edx
callq 0x1050
movq 0x10(%rsp), %rsi
leaq 0xcc9(%rip), %rdi       # 0x2026
movl $0x1, %edx
callq 0x1050
movq 0x10(%rbx), %rdx
movl 0xc(%rsp), %r15d
leaq 0xcb5(%rip), %rdi       # 0x202c
movl %ebp, %esi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1070
callq 0x1060
testl %eax, %eax
je 0x13c7
movl %eax, %r12d
leaq 0xdf9(%rip), %rdi       # 0x218f
callq 0x1040
cmpl %ebp, %r12d
je 0x142f
movq 0x2c4d(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcf6(%rip), %rsi       # 0x20ab
movl %r12d, %edx
movq %r14, %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x10a0
jmp 0x142f
movl %r15d, %edi
callq 0x10d0
testl %eax, %eax
jne 0x1479
movb %r13b, 0xb(%rsp)
leaq 0x24(%rsp), %r12
movl $0x100, (%r12)          # imm = 0x100
movl $0x400, %edi            # imm = 0x400
callq 0x10c0
movq %rax, %r13
movq %r14, %rdi
movl %r15d, %esi
movq %rax, %rdx
movq %r12, %rcx
callq 0x10e0
movslq (%r12), %rdi
movq %r13, %rsi
callq 0x1080
testl %eax, %eax
jne 0x148c
movl %ebp, %edi
callq 0x1120
testl %eax, %eax
movb 0xb(%rsp), %r13b
jne 0x149a
xorl %eax, %eax
movb $0x1, %cl
testb %cl, %cl
je 0x1461
cmpb $0x6e, %r13b
jne 0x1451
movl $0x26, %edi
movl $0x1, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x10b0
testl %eax, %eax
jne 0x1470
movq 0x18(%rbx), %rdi
addq $0x18, %rbx
movq %rbx, %rsi
callq 0x1100
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc66(%rip), %rdi       # 0x20dd
jmp 0x1480
leaq 0xbc8(%rip), %rdi       # 0x2048
callq 0x10f0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1461
leaq 0xbd5(%rip), %rdi       # 0x2068
movb 0xb(%rsp), %r13b
jmp 0x14a1
leaq 0xbeb(%rip), %rdi       # 0x208c
callq 0x10f0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ecx, %ecx
jmp 0x142b
leaq 0xc47(%rip), %rdi       # 0x2100
callq 0x1040
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110