// File: /workspace/github_c_makefile_star2_O3/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, 0x20(%rsp)
movq %rax, 0x18(%rsp)
cmpl $0x3, %edi
jle 0x1492
movq %rsi, %rbx
movq 0x8(%rsi), %r14
leaq 0xd21(%rip), %rsi       # 0x201e
movq %r14, %rdi
callq 0x1030
testl %eax, %eax
jne 0x1492
movb 0x1(%r14), %r12b
movq 0x10(%rbx), %r15
leaq 0x14(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x18(%rsp), %rcx
movq %r15, %rdi
callq 0x123c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
js 0x1458
movl %eax, %r14d
movb %r12b, 0xf(%rsp)
movq 0x20(%rsp), %rsi
leaq 0xcd4(%rip), %rdi       # 0x2021
movl $0x1, %edx
callq 0x1050
movq 0x18(%rsp), %rsi
leaq 0xcc3(%rip), %rdi       # 0x2026
movl $0x1, %edx
callq 0x1050
movq 0x10(%rbx), %rdx
movl 0x14(%rsp), %r12d
leaq 0xcaf(%rip), %rdi       # 0x202c
movl %r14d, %esi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1070
callq 0x1060
testl %eax, %eax
je 0x13ca
movl %eax, %r13d
leaq 0xdf2(%rip), %rdi       # 0x218f
callq 0x1040
cmpl %r14d, %r13d
je 0x1423
movq 0x2c4a(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcf3(%rip), %rsi       # 0x20ab
movl %r13d, %edx
movq %r15, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x10a0
jmp 0x1423
movl %r12d, %edi
callq 0x10d0
testl %eax, %eax
jne 0x1472
leaq 0x10(%rsp), %rax
movl $0x100, (%rax)          # imm = 0x100
movl $0x400, %edi            # imm = 0x400
callq 0x10c0
movq %rax, %r13
movq %r15, %rdi
movl %r12d, %esi
movq %rax, %rdx
leaq 0x10(%rsp), %r15
movq %r15, %rcx
callq 0x10e0
movslq (%r15), %rdi
movq %r13, %rsi
callq 0x1080
testl %eax, %eax
jne 0x147b
movl %r14d, %edi
callq 0x1120
testl %eax, %eax
jne 0x1484
cmpb $0x6e, 0xf(%rsp)
jne 0x1446
movl $0x26, %edi
movl $0x1, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x10b0
testl %eax, %eax
jne 0x1469
movq 0x18(%rbx), %rdi
addq $0x18, %rbx
movq %rbx, %rsi
callq 0x1100
movl %eax, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc6d(%rip), %rdi       # 0x20dd
jmp 0x148b
leaq 0xbcf(%rip), %rdi       # 0x2048
jmp 0x148b
leaq 0xbe6(%rip), %rdi       # 0x2068
jmp 0x148b
leaq 0xc01(%rip), %rdi       # 0x208c
callq 0x10f0
jmp 0x1458
leaq 0xc67(%rip), %rdi       # 0x2100
callq 0x1040
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1110