#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 execve(char *, char *const *, char *const *);
int fork();
int getpid();
int getppid();
int open(char *, char);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  int a = fork();
  char *const argv[] = {"/home/flag19/flag19", "-c", "/usr/bin/id", NULL};
  char *const envp[] = {NULL};
  if (a == 0) {
    int newfd = open("/tmp/a1", 'w');
    printf("Newfd is %d\n", newfd);
    sleep(1);
    printf("Child PID is %d\n", getpid());
    printf("Parent process ID before running flag19 is %d\n", getppid());
    int x = execve("/home/flag19/flag19", argv, envp);
    if (x == -1) {
      printf("Execve failed\n");
    }
  } else {
    printf("Parent PID is %d\n", getpid());
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    0x2c60(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c5d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c5a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c57(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1270 <main+0xf0>
lea    0xe3d(%rip),%rdi
mov    $0x77,%esi
call   1070 <open@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    0xdb5(%rip),%rdi
call   1060 <execve@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    126b <main+0xeb>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128e <main+0x10e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x38,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
movaps 0x2c7e(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0x2c62(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
test   %eax,%eax
je     11cc <main+0x4c>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xeba(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    124c <main+0xcc>
lea    0xe50(%rip),%rdi
mov    $0x77,%esi
call   1070 <open@plt>
lea    0xe47(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe33(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1080 <getppid@plt>
lea    0xe2d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd4(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1060 <execve@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0xcc>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x38,%rsp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"/home/flag19/flag19\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [12 x i8] c"/usr/bin/id\00"
@__const.main.argv = [4 x i8*] [i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.3 = [8 x i8] c"/tmp/a1\00"
@.str.4 = [13 x i8] c"Newfd is %d\0A\00"
@.str.5 = [17 x i8] c"Child PID is %d\0A\00"
@.str.6 = [47 x i8] c"Parent process ID before running flag19 is %d\0A\00"
@.str.7 = [15 x i8] c"Execve failed\0A\00"
@.str.8 = [18 x i8] c"Parent PID is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [4 x i8*]
%4 = alloca [1 x i8*]
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = bitcast [4 x i8*]* %3 to i8*
%9 = bitcast [1 x i8*]* %4 to i8*
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %0
%13 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8 signext 119)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = call i32 @sleep(i32 1)
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%19 = call i32 (...) @getppid()
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [4 x i8*], [4 x i8*]* %3, i64 0, i64 0
%22 = getelementptr inbounds [1 x i8*], [1 x i8*]* %4, i64 0, i64 0
%23 = call i32 @execve(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %21, i8** %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %12
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0))
br label %28
28: ; preds = %26, %12
br label %32
29: ; preds = %0
%30 = call i32 (...) @getpid()
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32 %30)
store i32 0, i32* %1
br label %32
32: ; preds = %29, %28
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str = [20 x i8] c"/home/flag19/flag19\00"
@.str.1 = [3 x i8] c"-c\00"
@.str.2 = [12 x i8] c"/usr/bin/id\00"
@__const.main.argv = [4 x i8*] [i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i32 0, i32 0), i8* null]
@.str.3 = [8 x i8] c"/tmp/a1\00"
@.str.4 = [13 x i8] c"Newfd is %d\0A\00"
@.str.5 = [17 x i8] c"Child PID is %d\0A\00"
@.str.6 = [47 x i8] c"Parent process ID before running flag19 is %d\0A\00"
@.str.8 = [18 x i8] c"Parent PID is %d\0A\00"
@str = [14 x i8] c"Execve failed\00"
define i32 @main() {
%1 = alloca [4 x i8*]
%2 = alloca [1 x i8*]
%3 = tail call i32 (...) @fork()
%4 = bitcast [4 x i8*]* %1 to i8*
%5 = bitcast [1 x i8*]* %2 to i8*
%6 = bitcast [1 x i8*]* %2 to i64*
store i64 0, i64* %6
%7 = icmp eq i32 %3, 0
br i1 %7, label %8, label %22
8: ; preds = %0
%9 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8 signext 119)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %9)
%11 = tail call i32 @sleep(i32 1)
%12 = tail call i32 (...) @getpid()
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %12)
%14 = tail call i32 (...) @getppid()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.6, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [4 x i8*], [4 x i8*]* %1, i64 0, i64 0
%17 = getelementptr inbounds [1 x i8*], [1 x i8*]* %2, i64 0, i64 0
%18 = call i32 @execve(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8** %16, i8** %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %8
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %25
22: ; preds = %0
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %8, %20, %22
ret i32 0
}