#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*/ SIGINT;
int /*<<< orphan*/ SIGTTOU;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ do_exec();
int fork();
int getpid();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setpgid(int, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ tcsetpgrp(int /*<<< orphan*/, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int c;
  int status;
  pid_t mypid = getpid();
  int args = 1;
  int loop = 0;
  while (args < argc) {
    if (strcmp("--loop", argv[args]) == 0) {
      loop = 1;
    }
    args++;
  }
  if (!loop)
    do_exec();
  signal(SIGINT, SIG_IGN);
  do {
    signal(SIGTTOU, SIG_DFL);
    c = fork();
    if (c < 0) {
      printf("ERROR: exec failed\n");
      sleep(5);
      return -1;
    }
    if (c == 0) {
      pid_t pid = getpid();
      setpgid(pid, pid);
      do_exec();
    } else {
      tcsetpgrp(0, c);
      waitpid(c, &status, 0);
      signal(SIGTTOU, SIG_IGN);
      tcsetpgrp(0, mypid);
      sleep(1);
    }
  } while (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   1040 <getpid@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1211 <main+0x71>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe13(%rip),%rdi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1203 <main+0x63>
movl   $0x1,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ce <main+0x2e>
cmpl   $0x0,-0x24(%rbp)
jne    1222 <main+0x82>
mov    $0x0,%al
call   1030 <do_exec@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1286 <main+0xe6>
lea    0xd9c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130f <main+0x16f>
cmpl   $0x0,-0x14(%rbp)
jne    12b1 <main+0x111>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x28(%rbp),%esi
call   10a0 <setpgid@plt>
mov    $0x0,%al
call   1030 <do_exec@plt>
jmp    12f4 <main+0x154>
mov    -0x14(%rbp),%esi
xor    %edi,%edi
call   1090 <tcsetpgrp@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    -0x1c(%rbp),%esi
xor    %edi,%edi
call   1090 <tcsetpgrp@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
jmp    12f9 <main+0x159>
mov    $0x1,%al
test   $0x1,%al
jne    1239 <main+0x99>
jmp    1308 <main+0x168>
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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%r14d
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%r15d
cmp    $0x2,%r14d
jl     1204 <main+0x64>
mov    %r15d,(%rsp)
mov    %r14d,%r13d
mov    $0x1,%ebx
lea    0xe2a(%rip),%r14
mov    $0x1,%r15d
nopl   0x0(%rax)
mov    (%r12,%rbx,8),%rsi
mov    %r14,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
cmove  %r15d,%ebp
add    $0x1,%rbx
cmp    %rbx,%r13
jne    11e0 <main+0x40>
test   %ebp,%ebp
mov    (%rsp),%r15d
jne    120b <main+0x6b>
xor    %eax,%eax
call   1040 <do_exec@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    0x2dbd(%rip),%r12
mov    (%r12),%esi
call   1070 <signal@plt>
mov    0x2dbd(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d93(%rip),%r13
mov    0x0(%r13),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     12cb <main+0x12b>
mov    %eax,%ebx
lea    0x4(%rsp),%r14
jmp    12ae <main+0x10e>
cs nopw 0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %ebx,%esi
call   1090 <tcsetpgrp@plt>
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0x0(%rbp),%edi
mov    (%r12),%esi
call   1070 <signal@plt>
xor    %edi,%edi
mov    %r15d,%esi
call   1090 <tcsetpgrp@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    0x0(%rbp),%edi
mov    0x0(%r13),%esi
call   1070 <signal@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12cb <main+0x12b>
test   %ebx,%ebx
jne    1260 <main+0xc0>
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
mov    %eax,%esi
call   10a0 <setpgid@plt>
xor    %eax,%eax
call   1040 <do_exec@plt>
jmp    1295 <main+0xf5>
lea    0xd35(%rip),%rdi
call   1030 <puts@plt>
mov    $0x5,%edi
call   10b0 <sleep@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"--loop\00"
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGTTOU = global i32 0
@SIG_DFL = global i32 0
@.str.1 = [20 x i8] c"ERROR: exec failed\0A\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 i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (...) @getpid()
store i32 %12, i32* %8
store i32 1, i32* %9
store i32 0, i32* %10
br label %13
13: ; preds = %26, %2
%14 = load i32, i32* %9
%15 = load i32, i32* %4
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = load i8**, i8*** %5
%19 = load i32, i32* %9
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %18, i64 %20
%22 = load i8*, i8** %21
%23 = call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %17
store i32 1, i32* %10
br label %26
26: ; preds = %25, %17
%27 = load i32, i32* %9
%28 = add nsw i32 %27, 1
store i32 %28, i32* %9
br label %13
29: ; preds = %13
%30 = load i32, i32* %10
%31 = icmp ne i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = call i32 (...) @do_exec()
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* @SIGINT
%36 = load i32, i32* @SIG_IGN
%37 = call i32 @signal(i32 %35, i32 %36)
br label %38
38: ; preds = %69, %34
%39 = load i32, i32* @SIGTTOU
%40 = load i32, i32* @SIG_DFL
%41 = call i32 @signal(i32 %39, i32 %40)
%42 = call i32 (...) @fork()
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%47 = call i32 @sleep(i32 5)
store i32 -1, i32* %3
br label %71
48: ; preds = %38
%49 = load i32, i32* %6
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = call i32 (...) @getpid()
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = load i32, i32* %11
%55 = call i32 @setpgid(i32 %53, i32 %54)
%56 = call i32 (...) @do_exec()
br label %68
57: ; preds = %48
%58 = load i32, i32* %6
%59 = call i32 @tcsetpgrp(i32 0, i32 %58)
%60 = load i32, i32* %6
%61 = call i32 @waitpid(i32 %60, i32* %7, i32 0)
%62 = load i32, i32* @SIGTTOU
%63 = load i32, i32* @SIG_IGN
%64 = call i32 @signal(i32 %62, i32 %63)
%65 = load i32, i32* %8
%66 = call i32 @tcsetpgrp(i32 0, i32 %65)
%67 = call i32 @sleep(i32 1)
br label %68
68: ; preds = %57, %51
br label %69
69: ; preds = %68
br i1 true, label %38, label %70
70: ; preds = %69
store i32 0, i32* %3
br label %71
71: ; preds = %70, %45
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@.str = [7 x i8] c"--loop\00"
@SIGINT = global i32 0
@SIG_IGN = global i32 0
@SIGTTOU = global i32 0
@SIG_DFL = global i32 0
@str = [19 x i8] c"ERROR: exec failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 (...) @getpid()
%6 = icmp sgt i32 %0, 1
br i1 %6, label %7, label %21
7: ; preds = %2
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 1, %7 ], [ %17, %9 ]
%11 = phi i32 [ 0, %7 ], [ %16, %9 ]
%12 = getelementptr inbounds i8*, i8** %1, i64 %10
%13 = load i8*, i8** %12
%14 = tail call i64 @strcmp(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i32 1, i32 %11
%17 = add nuw nsw i64 %10, 1
%18 = icmp eq i64 %17, %8
br i1 %18, label %19, label %9
19: ; preds = %9
%20 = icmp eq i32 %16, 0
br i1 %20, label %21, label %23
21: ; preds = %2, %19
%22 = tail call i32 (...) @do_exec()
br label %23
23: ; preds = %21, %19
%24 = load i32, i32* @SIGINT
%25 = load i32, i32* @SIG_IGN
%26 = tail call i32 @signal(i32 %24, i32 %25)
%27 = load i32, i32* @SIGTTOU
%28 = load i32, i32* @SIG_DFL
%29 = call i32 @signal(i32 %27, i32 %28)
%30 = call i32 (...) @fork()
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %50, %23
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%34 = call i32 @sleep(i32 5)
ret i32 -1
35: ; preds = %23, %50
%36 = phi i32 [ %54, %50 ], [ %30, %23 ]
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = call i32 (...) @getpid()
%40 = call i32 @setpgid(i32 %39, i32 %39)
%41 = call i32 (...) @do_exec()
br label %50
42: ; preds = %35
%43 = call i32 @tcsetpgrp(i32 0, i32 %36)
%44 = call i32 @waitpid(i32 %36, i32* %3, i32 0)
%45 = load i32, i32* @SIGTTOU
%46 = load i32, i32* @SIG_IGN
%47 = call i32 @signal(i32 %45, i32 %46)
%48 = call i32 @tcsetpgrp(i32 0, i32 %5)
%49 = call i32 @sleep(i32 1)
br label %50
50: ; preds = %38, %42
%51 = load i32, i32* @SIGTTOU
%52 = load i32, i32* @SIG_DFL
%53 = call i32 @signal(i32 %51, i32 %52)
%54 = call i32 (...) @fork()
%55 = icmp slt i32 %54, 0
br i1 %55, label %32, label %35
}