// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, 0x20(%rsp)
andq $0x0, 0x18(%rsp)
cmpl $0x3, %edi
jle 0x1488
movq %rsi, %rbx
movq 0x8(%rsi), %r14
leaq 0xd25(%rip), %rsi       # 0x201e
movq %r14, %rdi
callq 0x1030
testl %eax, %eax
jne 0x1488
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
pushq $-0x1
popq %rbp
testl %eax, %eax
js 0x144e
movl %eax, %r14d
movb %r12b, 0xf(%rsp)
movq 0x20(%rsp), %rsi
leaq 0xcda(%rip), %rdi       # 0x2021
pushq $0x1
popq %r12
movl %r12d, %edx
callq 0x1050
movq 0x18(%rsp), %rsi
leaq 0xcc7(%rip), %rdi       # 0x2026
movl %r12d, %edx
callq 0x1050
movq 0x10(%rbx), %rdx
movl 0x14(%rsp), %r12d
leaq 0xcb5(%rip), %rdi       # 0x202c
movl %r14d, %esi
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1070
callq 0x1060
testl %eax, %eax
je 0x13c4
movl %eax, %r13d
leaq 0xdf8(%rip), %rdi       # 0x218f
callq 0x1040
cmpl %r14d, %r13d
je 0x141d
movq 0x2c50(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0xcf9(%rip), %rsi       # 0x20ab
movl %r13d, %edx
movq %r15, %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0x10a0
jmp 0x141d
movl %r12d, %edi
callq 0x10d0
testl %eax, %eax
jne 0x1468
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 0x1471
movl %r14d, %edi
callq 0x1120
testl %eax, %eax
jne 0x147a
cmpb $0x6e, 0xf(%rsp)
jne 0x143c
pushq $0x26
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x10b0
testl %eax, %eax
jne 0x145f
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 0xc77(%rip), %rdi       # 0x20dd
jmp 0x1481
leaq 0xbd9(%rip), %rdi       # 0x2048
jmp 0x1481
leaq 0xbf0(%rip), %rdi       # 0x2068
jmp 0x1481
leaq 0xc0b(%rip), %rdi       # 0x208c
callq 0x10f0
jmp 0x144e
callq 0x12b3