#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;
};
struct Log {
  int dummy;
};
struct Except {
  int dummy;
};

/* Variables and functions */
scalar_t__ EPERM;
int /*<<< orphan*/ Except_raise(struct Except *, int, char *, ...);
scalar_t__ errno;
struct passwd *getpwnam(char *);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ strerror(scalar_t__);
void Security_setUser(char *userName, struct Log *logger, struct Except *eh) {
  struct passwd *pw = getpwnam(userName);
  if (!pw) {
    Except_raise(
        eh, -1,
        "Failed to set UID, couldn't find user named [%s] in the system.",
        strerror(errno));
    return;
  }
  if (setuid(pw->pw_uid)) {
    if (errno == EPERM) {
      Except_raise(eh, -2, "You do not have permission to set UID.");
      return;
    }
    Except_raise(eh, -3, "Failed to set UID [%s]", strerror(errno));
  }
}


x86 assembly (clang, -O0):
<Security_setUser>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a1 <Security_setUser+0x61>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%ecx
mov    $0xffffffff,%esi
lea    0xe6b(%rip),%rdx
mov    $0x0,%al
call   1040 <Except_raise@plt>
jmp    121c <Security_setUser+0xdc>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1050 <setuid@plt>
cmp    $0x0,%rax
je     121c <Security_setUser+0xdc>
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <Security_setUser+0xac>
mov    -0x18(%rbp),%rdi
mov    $0xfffffffe,%esi
lea    0xe60(%rip),%rdx
mov    $0x0,%al
call   1040 <Except_raise@plt>
jmp    121c <Security_setUser+0xdc>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%ecx
mov    $0xfffffffd,%esi
lea    0xe52(%rip),%rdx
mov    $0x0,%al
call   1040 <Except_raise@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Security_setUser>:
push   %rbx
mov    %rdx,%rbx
call   1030 <getpwnam@plt>
test   %rax,%rax
je     1187 <Security_setUser+0x47>
mov    (%rax),%edi
call   1050 <setuid@plt>
test   %rax,%rax
je     11a7 <Security_setUser+0x67>
mov    0x2e7f(%rip),%rax
mov    (%rax),%rdi
mov    0x2e65(%rip),%rax
cmp    (%rax),%rdi
jne    11a9 <Security_setUser+0x69>
lea    0xec9(%rip),%rdx
mov    %rbx,%rdi
mov    $0xfffffffe,%esi
xor    %eax,%eax
pop    %rbx
jmp    1040 <Except_raise@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
lea    0xe63(%rip),%rdx
mov    %rbx,%rdi
mov    $0xffffffff,%esi
jmp    11bd <Security_setUser+0x7d>
pop    %rbx
ret
call   1060 <strerror@plt>
lea    0xeb2(%rip),%rdx
mov    %rbx,%rdi
mov    $0xfffffffd,%esi
mov    %eax,%ecx
xor    %eax,%eax
pop    %rbx
jmp    1040 <Except_raise@plt>

LLVM IR (-O0):
%struct.Log = type { i32 }
%struct.Except = type { i32 }
%struct.passwd = type { i32 }
@.str = [64 x i8] c"Failed to set UID, couldn't find user named [%s] in the system.\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.1 = [39 x i8] c"You do not have permission to set UID.\00"
@.str.2 = [23 x i8] c"Failed to set UID [%s]\00"
define void @Security_setUser(i8* %0, %struct.Log* %1, %struct.Except* %2) {
%4 = alloca i8*
%5 = alloca %struct.Log*
%6 = alloca %struct.Except*
%7 = alloca %struct.passwd*
store i8* %0, i8** %4
store %struct.Log* %1, %struct.Log** %5
store %struct.Except* %2, %struct.Except** %6
%8 = load i8*, i8** %4
%9 = call %struct.passwd* @getpwnam(i8* %8)
store %struct.passwd* %9, %struct.passwd** %7
%10 = load %struct.passwd*, %struct.passwd** %7
%11 = icmp ne %struct.passwd* %10, null
br i1 %11, label %17, label %12
12: ; preds = %3
%13 = load %struct.Except*, %struct.Except** %6
%14 = load i64, i64* @errno
%15 = call i32 @strerror(i64 %14)
%16 = call i32 (%struct.Except*, i32, i8*, ...) @Except_raise(%struct.Except* %13, i32 -1, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i32 %15)
br label %35
17: ; preds = %3
%18 = load %struct.passwd*, %struct.passwd** %7
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @setuid(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %17
%24 = load i64, i64* @errno
%25 = load i64, i64* @EPERM
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load %struct.Except*, %struct.Except** %6
%29 = call i32 (%struct.Except*, i32, i8*, ...) @Except_raise(%struct.Except* %28, i32 -2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %35
30: ; preds = %23
%31 = load %struct.Except*, %struct.Except** %6
%32 = load i64, i64* @errno
%33 = call i32 @strerror(i64 %32)
%34 = call i32 (%struct.Except*, i32, i8*, ...) @Except_raise(%struct.Except* %31, i32 -3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %12, %27, %30, %17
ret void
}

LLVM IR (-O2):
%struct.Log = type { i32 }
%struct.Except = type { i32 }
%struct.passwd = type { i32 }
@.str = [64 x i8] c"Failed to set UID, couldn't find user named [%s] in the system.\00"
@errno = global i64 0
@EPERM = global i64 0
@.str.1 = [39 x i8] c"You do not have permission to set UID.\00"
@.str.2 = [23 x i8] c"Failed to set UID [%s]\00"
define void @Security_setUser(i8* nocapture %0, %struct.Log* nocapture readnone %1, %struct.Except* %2) {
%4 = tail call %struct.passwd* @getpwnam(i8* %0)
%5 = icmp eq %struct.passwd* %4, null
br i1 %5, label %6, label %10
6: ; preds = %3
%7 = load i64, i64* @errno
%8 = tail call i32 @strerror(i64 %7)
%9 = tail call i32 (%struct.Except*, i32, i8*, ...) @Except_raise(%struct.Except* %2, i32 -1, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %24
10: ; preds = %3
%11 = getelementptr inbounds %struct.passwd, %struct.passwd* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i64 @setuid(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %24, label %15
15: ; preds = %10
%16 = load i64, i64* @errno
%17 = load i64, i64* @EPERM
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 (%struct.Except*, i32, i8*, ...) @Except_raise(%struct.Except* %2, i32 -2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %24
21: ; preds = %15
%22 = tail call i32 @strerror(i64 %16)
%23 = tail call i32 (%struct.Except*, i32, i8*, ...) @Except_raise(%struct.Except* %2, i32 -3, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %10, %21, %19, %6
ret void
}