#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;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ LOG_NOTICE;
scalar_t__ geteuid();
struct passwd *getpwnam(char const *);
scalar_t__ getuid();
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
int set_run_user(const char *run_user) {
  if (getuid() == 0 && geteuid() == 0) {
    struct passwd *pw = getpwnam(run_user);
    if (pw) {
      syslog(LOG_NOTICE, "setting user to  %s", run_user);
      setuid(pw->pw_uid);
      return 1;
    }
  }
  syslog(LOG_ERR, "failed to set run user, calling user was not root");
  return 0;
}


x86 assembly (clang, -O0):
<set_run_user>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
cmp    $0x0,%rax
jne    11cd <set_run_user+0x7d>
mov    $0x0,%al
call   1040 <geteuid@plt>
cmp    $0x0,%rax
jne    11cd <set_run_user+0x7d>
mov    -0x10(%rbp),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11c8 <set_run_user+0x78>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1070 <setuid@plt>
movl   $0x1,-0x4(%rbp)
jmp    11eb <set_run_user+0x9b>
jmp    11cd <set_run_user+0x7d>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_run_user>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <getuid@plt>
test   %rax,%rax
jne    1172 <set_run_user+0x22>
xor    %eax,%eax
call   1040 <geteuid@plt>
test   %rax,%rax
je     1191 <set_run_user+0x41>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe92(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r14,%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
je     1172 <set_run_user+0x22>
mov    %rax,%r15
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    (%r15),%edi
call   1070 <setuid@plt>
mov    $0x1,%ebx
jmp    1189 <set_run_user+0x39>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@LOG_NOTICE = global i32 0
@.str = [20 x i8] c"setting user to %s\00"
@LOG_ERR = global i32 0
@.str.1 = [50 x i8] c"failed to set run user, calling user was not root\00"
define i32 @set_run_user(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.passwd*
store i8* %0, i8** %3
%5 = call i64 (...) @getuid()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %24
7: ; preds = %1
%8 = call i64 (...) @geteuid()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %24
10: ; preds = %7
%11 = load i8*, i8** %3
%12 = call %struct.passwd* @getpwnam(i8* %11)
store %struct.passwd* %12, %struct.passwd** %4
%13 = load %struct.passwd*, %struct.passwd** %4
%14 = icmp ne %struct.passwd* %13, null
br i1 %14, label %15, label %23
15: ; preds = %10
%16 = load i32, i32* @LOG_NOTICE
%17 = load i8*, i8** %3
%18 = call i32 (i32, i8*, ...) @syslog(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load %struct.passwd*, %struct.passwd** %4
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @setuid(i32 %21)
store i32 1, i32* %2
br label %27
23: ; preds = %10
br label %24
24: ; preds = %23, %7, %1
%25 = load i32, i32* @LOG_ERR
%26 = call i32 (i32, i8*, ...) @syslog(i32 %25, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %27
27: ; preds = %24, %15
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@LOG_NOTICE = global i32 0
@.str = [20 x i8] c"setting user to %s\00"
@LOG_ERR = global i32 0
@.str.1 = [50 x i8] c"failed to set run user, calling user was not root\00"
define i32 @set_run_user(i8* %0) {
%2 = tail call i64 (...) @getuid()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %16
4: ; preds = %1
%5 = tail call i64 (...) @geteuid()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %16
7: ; preds = %4
%8 = tail call %struct.passwd* @getpwnam(i8* %0)
%9 = icmp eq %struct.passwd* %8, null
br i1 %9, label %16, label %10
10: ; preds = %7
%11 = load i32, i32* @LOG_NOTICE
%12 = tail call i32 (i32, i8*, ...) @syslog(i32 %11, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = getelementptr inbounds %struct.passwd, %struct.passwd* %8, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @setuid(i32 %14)
br label %19
16: ; preds = %7, %4, %1
%17 = load i32, i32* @LOG_ERR
%18 = tail call i32 (i32, i8*, ...) @syslog(i32 %17, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %10, %16
%20 = phi i32 [ 1, %10 ], [ 0, %16 ]
ret i32 %20
}