#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 seteuid(int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
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 == 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());
  system("/bin/sh");
  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   1060 <printf@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%esi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1070 <getsid@plt>
mov    %eax,%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <geteuid@plt>
mov    %eax,%esi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   10b0 <seteuid@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    122e <main+0x9e>
lea    0xe13(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a2 <main+0x112>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,%esi
lea    0xdc6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1070 <getsid@plt>
mov    %eax,%esi
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <geteuid@plt>
mov    %eax,%esi
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdc7(%rip),%rdi
call   1050 <system@plt>
mov    $0x0,%al
call   1090 <getchar@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <getuid@plt>
lea    0xe45(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1080 <getsid@plt>
lea    0xe30(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1090 <geteuid@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   10c0 <seteuid@plt>
cmp    $0xffffffff,%eax
je     1274 <main+0xd4>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1050 <getuid@plt>
lea    0xde1(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%edi
call   1080 <getsid@plt>
lea    0xdcc(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   1090 <geteuid@plt>
lea    0xdbe(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdcd(%rip),%rdi
call   1060 <system@plt>
xor    %eax,%eax
call   10a0 <getchar@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xda3(%rip),%rdi
call   10b0 <perror@plt>
jmp    1270 <main+0xd0>

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 = [19 x i8] c"seteuid(0) error!\0A\00"
@.str.5 = [19 x i8] c"\0A\0Aafter setuid(0)\0A\00"
@.str.6 = [8 x i8] c"/bin/sh\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 @seteuid(i32 0)
%15 = icmp eq i32 -1, %14
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %29
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%20 = call i32 (...) @getuid()
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = call i32 (...) @getpid()
%23 = call i32 @getsid(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = call i32 (...) @geteuid()
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%28 = call i32 (...) @getchar()
store i32 0, i32* %3
br label %29
29: ; preds = %18, %16
%30 = load i32, i32* %3
ret i32 %30
}

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 = [19 x i8] c"seteuid(0) error!\0A\00"
@.str.6 = [8 x i8] c"/bin/sh\00"
@str = [17 x i8] c"before setuid(0)\00"
@str.7 = [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 @seteuid(i32 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 ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
br label %26
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
%17 = tail call i32 (...) @getuid()
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 @getsid(i32 %19)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = tail call i32 (...) @geteuid()
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%25 = tail call i32 (...) @getchar()
br label %26
26: ; preds = %15, %13
ret i32 0
}