#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 */

/* Variables and functions */
int /*<<< orphan*/ getchar();
int geteuid();
int /*<<< orphan*/ getpid();
int getsid(int /*<<< orphan*/);
int getuid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int readFile(char *);
int seteuid(int /*<<< orphan*/);
int main(int argc, char **argv) {
  printf("before setuid(0)\n");
  printf("uid: %d\n", getuid());
  printf("sid: %d\n", getsid(getpid()));
  printf("euid: %d\n\n\n", geteuid());
  if (-1 == readFile("/etc/shadow")) {
    perror("readfile");
    return -1;
  }
  if (-1 == seteuid(0)) {
    perror("seteuid(0) error!\n");
    return 0;
  }
  printf("\n\nafter setuid(0)\n");
  printf("uid: %d\n", getuid());
  printf("sid: %d\n", getsid(getpid()));
  printf("euid: %d\n\n\n", geteuid());
  if (-1 == readFile("/etc/shadow")) {
    perror("readfile");
    return -1;
  }
  getchar();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%esi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1060 <getsid@plt>
mov    %eax,%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,%esi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe29(%rip),%rdi
call   10a0 <readFile@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1233 <main+0xa3>
lea    0xe1a(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fc <main+0x16c>
xor    %edi,%edi
call   10b0 <seteuid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1261 <main+0xd1>
lea    0xdf5(%rip),%rdi
call   1090 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fc <main+0x16c>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%esi
lea    0xd93(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1060 <getsid@plt>
mov    %eax,%esi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,%esi
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xd6e(%rip),%rdi
call   10a0 <readFile@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12ee <main+0x15e>
lea    0xd5f(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fc <main+0x16c>
mov    $0x0,%al
call   1080 <getchar@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe9e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <getuid@plt>
lea    0xe45(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1070 <getsid@plt>
lea    0xe30(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <geteuid@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe1e(%rip),%rdi
call   10b0 <readFile@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0xf6>
xor    %ebx,%ebx
xor    %edi,%edi
call   10c0 <seteuid@plt>
cmp    $0xffffffff,%eax
je     12a9 <main+0x109>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getuid@plt>
lea    0xdc4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1070 <getsid@plt>
lea    0xdaf(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <geteuid@plt>
lea    0xda1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd9d(%rip),%rdi
call   10b0 <readFile@plt>
cmp    $0xffffffff,%eax
je     1296 <main+0xf6>
xor    %eax,%eax
call   1090 <getchar@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xd8d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1292 <main+0xf2>
lea    0xd83(%rip),%rdi
call   10a0 <perror@plt>
jmp    1292 <main+0xf2>

LLVM IR (-O0):
@.str = [18 x i8] c"before setuid(0)\0A\00"
@.str.1 = [9 x i8] c"uid: %d\0A\00"
@.str.2 = [9 x i8] c"sid: %d\0A\00"
@.str.3 = [12 x i8] c"euid: %d\0A\0A\0A\00"
@.str.4 = [12 x i8] c"/etc/shadow\00"
@.str.5 = [9 x i8] c"readfile\00"
@.str.6 = [19 x i8] c"seteuid(0) error!\0A\00"
@.str.7 = [19 x i8] c"\0A\0Aafter setuid(0)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%7 = call i32 (...) @getuid()
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = call i32 (...) @getpid()
%10 = call i32 @getsid(i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = call i32 (...) @geteuid()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = call i32 @readFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
18: ; preds = %2
%19 = call i32 @seteuid(i32 0)
%20 = icmp eq i32 -1, %19
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %38
23: ; preds = %18
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%25 = call i32 (...) @getuid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = call i32 (...) @getpid()
%28 = call i32 @getsid(i32 %27)
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = call i32 (...) @geteuid()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = call i32 @readFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%33 = icmp eq i32 -1, %32
br i1 %33, label %34, label %36
34: ; preds = %23
%35 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %38
36: ; preds = %23
%37 = call i32 (...) @getchar()
store i32 0, i32* %3
br label %38
38: ; preds = %36, %34, %21, %16
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"uid: %d\0A\00"
@.str.2 = [9 x i8] c"sid: %d\0A\00"
@.str.3 = [12 x i8] c"euid: %d\0A\0A\0A\00"
@.str.4 = [12 x i8] c"/etc/shadow\00"
@.str.5 = [9 x i8] c"readfile\00"
@.str.6 = [19 x i8] c"seteuid(0) error!\0A\00"
@str = [17 x i8] c"before setuid(0)\00"
@str.8 = [18 x i8] c"\0A\0Aafter setuid(0)\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @getuid()
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 @getsid(i32 %6)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%9 = tail call i32 (...) @geteuid()
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = tail call i32 @readFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %35
15: ; preds = %2
%16 = tail call i32 @seteuid(i32 0)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %35
20: ; preds = %15
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
%22 = tail call i32 (...) @getuid()
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = tail call i32 (...) @getpid()
%25 = tail call i32 @getsid(i32 %24)
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = tail call i32 (...) @geteuid()
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = tail call i32 @readFile(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %20
%32 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %35
33: ; preds = %20
%34 = tail call i32 (...) @getchar()
br label %35
35: ; preds = %33, %31, %18, %13
%36 = phi i32 [ -1, %13 ], [ 0, %18 ], [ -1, %31 ], [ 0, %33 ]
ret i32 %36
}