#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 */
struct passwd {
  int /*<<< orphan*/ pw_uid;
  int /*<<< orphan*/ pw_gid;
};

/* Variables and functions */
struct passwd *getpwnam(char const *);
int /*<<< orphan*/ getuid();
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
char drop_privs_to_user(char const *username) {
  if (!getuid()) {
    struct passwd *pswd = getpwnam(username);
    if (!pswd)
      return 0;
    if (!pswd->pw_uid)
      return 0;
    if (!pswd->pw_gid)
      return 0;
    if (setgid(pswd->pw_gid) != 0)
      return 0;
    if (setuid(pswd->pw_uid) != 0)
      return 0;
    return 1;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<drop_privs_to_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%eax
jne    11f0 <drop_privs_to_user+0xb0>
mov    -0x10(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117d <drop_privs_to_user+0x3d>
movb   $0x0,-0x1(%rbp)
jmp    11f4 <drop_privs_to_user+0xb4>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    1193 <drop_privs_to_user+0x53>
movb   $0x0,-0x1(%rbp)
jmp    11f4 <drop_privs_to_user+0xb4>
mov    -0x18(%rbp),%rax
cmpl   $0x0,0x4(%rax)
jne    11aa <drop_privs_to_user+0x6a>
movb   $0x0,-0x1(%rbp)
jmp    11f4 <drop_privs_to_user+0xb4>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <setgid@plt>
cmp    $0x0,%rax
je     11c9 <drop_privs_to_user+0x89>
movb   $0x0,-0x1(%rbp)
jmp    11f4 <drop_privs_to_user+0xb4>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <setuid@plt>
cmp    $0x0,%rax
je     11e7 <drop_privs_to_user+0xa7>
movb   $0x0,-0x1(%rbp)
jmp    11f4 <drop_privs_to_user+0xb4>
movb   $0x1,-0x1(%rbp)
jmp    11f4 <drop_privs_to_user+0xb4>
movb   $0x1,-0x1(%rbp)
movsbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_privs_to_user>:
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ecx
mov    $0x1,%al
test   %ecx,%ecx
je     1155 <drop_privs_to_user+0x15>
pop    %rbx
ret
mov    %rbx,%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     117b <drop_privs_to_user+0x3b>
mov    %rax,%rbx
cmpl   $0x0,(%rax)
je     117b <drop_privs_to_user+0x3b>
mov    0x4(%rbx),%edi
test   %edi,%edi
je     117b <drop_privs_to_user+0x3b>
call   1050 <setgid@plt>
test   %rax,%rax
je     117f <drop_privs_to_user+0x3f>
xor    %eax,%eax
pop    %rbx
ret
mov    (%rbx),%edi
call   1060 <setuid@plt>
test   %rax,%rax
sete   %al
pop    %rbx
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
define signext i8 @drop_privs_to_user(i8* %0) {
%2 = alloca i8
%3 = alloca i8*
%4 = alloca %struct.passwd*
store i8* %0, i8** %3
%5 = call i32 (...) @getuid()
%6 = icmp ne i32 %5, 0
br i1 %6, label %40, label %7
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = call %struct.passwd* @getpwnam(i8* %8)
store %struct.passwd* %9, %struct.passwd** %4
%10 = load %struct.passwd*, %struct.passwd** %4
%11 = icmp ne %struct.passwd* %10, null
br i1 %11, label %13, label %12
12: ; preds = %7
store i8 0, i8* %2
br label %41
13: ; preds = %7
%14 = load %struct.passwd*, %struct.passwd** %4
%15 = getelementptr inbounds %struct.passwd, %struct.passwd* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %13
store i8 0, i8* %2
br label %41
19: ; preds = %13
%20 = load %struct.passwd*, %struct.passwd** %4
%21 = getelementptr inbounds %struct.passwd, %struct.passwd* %20, i32 0, i32 1
%22 = load i32, i32* %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %19
store i8 0, i8* %2
br label %41
25: ; preds = %19
%26 = load %struct.passwd*, %struct.passwd** %4
%27 = getelementptr inbounds %struct.passwd, %struct.passwd* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = call i64 @setgid(i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %25
store i8 0, i8* %2
br label %41
32: ; preds = %25
%33 = load %struct.passwd*, %struct.passwd** %4
%34 = getelementptr inbounds %struct.passwd, %struct.passwd* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @setuid(i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
store i8 0, i8* %2
br label %41
39: ; preds = %32
store i8 1, i8* %2
br label %41
40: ; preds = %1
store i8 1, i8* %2
br label %41
41: ; preds = %40, %39, %38, %31, %24, %18, %12
%42 = load i8, i8* %2
ret i8 %42
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
define signext i8 @drop_privs_to_user(i8* nocapture %0) {
%2 = tail call i32 (...) @getuid()
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %23
4: ; preds = %1
%5 = tail call %struct.passwd* @getpwnam(i8* %0)
%6 = icmp eq %struct.passwd* %5, null
br i1 %6, label %23, label %7
7: ; preds = %4
%8 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 0
%9 = load i32, i32* %8
%10 = icmp eq i32 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %7
%12 = getelementptr inbounds %struct.passwd, %struct.passwd* %5, i64 0, i32 1
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %23, label %15
15: ; preds = %11
%16 = tail call i64 @setgid(i32 %13)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i32, i32* %8
%20 = tail call i64 @setuid(i32 %19)
%21 = icmp eq i64 %20, 0
%22 = zext i1 %21 to i8
br label %23
23: ; preds = %1, %4, %7, %11, %15, %18
%24 = phi i8 [ 0, %4 ], [ 0, %7 ], [ 0, %11 ], [ 0, %15 ], [ %22, %18 ], [ 1, %1 ]
ret i8 %24
}