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

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int geteuid();
int getuid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ setuid(int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ system(char *);
int main(int argc, char **argv) {
  char buf[255];
  pid_t old_uid, e_uid;
  bzero(buf, 255);
  old_uid = getuid();
  e_uid = geteuid();
  printf("before:  uid = %d euid = %d \n", old_uid, e_uid);
  setuid(e_uid);
  printf("after: uid = %d euid = %d \n", getuid(), geteuid());
  strcpy(buf, "/bin/cat /etc/shadow ");
  system(buf);
  setuid(old_uid);
  printf("after drop privileges: uid = %d euid = %d \n", getuid(), geteuid());
  system(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0xff,%esi
call   1080 <bzero@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x114(%rbp)
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    %eax,-0x118(%rbp)
mov    -0x114(%rbp),%esi
mov    -0x118(%rbp),%edx
lea    0xe39(%rip),%rdi
call   1060 <printf@plt>
mov    -0x118(%rbp),%edi
call   1090 <setuid@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x120(%rbp)
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    -0x120(%rbp),%esi
mov    %eax,%edx
lea    0xe24(%rip),%rdi
call   1060 <printf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x110(%rbp),%rdi
call   1050 <system@plt>
mov    -0x114(%rbp),%edi
call   1090 <setuid@plt>
mov    $0x0,%al
call   1040 <getuid@plt>
mov    %eax,-0x11c(%rbp)
mov    $0x0,%al
call   1070 <geteuid@plt>
mov    -0x11c(%rbp),%esi
mov    %eax,%edx
lea    0xe04(%rip),%rdi
call   1060 <printf@plt>
lea    -0x110(%rbp),%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xff,%esi
call   1080 <bzero@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%ebx
lea    0xe5c(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <printf@plt>
mov    %ebx,%edi
call   1090 <setuid@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <geteuid@plt>
lea    0xe53(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1060 <printf@plt>
lea    0xe5f(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rdi
call   1050 <system@plt>
mov    %ebp,%edi
call   1090 <setuid@plt>
xor    %eax,%eax
call   1040 <getuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <geteuid@plt>
lea    0xe47(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"before: uid = %d euid = %d \0A\00"
@.str.1 = [28 x i8] c"after: uid = %d euid = %d \0A\00"
@.str.2 = [22 x i8] c"/bin/cat /etc/shadow \00"
@.str.3 = [44 x i8] c"after drop privileges: uid = %d euid = %d \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [255 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%10 = call i32 @bzero(i8* %9, i32 255)
%11 = call i32 (...) @getuid()
store i32 %11, i32* %7
%12 = call i32 (...) @geteuid()
store i32 %12, i32* %8
%13 = load i32, i32* %7
%14 = load i32, i32* %8
%15 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %14)
%16 = load i32, i32* %8
%17 = call i32 @setuid(i32 %16)
%18 = call i32 (...) @getuid()
%19 = call i32 (...) @geteuid()
%20 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %19)
%21 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%22 = call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%23 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%24 = call i32 @system(i8* %23)
%25 = load i32, i32* %7
%26 = call i32 @setuid(i32 %25)
%27 = call i32 (...) @getuid()
%28 = call i32 (...) @geteuid()
%29 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %28)
%30 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%31 = call i32 @system(i8* %30)
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"before: uid = %d euid = %d \0A\00"
@.str.1 = [28 x i8] c"after: uid = %d euid = %d \0A\00"
@.str.2 = [22 x i8] c"/bin/cat /etc/shadow \00"
@.str.3 = [44 x i8] c"after drop privileges: uid = %d euid = %d \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [255 x i8]
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%5 = call i32 @bzero(i8* %4, i32 255)
%6 = tail call i32 (...) @getuid()
%7 = tail call i32 (...) @geteuid()
%8 = tail call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = tail call i32 @setuid(i32 %7)
%10 = tail call i32 (...) @getuid()
%11 = tail call i32 (...) @geteuid()
%12 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 %11)
%13 = call i32 @strcpy(i8* %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @system(i8* %4)
%15 = call i32 @setuid(i32 %6)
%16 = call i32 (...) @getuid()
%17 = call i32 (...) @geteuid()
%18 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %16, i32 %17)
%19 = call i32 @system(i8* %4)
ret i32 0
}