#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;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/);
scalar_t__ geteuid();
struct passwd *getpwnam(char *);
scalar_t__ getuid();
int /*<<< orphan*/ printf(char *, long, long);
int /*<<< orphan*/ setuid(int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  FILE *fp;
  struct passwd *user;
  int c;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <login name>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  user = getpwnam(argv[1]);
  setuid(0);
  printf("Real: %ld Effective: %ld\n", (long)getuid(), (long)geteuid());
  fp = fopen("/etc/shadow", "r");
  if (fp == NULL) {
    fprintf(stderr, "Err. Can't open file.");
    exit(EXIT_FAILURE);
  }
  while ((c = fgetc(fp)) != EOF)
    fputc(c, stdout);
  fclose(fp);
  setuid(user->pw_uid);
  printf("Real: %ld Effective: %ld\n", (long)getuid(), (long)geteuid());
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11fc <main+0x4c>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <getpwnam@plt>
mov    %rax,-0x20(%rbp)
xor    %edi,%edi
call   10d0 <setuid@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
lea    0xdf2(%rip),%rdi
lea    0xdf7(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1280 <main+0xd0>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    1285 <main+0xd5>
mov    -0x18(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d50(%rip),%rcx
cmp    (%rcx),%eax
je     12b6 <main+0x106>
mov    -0x24(%rbp),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
call   1080 <fputc@plt>
jmp    1285 <main+0xd5>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   10d0 <setuid@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    -0x38(%rbp),%rsi
mov    %rax,%rdx
lea    0xd2e(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    12a0 <main+0xf0>
mov    0x8(%rsi),%rdi
call   1090 <getpwnam@plt>
mov    %rax,%r14
xor    %edi,%edi
call   10d0 <setuid@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <geteuid@plt>
lea    0xe2c(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
lea    0xe34(%rip),%rdi
lea    0xe39(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12bc <main+0x10c>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <fgetc@plt>
mov    0x2dc3(%rip),%r15
cmp    (%r15),%eax
je     1258 <main+0xa8>
mov    0x2d97(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12),%esi
mov    %eax,%edi
call   1080 <fputc@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
cmp    (%r15),%eax
jne    1240 <main+0x90>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    (%r14),%edi
call   10d0 <setuid@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <geteuid@plt>
lea    0xd98(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd4d(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    12d3 <main+0x123>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd74(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s <login name>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"Real: %ld Effective: %ld\0A\00"
@.str.2 = [12 x i8] c"/etc/shadow\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [22 x i8] c"Err. Can't open file.\00"
@EOF = global i32 0
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca %struct.passwd*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call %struct.passwd* @getpwnam(i8* %22)
store %struct.passwd* %23, %struct.passwd** %7
%24 = call i32 @setuid(i32 0)
%25 = call i64 (...) @getuid()
%26 = call i64 (...) @geteuid()
%27 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %25, i64 %26)
%28 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %28, i32** %6
%29 = load i32*, i32** %6
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %36
31: ; preds = %19
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %19
br label %37
37: ; preds = %42, %36
%38 = load i32*, i32** %6
%39 = call i32 @fgetc(i32* %38)
store i32 %39, i32* %8
%40 = load i32, i32* @EOF
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %37
%43 = load i32, i32* %8
%44 = load i32, i32* @stdout
%45 = call i32 @fputc(i32 %43, i32 %44)
br label %37
46: ; preds = %37
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
%49 = load %struct.passwd*, %struct.passwd** %7
%50 = getelementptr inbounds %struct.passwd, %struct.passwd* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @setuid(i32 %51)
%53 = call i64 (...) @getuid()
%54 = call i64 (...) @geteuid()
%55 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %53, i64 %54)
%56 = load i32, i32* @EXIT_SUCCESS
ret i32 %56
}

LLVM IR (-O2):
%struct.passwd = type { i32 }
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s <login name>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"Real: %ld Effective: %ld\0A\00"
@.str.2 = [12 x i8] c"/etc/shadow\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [22 x i8] c"Err. Can't open file.\00"
@EOF = global i32 0
@stdout = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 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 = tail call i32 @setuid(i32 0)
%15 = tail call i64 (...) @getuid()
%16 = tail call i64 (...) @geteuid()
%17 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %15, i64 %16)
%18 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %24, label %20
20: ; preds = %10
%21 = tail call i32 @fgetc(i32* %18)
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %36, label %29
24: ; preds = %10
%25 = load i32, i32* @stderr
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %20, %29
%30 = phi i32 [ %33, %29 ], [ %21, %20 ]
%31 = load i32, i32* @stdout
%32 = tail call i32 @fputc(i32 %30, i32 %31)
%33 = tail call i32 @fgetc(i32* %18)
%34 = load i32, i32* @EOF
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %29
36: ; preds = %29, %20
%37 = tail call i32 @fclose(i32* %18)
%38 = getelementptr inbounds %struct.passwd, %struct.passwd* %13, i64 0, i32 0
%39 = load i32, i32* %38
%40 = tail call i32 @setuid(i32 %39)
%41 = tail call i64 (...) @getuid()
%42 = tail call i64 (...) @geteuid()
%43 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i64 %41, i64 %42)
%44 = load i32, i32* @EXIT_SUCCESS
ret i32 %44
}