// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
cmpl $0x4, -0x8(%rbp)
jge 0x1340
callq 0x12e0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0xd5f(%rip), %rsi       # 0x20ae
callq 0x1030
cmpl $0x0, %eax
je 0x135e
callq 0x12e0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movsbl 0x1(%rax), %eax
cmpl $0x6e, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x30(%rbp), %rdx
leaq -0x38(%rbp), %rcx
callq 0x1230
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x13ae
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1535
movq -0x30(%rbp), %rsi
leaq 0xcf8(%rip), %rdi       # 0x20b1
movl $0x1, %edx
callq 0x1040
movq -0x38(%rbp), %rsi
leaq 0xce8(%rip), %rdi       # 0x20b6
movl $0x1, %edx
callq 0x1040
movl -0x1c(%rbp), %esi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdx
movl -0x20(%rbp), %ecx
leaq 0xccf(%rip), %rdi       # 0x20bc
movb $0x0, %al
callq 0x1060
callq 0x1050
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jne 0x14a8
movl -0x20(%rbp), %edi
callq 0x10c0
cmpl $0x0, %eax
je 0x142b
leaq 0xcbe(%rip), %rdi       # 0x20d8
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1535
movl $0x100, -0x3c(%rbp)     # imm = 0x100
movslq -0x3c(%rbp), %rdi
shlq $0x2, %rdi
callq 0x10b0
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rdi
movl -0x20(%rbp), %esi
movq -0x48(%rbp), %rdx
leaq -0x3c(%rbp), %rcx
callq 0x10d0
movslq -0x3c(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x1070
cmpl $0x0, %eax
je 0x1481
leaq 0xc88(%rip), %rdi       # 0x20f8
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1535
movl -0x1c(%rbp), %edi
callq 0x1110
cmpl $0x0, %eax
je 0x14a6
leaq 0xc87(%rip), %rdi       # 0x211c
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1535
jmp 0x14e3
leaq 0xc8c(%rip), %rdi       # 0x213b
movb $0x0, %al
callq 0x1060
movl -0x18(%rbp), %eax
cmpl -0x1c(%rbp), %eax
je 0x14e1
movq 0x2b33(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movl -0x18(%rbp), %edx
movq -0x28(%rbp), %rcx
movl -0x1c(%rbp), %r8d
leaq 0xc79(%rip), %rsi       # 0x2153
movb $0x0, %al
callq 0x1090
jmp 0x14e3
cmpl $0x0, -0x14(%rbp)
je 0x151d
movl $0x26, %edi
movl $0x1, %esi
xorl %r8d, %r8d
movl %r8d, %edx
movl %r8d, %ecx
movb $0x0, %al
callq 0x10a0
cmpl $0x0, %eax
je 0x151d
leaq 0xc76(%rip), %rdi       # 0x2185
callq 0x10e0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1535
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rsi
addq $0x18, %rsi
callq 0x10f0
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq