#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 /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int PATH_MAX;
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpidproc();
int getppidex(int);
int has_makeflags(int);
int /*<<< orphan*/ open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int, ...);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  char buf[PATH_MAX];
  int pid = getpidproc();
  if (has_makeflags(pid) != 1) {
    fprintf(stderr, "Not running in a make context!\n");
    exit(-1);
  }
  int found = 0;
  while ((pid = getppidex(pid)) >= 0) {
    if (has_makeflags(pid) != 1) {
      found = 1;
      break;
    }
  }
  if (found == 0) {
    fprintf(stderr, "make not found!\n");
    exit(-1);
  }
  if (argc <= 1) {
    snprintf(buf, sizeof(buf), "/proc/%d/fd", pid);
    printf("%s", buf);
    return 0;
  } else {
    snprintf(buf, sizeof(buf), "/proc/%d/fd/%d", pid, 3);
    open(buf, O_RDONLY);
    snprintf(buf, sizeof(buf), "/proc/%d/fd/%d", pid, 4);
    open(buf, O_WRONLY);
    int rc = execvp(argv[1], argv + 1);
    fprintf(stderr, "execve returned! %d\n", rc);
    return -1;
  }
}


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    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getpidproc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <has_makeflags@plt>
cmp    $0x1,%eax
je     1212 <main+0x82>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1080 <getppidex@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jl     124f <main+0xbf>
mov    -0x24(%rbp),%edi
call   1060 <has_makeflags@plt>
cmp    $0x1,%eax
je     124a <main+0xba>
movl   $0x1,-0x28(%rbp)
jmp    124f <main+0xbf>
jmp    1219 <main+0x89>
cmpl   $0x0,-0x28(%rbp)
jne    127a <main+0xea>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xdb7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jg     12c2 <main+0x132>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x24(%rbp),%ecx
lea    0xd99(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rsi
lea    0xd93(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1368 <main+0x1d8>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x24(%rbp),%ecx
lea    0xd6a(%rip),%rdx
mov    $0x3,%r8d
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2cf2(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %eax,%esi
mov    -0x24(%rbp),%ecx
lea    0xd37(%rip),%rdx
mov    $0x4,%r8d
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
call   10a0 <execvp@plt>
mov    %eax,-0x30(%rbp)
mov    0x2c89(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%edx
lea    0xcfc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%r13d
mov    0x2e45(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r12
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
xor    %eax,%eax
call   1050 <getpidproc@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <has_makeflags@plt>
cmp    $0x1,%eax
jne    12eb <main+0x15b>
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1080 <getppidex@plt>
test   %eax,%eax
js     12ca <main+0x13a>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <has_makeflags@plt>
cmp    $0x1,%eax
je     11e0 <main+0x50>
cmp    $0x1,%r13d
jg     1230 <main+0xa0>
lea    0xe27(%rip),%rdx
xor    %r14d,%r14d
mov    %r12,%rdi
mov    %r15d,%esi
mov    %ebx,%ecx
xor    %eax,%eax
call   1040 <snprintf@plt>
lea    0xe1a(%rip),%rdi
mov    %r12,%rsi
call   1030 <printf@plt>
jmp    12b8 <main+0x128>
lea    0xe09(%rip),%r13
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r13,%rdx
mov    %ebx,%ecx
mov    $0x3,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r13,%rdx
mov    %ebx,%ecx
mov    $0x4,%r8d
xor    %eax,%eax
call   1040 <snprintf@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1090 <open@plt>
mov    0x8(%r14),%rdi
add    $0x8,%r14
mov    %r14,%rsi
call   10a0 <execvp@plt>
mov    0x2d30(%rip),%rcx
mov    (%rcx),%edi
lea    0xda6(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
lea    0xd46(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
lea    0xd05(%rip),%rsi
jmp    12da <main+0x14a>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Not running in a make context!\0A\00"
@.str.1 = [17 x i8] c"make not found!\0A\00"
@.str.2 = [12 x i8] c"/proc/%d/fd\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [15 x i8] c"/proc/%d/fd/%d\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.5 = [21 x i8] c"execve returned! %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %6
%15 = alloca i8, i64 %13
store i64 %13, i64* %7
%16 = call i32 (...) @getpidproc()
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = call i32 @has_makeflags(i32 %17)
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %2
store i32 0, i32* %9
br label %25
25: ; preds = %34, %24
%26 = load i32, i32* %8
%27 = call i32 @getppidex(i32 %26)
store i32 %27, i32* %8
%28 = icmp sge i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %25
%30 = load i32, i32* %8
%31 = call i32 @has_makeflags(i32 %30)
%32 = icmp ne i32 %31, 1
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 1, i32* %9
br label %35
34: ; preds = %29
br label %25
35: ; preds = %33, %25
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 -1)
unreachable
42: ; preds = %35
%43 = load i32, i32* %4
%44 = icmp sle i32 %43, 1
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = trunc i64 %13 to i32
%47 = load i32, i32* %8
%48 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %15, i32 %46, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %15)
store i32 0, i32* %3
store i32 1, i32* %10
br label %70
50: ; preds = %42
%51 = trunc i64 %13 to i32
%52 = load i32, i32* %8
%53 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %15, i32 %51, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %52, i32 3)
%54 = load i32, i32* @O_RDONLY
%55 = call i32 @open(i8* %15, i32 %54)
%56 = trunc i64 %13 to i32
%57 = load i32, i32* %8
%58 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %15, i32 %56, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %57, i32 4)
%59 = load i32, i32* @O_WRONLY
%60 = call i32 @open(i8* %15, i32 %59)
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 1
%66 = call i32 @execvp(i8* %63, i8** %65)
store i32 %66, i32* %11
%67 = load i32, i32* @stderr
%68 = load i32, i32* %11
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %68)
store i32 -1, i32* %3
store i32 1, i32* %10
br label %70
70: ; preds = %50, %45
%71 = load i8*, i8** %6
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Not running in a make context!\0A\00"
@.str.1 = [17 x i8] c"make not found!\0A\00"
@.str.2 = [12 x i8] c"/proc/%d/fd\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [15 x i8] c"/proc/%d/fd/%d\00"
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@.str.5 = [21 x i8] c"execve returned! %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i32 (...) @getpidproc()
%7 = tail call i32 @has_makeflags(i32 %6)
%8 = icmp eq i32 %7, 1
br i1 %8, label %13, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %2, %17
%14 = phi i32 [ %15, %17 ], [ %6, %2 ]
%15 = tail call i32 @getppidex(i32 %14)
%16 = icmp sgt i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = tail call i32 @has_makeflags(i32 %15)
%19 = icmp eq i32 %18, 1
br i1 %19, label %13, label %24
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 -1)
unreachable
24: ; preds = %17
%25 = icmp slt i32 %0, 2
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%28 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %41
29: ; preds = %24
%30 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %15, i32 3)
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %5, i32 %31)
%33 = call i32 (i8*, i32, i8*, i32, ...) @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %15, i32 4)
%34 = load i32, i32* @O_WRONLY
%35 = call i32 @open(i8* %5, i32 %34)
%36 = getelementptr inbounds i8*, i8** %1, i64 1
%37 = load i8*, i8** %36
%38 = call i32 @execvp(i8* %37, i8** %36)
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %38)
br label %41
41: ; preds = %29, %26
%42 = phi i32 [ 0, %26 ], [ -1, %29 ]
ret i32 %42
}