#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 */
struct stat {
  int dummy;
};
typedef int pid_t;

/* Variables and functions */
int /*<<< orphan*/ SIGTERM;
int execvp(char *, char **);
int fork();
int getppid();
int /*<<< orphan*/ kill(int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsid();
int /*<<< orphan*/ sleep(int);
int stat(char *, struct stat *);
int main(int argc, char **argv) {
  pid_t pid, ppid, pgid;
  int res;
  struct stat buf;
  char *waitfile;
  pgid = setsid();
  if (pgid == -1) {
    perror("pgid");
    return -1;
  }
  pid = fork();
  if (pid == 0) {
    waitfile = argv[1];
    while (1) {
      if ((res = stat(waitfile, &buf)) == -1) {
        sleep(1);
      } else {
        break;
      }
    }
    ppid = getppid();
    kill(-1 * pgid, SIGTERM);
    return 0;
  } else {
    if (execvp(argv[2], &argv[2]) == -1) {
      perror("execvp");
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1030 <setsid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c2 <main+0x42>
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <main+0x101>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    123f <main+0xbf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    120a <main+0x8a>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    120f <main+0x8f>
jmp    1214 <main+0x94>
jmp    11e2 <main+0x62>
mov    $0x0,%al
call   1080 <getppid@plt>
mov    %eax,-0x18(%rbp)
imul   $0xffffffff,-0x1c(%rbp),%edi
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
movl   $0x0,-0x4(%rbp)
jmp    1281 <main+0x101>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x10(%rbp),%rsi
add    $0x10,%rsi
call   1070 <execvp@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
lea    0xda1(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <main+0x101>
jmp    127a <main+0xfa>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbp
xor    %eax,%eax
call   1030 <setsid@plt>
cmp    $0xffffffff,%eax
je     121a <main+0x9a>
mov    %eax,%r15d
xor    %r14d,%r14d
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11c9 <main+0x49>
mov    0x10(%rbp),%rdi
add    $0x10,%rbp
mov    %rbp,%rsi
call   1070 <execvp@plt>
cmp    $0xffffffff,%eax
jne    122c <main+0xac>
lea    0xe3e(%rip),%rdi
jmp    1221 <main+0xa1>
mov    0x8(%rbp),%rbp
mov    %rsp,%rsi
mov    %rbp,%rdi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    11fa <main+0x7a>
mov    %rsp,%rbx
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x60>
xor    %r14d,%r14d
xor    %eax,%eax
call   1080 <getppid@plt>
neg    %r15d
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1050 <kill@plt>
jmp    122c <main+0xac>
lea    0xddf(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [5 x i8] c"pgid\00"
@SIGTERM = global i32 0
@.str.1 = [7 x i8] c"execvp\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
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (...) @setsid()
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
17: ; preds = %2
%18 = call i32 (...) @fork()
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %39
21: ; preds = %17
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %11
br label %25
25: ; preds = %21, %32
%26 = load i8*, i8** %11
%27 = call i32 @stat(i8* %26, %struct.stat* %10)
store i32 %27, i32* %9
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @sleep(i32 1)
br label %32
31: ; preds = %25
br label %33
32: ; preds = %29
br label %25
33: ; preds = %31
%34 = call i32 (...) @getppid()
store i32 %34, i32* %7
%35 = load i32, i32* %8
%36 = mul nsw i32 -1, %35
%37 = load i32, i32* @SIGTERM
%38 = call i32 @kill(i32 %36, i32 %37)
store i32 0, i32* %3
br label %51
39: ; preds = %17
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 2
%45 = call i32 @execvp(i8* %42, i8** %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %51
49: ; preds = %39
br label %50
50: ; preds = %49
store i32 0, i32* %3
br label %51
51: ; preds = %50, %47, %33, %15
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [5 x i8] c"pgid\00"
@SIGTERM = global i32 0
@.str.1 = [7 x i8] c"execvp\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 (...) @setsid()
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %33
9: ; preds = %2
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %26
12: ; preds = %9
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @stat(i8* %14, %struct.stat* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %12, %17
%18 = tail call i32 @sleep(i32 1)
%19 = call i32 @stat(i8* %14, %struct.stat* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %17, label %21
21: ; preds = %17, %12
%22 = tail call i32 (...) @getppid()
%23 = sub nsw i32 0, %5
%24 = load i32, i32* @SIGTERM
%25 = tail call i32 @kill(i32 %23, i32 %24)
br label %33
26: ; preds = %9
%27 = getelementptr inbounds i8*, i8** %1, i64 2
%28 = load i8*, i8** %27
%29 = tail call i32 @execvp(i8* %28, i8** %27)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %26, %31, %21, %7
%34 = phi i32 [ -1, %7 ], [ 0, %21 ], [ -1, %31 ], [ 0, %26 ]
ret i32 %34
}