#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 int uid_t;
struct passwd {
  int pw_uid;
  char *pw_name;
};

/* Variables and functions */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ printf(char *, char *);
int setuid(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int);
int /*<<< orphan*/ usleep(int);
int main(int argc, char *argv[]) {
  struct passwd *pwd;
  uid_t uid;
  int ret = 0;
  if (argc < 2) {
    printf("Usage: %s <uid_value> \n", argv[0]);
    goto finished;
  }
  pwd = getpwnam(argv[1]);
  if (!pwd) {
    fprintf(stderr, "getpwnam() failed: %s\n", strerror(errno));
    ret = -errno;
    goto finished;
  }
  uid = pwd->pw_uid;
  fprintf(stdout, "Setting UID to %s (%d).\n", pwd->pw_name, uid);
  if ((ret = setuid(uid))) {
    fprintf(stderr, "Call to setuid() failed with error: %s\n",
            strerror(errno));
    ret = -errno;
    goto finished;
  }
  while (1) {
    usleep(3000000);
  }
finished:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6b(%rip),%rdi
call   1030 <printf@plt>
jmp    1290 <main+0x130>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fc <main+0x9c>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2de6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    1290 <main+0x130>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <setuid@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
je     127c <main+0x11c>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    0x2d66(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x20(%rbp)
jmp    1290 <main+0x130>
jmp    1281 <main+0x121>
mov    $0x2dc6c0,%edi
call   1080 <usleep@plt>
jmp    1281 <main+0x121>
mov    -0x20(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     117f <main+0x1f>
mov    (%rsi),%rsi
lea    0xe8d(%rip),%rdi
call   1030 <printf@plt>
xor    %ebx,%ebx
jmp    121c <main+0xbc>
mov    0x8(%rsi),%rdi
call   1040 <getpwnam@plt>
test   %rax,%rax
je     11ec <main+0x8c>
mov    (%rax),%ebp
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%edi
mov    0x8(%rax),%rdx
lea    0xe8c(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1060 <setuid@plt>
test   %eax,%eax
je     11e0 <main+0x80>
mov    0x2e20(%rip),%rax
mov    (%rax),%ebp
mov    0x2e0f(%rip),%r14
mov    (%r14),%edi
call   1070 <strerror@plt>
lea    0xe70(%rip),%rsi
jmp    120d <main+0xad>
nopw   0x0(%rax,%rax,1)
mov    $0x2dc6c0,%edi
call   1080 <usleep@plt>
jmp    11e0 <main+0x80>
mov    0x2ded(%rip),%rax
mov    (%rax),%ebp
mov    0x2ddc(%rip),%r14
mov    (%r14),%edi
call   1070 <strerror@plt>
lea    0xe0d(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
sub    (%r14),%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.passwd = type { i32, i8* }
@.str = [24 x i8] c"Usage: %s <uid_value> \0A\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"getpwnam() failed: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.2 = [25 x i8] c"Setting UID to %s (%d).\0A\00"
@.str.3 = [40 x i8] c"Call to setuid() failed with error: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %53
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call %struct.passwd* @getpwnam(i8* %19)
store %struct.passwd* %20, %struct.passwd** %6
%21 = load %struct.passwd*, %struct.passwd** %6
%22 = icmp ne %struct.passwd* %21, null
br i1 %22, label %30, label %23
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %8
br label %53
30: ; preds = %16
%31 = load %struct.passwd*, %struct.passwd** %6
%32 = getelementptr inbounds %struct.passwd, %struct.passwd* %31, i32 0, i32 0
%33 = load i32, i32* %32
store i32 %33, i32* %7
%34 = load i32, i32* @stdout
%35 = load %struct.passwd*, %struct.passwd** %6
%36 = getelementptr inbounds %struct.passwd, %struct.passwd* %35, i32 0, i32 1
%37 = load i8*, i8** %36
%38 = load i32, i32* %7
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %37, i32 %38)
%40 = load i32, i32* %7
%41 = call i32 @setuid(i32 %40)
store i32 %41, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %30
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = load i32, i32* @errno
%49 = sub nsw i32 0, %48
store i32 %49, i32* %8
br label %53
50: ; preds = %30
br label %51
51: ; preds = %50, %51
%52 = call i32 @usleep(i32 3000000)
br label %51
53: ; preds = %43, %23, %11
%54 = load i32, i32* %8
ret i32 %54
}

LLVM IR (-O2):
%struct.passwd = type { i32, i8* }
@.str = [24 x i8] c"Usage: %s <uid_value> \0A\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"getpwnam() failed: %s\0A\00"
@errno = global i32 0
@stdout = global i32 0
@.str.2 = [25 x i8] c"Setting UID to %s (%d).\0A\00"
@.str.3 = [40 x i8] c"Call to setuid() failed with error: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %37
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call %struct.passwd* @getpwnam(i8* %9)
%11 = icmp eq %struct.passwd* %10, null
br i1 %11, label %12, label %19
12: ; preds = %7
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @errno
%18 = sub nsw i32 0, %17
br label %37
19: ; preds = %7
%20 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 0
%21 = load i32, i32* %20
%22 = load i32, i32* @stdout
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %10, i64 0, i32 1
%24 = load i8*, i8** %23
%25 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %24, i32 %21)
%26 = tail call i32 @setuid(i32 %21)
%27 = icmp eq i32 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @errno
%34 = sub nsw i32 0, %33
br label %37
35: ; preds = %19, %35
%36 = tail call i32 @usleep(i32 3000000)
br label %35
37: ; preds = %28, %12, %4
%38 = phi i32 [ 0, %4 ], [ %34, %28 ], [ %18, %12 ]
ret i32 %38
}