#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef scalar_t__ uid_t;
struct passwd {
  int pw_gid;
  int pw_uid;
};

/* Variables and functions */
scalar_t__ atoi(char *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
struct passwd *getpwnam(char *);
struct passwd *getpwuid(scalar_t__);
scalar_t__ getuid();
scalar_t__ initgroups(char *, int);
scalar_t__ setgid(int);
scalar_t__ setuid(int);
int /*<<< orphan*/ stderr;
int os_drop_privs(char *user) {
  int err;
  struct passwd *pw = NULL;
  if (getuid() == (uid_t)0) {
    if (atoi(user)) {
      pw = getpwuid((uid_t)atoi(user));
    } else {
      pw = getpwnam(user);
    }
    if (pw) {
      if (initgroups(user, pw->pw_gid) != 0 || setgid(pw->pw_gid) != 0 ||
          setuid(pw->pw_uid) != 0) {
        err = errno;
        fprintf(stderr, "Couldn't change to %s, gid=%d, uid=%d\n", user,
                pw->pw_gid, pw->pw_uid);
        errno = err;
        return -1;
      }
    } else {
      err = errno;
      fprintf(stderr, "Couldn't lookup user %s\n", user);
      errno = err;
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<os_drop_privs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getuid@plt>
cmp    $0x0,%rax
jne    12c4 <os_drop_privs+0x144>
mov    -0x10(%rbp),%rdi
call   1080 <atoi@plt>
cmp    $0x0,%rax
je     11d2 <os_drop_privs+0x52>
mov    -0x10(%rbp),%rdi
call   1080 <atoi@plt>
mov    %rax,%rdi
call   1030 <getpwuid@plt>
mov    %rax,-0x20(%rbp)
jmp    11df <os_drop_privs+0x5f>
mov    -0x10(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1280 <os_drop_privs+0x100>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   10a0 <initgroups@plt>
cmp    $0x0,%rax
jne    122e <os_drop_privs+0xae>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1070 <setgid@plt>
cmp    $0x0,%rax
jne    122e <os_drop_privs+0xae>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
je     127b <os_drop_privs+0xfb>
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%r8d
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%ecx
mov    0x2d6b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <os_drop_privs+0x14b>
jmp    12bf <os_drop_privs+0x13f>
mov    0x2d51(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x14(%rbp),%ecx
mov    0x2d27(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12cb <os_drop_privs+0x14b>
jmp    12c4 <os_drop_privs+0x144>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_drop_privs>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   1040 <getuid@plt>
test   %rax,%rax
jne    1244 <os_drop_privs+0xc4>
mov    %r14,%rdi
call   1080 <atoi@plt>
test   %rax,%rax
je     11b0 <os_drop_privs+0x30>
mov    %rax,%rdi
call   1030 <getpwuid@plt>
jmp    11b8 <os_drop_privs+0x38>
mov    %r14,%rdi
call   1050 <getpwnam@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1213 <os_drop_privs+0x93>
mov    (%rbx),%esi
mov    %r14,%rdi
call   10a0 <initgroups@plt>
test   %rax,%rax
jne    11e8 <os_drop_privs+0x68>
mov    (%rbx),%edi
call   1070 <setgid@plt>
test   %rax,%rax
jne    11e8 <os_drop_privs+0x68>
mov    0x4(%rbx),%edi
call   1090 <setuid@plt>
test   %rax,%rax
je     1244 <os_drop_privs+0xc4>
mov    0x2de9(%rip),%rax
mov    (%rax),%ebp
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%ecx
mov    0x4(%rbx),%r8d
lea    0xdf9(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1236 <os_drop_privs+0xb6>
mov    0x2dbe(%rip),%rax
mov    (%rax),%ebp
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d9b(%rip),%rax
mov    %ebp,(%rax)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@errno = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Couldn't change to %s, gid=%d, uid=%d\0A\00"
@.str.1 = [25 x i8] c"Couldn't lookup user %s\0A\00"
define i32 @os_drop_privs(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.passwd*
store i8* %0, i8** %3
store %struct.passwd* null, %struct.passwd** %5
%6 = call i64 (...) @getuid()
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %61
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i64 @atoi(i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = call i64 @atoi(i8* %13)
%15 = call %struct.passwd* @getpwuid(i64 %14)
store %struct.passwd* %15, %struct.passwd** %5
br label %19
16: ; preds = %8
%17 = load i8*, i8** %3
%18 = call %struct.passwd* @getpwnam(i8* %17)
store %struct.passwd* %18, %struct.passwd** %5
br label %19
19: ; preds = %16, %12
%20 = load %struct.passwd*, %struct.passwd** %5
%21 = icmp ne %struct.passwd* %20, null
br i1 %21, label %22, label %54
22: ; preds = %19
%23 = load i8*, i8** %3
%24 = load %struct.passwd*, %struct.passwd** %5
%25 = getelementptr inbounds %struct.passwd, %struct.passwd* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @initgroups(i8* %23, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %41, label %29
29: ; preds = %22
%30 = load %struct.passwd*, %struct.passwd** %5
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %30, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @setgid(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %29
%36 = load %struct.passwd*, %struct.passwd** %5
%37 = getelementptr inbounds %struct.passwd, %struct.passwd* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = call i64 @setuid(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %53
41: ; preds = %35, %29, %22
%42 = load i32, i32* @errno
store i32 %42, i32* %4
%43 = load i32, i32* @stderr
%44 = load i8*, i8** %3
%45 = load %struct.passwd*, %struct.passwd** %5
%46 = getelementptr inbounds %struct.passwd, %struct.passwd* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = load %struct.passwd*, %struct.passwd** %5
%49 = getelementptr inbounds %struct.passwd, %struct.passwd* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %44, i32 %47, i32 %50)
%52 = load i32, i32* %4
store i32 %52, i32* @errno
store i32 -1, i32* %2
br label %62
53: ; preds = %35
br label %60
54: ; preds = %19
%55 = load i32, i32* @errno
store i32 %55, i32* %4
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %3
%58 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %57)
%59 = load i32, i32* %4
store i32 %59, i32* @errno
store i32 -1, i32* %2
br label %62
60: ; preds = %53
br label %61
61: ; preds = %60, %1
store i32 0, i32* %2
br label %62
62: ; preds = %61, %54, %41
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@errno = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Couldn't change to %s, gid=%d, uid=%d\0A\00"
@.str.1 = [25 x i8] c"Couldn't lookup user %s\0A\00"
define i32 @os_drop_privs(i8* %0) {
%2 = tail call i64 (...) @getuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %41
4: ; preds = %1
%5 = tail call i64 @atoi(i8* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %9, label %7
7: ; preds = %4
%8 = tail call %struct.passwd* @getpwuid(i64 %5)
br label %11
9: ; preds = %4
%10 = tail call %struct.passwd* @getpwnam(i8* %0)
br label %11
11: ; preds = %9, %7
%12 = phi %struct.passwd* [ %8, %7 ], [ %10, %9 ]
%13 = icmp eq %struct.passwd* %12, null
br i1 %13, label %35, label %14
14: ; preds = %11
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 0
%16 = load i32, i32* %15
%17 = tail call i64 @initgroups(i8* %0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i32, i32* %15
%21 = tail call i64 @setgid(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 1
%25 = load i32, i32* %24
%26 = tail call i64 @setuid(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %41, label %28
28: ; preds = %23, %19, %14
%29 = load i32, i32* @errno
%30 = load i32, i32* @stderr
%31 = load i32, i32* %15
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %12, i64 0, i32 1
%33 = load i32, i32* %32
%34 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %31, i32 %33)
br label %39
35: ; preds = %11
%36 = load i32, i32* @errno
%37 = load i32, i32* @stderr
%38 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %39
39: ; preds = %28, %35
%40 = phi i32 [ %36, %35 ], [ %29, %28 ]
store i32 %40, i32* @errno
br label %41
41: ; preds = %39, %1, %23
%42 = phi i32 [ 0, %23 ], [ 0, %1 ], [ -1, %39 ]
ret i32 %42
}