#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*/ EXIT_FAILURE;
int /*<<< orphan*/ display();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s <user>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  struct passwd *pwd = getpwnam(argv[1]);
  if (pwd == NULL) {
    printf("'%s' not found\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  printf("Before: ");
  display();
  if (setuid(pwd->pw_uid) < 0) {
    perror("setuid");
    goto fail;
  }
  printf("After: ");
  display();
  exit(0);
fail:
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ba <main+0x4a>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <getpwnam@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fa <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1090 <setuid@plt>
cmp    $0x0,%rax
jge    1235 <main+0xc5>
lea    0xe00(%rip),%rdi
call   1070 <perror@plt>
jmp    1251 <main+0xe1>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <display@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     1196 <main+0x26>
mov    0x2e5d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe71(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11e9 <main+0x79>
mov    0x8(%rbx),%rdi
call   1050 <getpwnam@plt>
test   %rax,%rax
jne    11b8 <main+0x48>
mov    0x8(%rbx),%rsi
lea    0xe63(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11e9 <main+0x79>
mov    %rax,%r14
lea    0xe60(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
mov    (%r14),%edi
call   1090 <setuid@plt>
test   %rax,%rax
jns    11f7 <main+0x87>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xe34(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <display@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <user>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"'%s' not found\0A\00"
@.str.2 = [9 x i8] c"Before: \00"
@.str.3 = [7 x i8] c"setuid\00"
@.str.4 = [8 x i8] c"After: \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.passwd*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call %struct.passwd* @getpwnam(i8* %20)
store %struct.passwd* %21, %struct.passwd** %6
%22 = load %struct.passwd*, %struct.passwd** %6
%23 = icmp eq %struct.passwd* %22, null
br i1 %23, label %24, label %31
24: ; preds = %17
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @EXIT_FAILURE
%30 = call i32 @exit(i32 %29)
unreachable
31: ; preds = %17
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 (...) @display()
%34 = load %struct.passwd*, %struct.passwd** %6
%35 = getelementptr inbounds %struct.passwd, %struct.passwd* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @setuid(i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %45
41: ; preds = %31
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%43 = call i32 (...) @display()
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %39
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <user>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"'%s' not found\0A\00"
@.str.2 = [9 x i8] c"Before: \00"
@.str.3 = [7 x i8] c"setuid\00"
@.str.4 = [8 x i8] c"After: \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call %struct.passwd* @getpwnam(i8* %12)
%14 = icmp eq %struct.passwd* %13, null
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i8*, i8** %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %10
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%22 = tail call i32 (...) @display()
%23 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i64 @setuid(i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %20
%28 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %20
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%33 = tail call i32 (...) @display()
%34 = tail call i32 @exit(i32 0)
unreachable
}