#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 */
int /*<<< orphan*/ LOG_ERR;
scalar_t__ errno;
struct passwd *getpwnam(char *);
scalar_t__ initgroups(char *, int /*<<< orphan*/);
scalar_t__ setgid(int /*<<< orphan*/);
scalar_t__ setuid(int /*<<< orphan*/);
char *strerror(scalar_t__);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, char *, char *);
int drop_to_user(char *username) {
  struct passwd *pw = getpwnam(username);
  if (pw == NULL) {
    syslog(LOG_ERR, "User \"%s\": %s\n", username,
           errno ? strerror(errno) : "User not found");
    return -1;
  }
  if (setgid(pw->pw_gid) < 0)
    return -1;
  if (initgroups(username, pw->pw_gid) < 0)
    return -1;
  if (setuid(pw->pw_uid) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<drop_to_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f4 <drop_to_user+0x94>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e41(%rip),%rax
cmpq   $0x0,(%rax)
je     11c1 <drop_to_user+0x61>
mov    0x2e30(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %rax,-0x30(%rbp)
jmp    11d1 <drop_to_user+0x71>
lea    0xe47(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    11d1 <drop_to_user+0x71>
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rcx
lea    0xe1d(%rip),%rsi
call   1040 <syslog@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <drop_to_user+0x104>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <setgid@plt>
cmp    $0x0,%rax
jge    1216 <drop_to_user+0xb6>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <drop_to_user+0x104>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
call   1080 <initgroups@plt>
cmp    $0x0,%rax
jge    123c <drop_to_user+0xdc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <drop_to_user+0x104>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1060 <setuid@plt>
cmp    $0x0,%rax
jge    125d <drop_to_user+0xfd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <drop_to_user+0x104>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drop_to_user>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <getpwnam@plt>
test   %rax,%rax
je     11a8 <drop_to_user+0x48>
mov    %rax,%rbx
mov    0x4(%rax),%edi
call   1050 <setgid@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
js     11e8 <drop_to_user+0x88>
mov    0x4(%rbx),%esi
mov    %r14,%rdi
call   1080 <initgroups@plt>
test   %rax,%rax
js     11e8 <drop_to_user+0x88>
mov    (%rbx),%edi
call   1060 <setuid@plt>
mov    %rax,%r15
sar    $0x3f,%r15
jmp    11e8 <drop_to_user+0x88>
mov    0x2e21(%rip),%rax
mov    (%rax),%ebx
mov    0x2e28(%rip),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     11ca <drop_to_user+0x6a>
call   1070 <strerror@plt>
mov    %rax,%rcx
jmp    11d1 <drop_to_user+0x71>
lea    0xe3e(%rip),%rcx
lea    0xe28(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
call   1040 <syslog@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i32 }
@LOG_ERR = global i32 0
@.str = [15 x i8] c"User \22%s\22: %s\0A\00"
@errno = global i64 0
@.str.1 = [15 x i8] c"User not found\00"
define i32 @drop_to_user(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call %struct.passwd* @getpwnam(i8* %5)
store %struct.passwd* %6, %struct.passwd** %4
%7 = load %struct.passwd*, %struct.passwd** %4
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %9, label %21
9: ; preds = %1
%10 = load i32, i32* @LOG_ERR
%11 = load i8*, i8** %3
%12 = load i64, i64* @errno
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i64, i64* @errno
%16 = call i8* @strerror(i64 %15)
br label %18
17: ; preds = %9
br label %18
18: ; preds = %17, %14
%19 = phi i8* [ %16, %14 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %17 ]
%20 = call i32 @syslog(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %19)
store i32 -1, i32* %2
br label %44
21: ; preds = %1
%22 = load %struct.passwd*, %struct.passwd** %4
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = call i64 @setgid(i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 -1, i32* %2
br label %44
28: ; preds = %21
%29 = load i8*, i8** %3
%30 = load %struct.passwd*, %struct.passwd** %4
%31 = getelementptr inbounds %struct.passwd, %struct.passwd* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i64 @initgroups(i8* %29, i32 %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %28
store i32 -1, i32* %2
br label %44
36: ; preds = %28
%37 = load %struct.passwd*, %struct.passwd** %4
%38 = getelementptr inbounds %struct.passwd, %struct.passwd* %37, i32 0, i32 0
%39 = load i32, i32* %38
%40 = call i64 @setuid(i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %2
br label %44
43: ; preds = %36
store i32 0, i32* %2
br label %44
44: ; preds = %43, %42, %35, %27, %18
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.passwd = type { i32, i32 }
@LOG_ERR = global i32 0
@.str = [15 x i8] c"User \22%s\22: %s\0A\00"
@errno = global i64 0
@.str.1 = [15 x i8] c"User not found\00"
define i32 @drop_to_user(i8* %0) {
%2 = tail call %struct.passwd* @getpwnam(i8* %0)
%3 = icmp eq %struct.passwd* %2, null
br i1 %3, label %4, label %13
4: ; preds = %1
%5 = load i32, i32* @LOG_ERR
%6 = load i64, i64* @errno
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = tail call i8* @strerror(i64 %6)
br label %10
10: ; preds = %4, %8
%11 = phi i8* [ %9, %8 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %4 ]
%12 = tail call i32 @syslog(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11)
br label %28
13: ; preds = %1
%14 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 1
%15 = load i32, i32* %14
%16 = tail call i64 @setgid(i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %28, label %18
18: ; preds = %13
%19 = load i32, i32* %14
%20 = tail call i64 @initgroups(i8* %0, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %2, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i64 @setuid(i32 %24)
%26 = ashr i64 %25, 63
%27 = trunc i64 %26 to i32
br label %28
28: ; preds = %22, %18, %13, %10
%29 = phi i32 [ -1, %10 ], [ -1, %13 ], [ -1, %18 ], [ %27, %22 ]
ret i32 %29
}