#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;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int geteuid();
int getuid();
int setuid(int);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  fprintf(stdout,
          "Run the two following commands as root to test this program:\n"
          "chown 1000.1000 %s\n"
          "chmod u+s %s\n\n",
          argv[0], argv[0]);
  uid_t real_user_id = getuid();
  uid_t effective_user_id = geteuid();
  fprintf(stdout, "Real user ID of the process (UID) = %u\n", getuid());
  fprintf(stdout, "Effective user ID of the process (EUID) = %u\n", geteuid());
  int result;
  result = setuid(real_user_id);
  fprintf(stdout,
          "\nSet EUID to the UID (lose privileges): setuid(%u) = %d [%s]\n\n",
          real_user_id, result, result == 0 ? "success" : "failure");
  fprintf(stdout, "Real user ID of the process (UID) = %u\n", getuid());
  fprintf(stdout, "Effective user ID of the process (EUID) = %u\n", geteuid());
  result = setuid(effective_user_id);
  fprintf(stdout,
          "\nSet EUID to the former EUID (gain back privileges): setuid(%u) = "
          "%d [%s]\n\n",
          effective_user_id, result, result == 0 ? "success" : "failure");
  fprintf(stdout, "Real user ID of the process (UID) = %u\n", getuid());
  fprintf(stdout, "Effective user ID of the process (EUID) = %u\n", geteuid());
  return 0;
}


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)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
lea    0xe8c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1030 <getuid@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%edx
lea    0xeb1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%edx
lea    0xeb3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1060 <setuid@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x1c(%rbp),%esi
lea    0xefd(%rip),%r8
lea    0xeee(%rip),%rax
cmp    $0x0,%esi
cmove  %rax,%r8
lea    0xea1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%edx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%edx
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1060 <setuid@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x1c(%rbp),%esi
lea    0xe71(%rip),%r8
lea    0xe62(%rip),%rax
cmp    $0x0,%esi
cmove  %rax,%r8
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   1030 <getuid@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <geteuid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e8e(%rip),%r13
mov    0x0(%r13),%edi
mov    (%rsi),%rcx
lea    0xea0(%rip),%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %eax,%r12d
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1030 <getuid@plt>
lea    0xed0(%rip),%r15
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1040 <geteuid@plt>
lea    0xed8(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1060 <setuid@plt>
mov    0x0(%r13),%edi
test   %eax,%eax
lea    0xf26(%rip),%r14
lea    0xf27(%rip),%rbx
mov    %rbx,%r8
cmove  %r14,%r8
lea    0xed2(%rip),%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %ebp,%edi
lea    0xe6c(%rip),%rbp
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r12d,%edi
call   1060 <setuid@plt>
mov    0x0(%r13),%edi
test   %eax,%eax
cmove  %r14,%rbx
lea    0xec4(%rip),%rsi
mov    %r12d,%edx
mov    %eax,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1030 <getuid@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x0(%r13),%ebx
xor    %eax,%eax
call   1040 <geteuid@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [95 x i8] c"Run the two following commands as root to test this program:\0Achown 1000.1000 %s\0Achmod u+s %s\0A\0A\00"
@.str.1 = [40 x i8] c"Real user ID of the process (UID) = %u\0A\00"
@.str.2 = [46 x i8] c"Effective user ID of the process (EUID) = %u\0A\00"
@.str.3 = [63 x i8] c"\0ASet EUID to the UID (lose privileges): setuid(%u) = %d [%s]\0A\0A\00"
@.str.4 = [8 x i8] c"success\00"
@.str.5 = [8 x i8] c"failure\00"
@.str.6 = [76 x i8] c"\0ASet EUID to the former EUID (gain back privileges): setuid(%u) = %d [%s]\0A\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @stdout
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 0
%12 = load i8*, i8** %11
%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 %9, i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str, i64 0, i64 0), i8* %12, i8* %15)
%17 = call i32 (...) @getuid()
store i32 %17, i32* %6
%18 = call i32 (...) @geteuid()
store i32 %18, i32* %7
%19 = load i32, i32* @stdout
%20 = call i32 (...) @getuid()
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @stdout
%23 = call i32 (...) @geteuid()
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %6
%26 = call i32 @setuid(i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* @stdout
%28 = load i32, i32* %6
%29 = load i32, i32* %8
%30 = load i32, i32* %8
%31 = icmp eq i32 %30, 0
%32 = zext i1 %31 to i64
%33 = select i1 %31, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %29, i8* %33)
%35 = load i32, i32* @stdout
%36 = call i32 (...) @getuid()
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @stdout
%39 = call i32 (...) @geteuid()
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %7
%42 = call i32 @setuid(i32 %41)
store i32 %42, i32* %8
%43 = load i32, i32* @stdout
%44 = load i32, i32* %7
%45 = load i32, i32* %8
%46 = load i32, i32* %8
%47 = icmp eq i32 %46, 0
%48 = zext i1 %47 to i64
%49 = select i1 %47, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.6, i64 0, i64 0), i32 %44, i32 %45, i8* %49)
%51 = load i32, i32* @stdout
%52 = call i32 (...) @getuid()
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %52)
%54 = load i32, i32* @stdout
%55 = call i32 (...) @geteuid()
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %55)
ret i32 0
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [95 x i8] c"Run the two following commands as root to test this program:\0Achown 1000.1000 %s\0Achmod u+s %s\0A\0A\00"
@.str.1 = [40 x i8] c"Real user ID of the process (UID) = %u\0A\00"
@.str.2 = [46 x i8] c"Effective user ID of the process (EUID) = %u\0A\00"
@.str.3 = [63 x i8] c"\0ASet EUID to the UID (lose privileges): setuid(%u) = %d [%s]\0A\0A\00"
@.str.4 = [8 x i8] c"success\00"
@.str.5 = [8 x i8] c"failure\00"
@.str.6 = [76 x i8] c"\0ASet EUID to the former EUID (gain back privileges): setuid(%u) = %d [%s]\0A\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @stdout
%4 = load i8*, i8** %1
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %3, i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %4)
%6 = tail call i32 (...) @getuid()
%7 = tail call i32 (...) @geteuid()
%8 = load i32, i32* @stdout
%9 = tail call i32 (...) @getuid()
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @stdout
%12 = tail call i32 (...) @geteuid()
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = tail call i32 @setuid(i32 %6)
%15 = load i32, i32* @stdout
%16 = icmp eq i32 %14, 0
%17 = select i1 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i32 %6, i32 %14, i8* %17)
%19 = load i32, i32* @stdout
%20 = tail call i32 (...) @getuid()
%21 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @stdout
%23 = tail call i32 (...) @geteuid()
%24 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = tail call i32 @setuid(i32 %7)
%26 = load i32, i32* @stdout
%27 = icmp eq i32 %25, 0
%28 = select i1 %27, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0)
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([76 x i8], [76 x i8]* @.str.6, i64 0, i64 0), i32 %7, i32 %25, i8* %28)
%30 = load i32, i32* @stdout
%31 = tail call i32 (...) @getuid()
%32 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @stdout
%34 = tail call i32 (...) @geteuid()
%35 = tail call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %34)
ret i32 0
}