#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 WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void superviseloop(char **cmdv, int verbose) {
  int pid, status;
  while (1) {
    if (verbose)
      fprintf(stderr, "Started %s\n", cmdv[0]);
    switch (pid = fork()) {
    case -1:
      fprintf(stderr, "Fork failed: %s\n", strerror(errno));
      break;
    case 0:
      execvp(cmdv[0], cmdv);
      fprintf(stderr, "Exec failed: %s\n", strerror(errno));
      exit(1);
    default:
      waitpid(pid, &status, 0);
      if (verbose && WIFEXITED(status) && WEXITSTATUS(status))
        fprintf(stderr, "%s exited abnormally with status %d\n", cmdv[0],
                WEXITSTATUS(status));
      else if (verbose && WIFSIGNALED(status))
        fprintf(stderr, "%s was terminated by signal %d\n", cmdv[0],
                WTERMSIG(status));
    }
  }
}


x86 assembly (clang, -O0):
<superviseloop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11d7 <superviseloop+0x37>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
sub    $0xffffffff,%eax
je     1204 <superviseloop+0x64>
jmp    11f4 <superviseloop+0x54>
mov    -0x18(%rbp),%eax
test   %eax,%eax
je     1237 <superviseloop+0x97>
jmp    127f <superviseloop+0xdf>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    134f <superviseloop+0x1af>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <execvp@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmpl   $0x0,-0xc(%rbp)
je     12f4 <superviseloop+0x154>
mov    -0x14(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%eax
je     12f4 <superviseloop+0x154>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     12f4 <superviseloop+0x154>
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xd46(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    134a <superviseloop+0x1aa>
cmpl   $0x0,-0xc(%rbp)
je     1345 <superviseloop+0x1a5>
mov    -0x14(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%eax
je     1345 <superviseloop+0x1a5>
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xd15(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    134a <superviseloop+0x1aa>
jmp    134f <superviseloop+0x1af>
jmp    11af <superviseloop+0xf>

x86 assembly (clang, -O2):
<superviseloop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
mov    0x2e29(%rip),%r12
lea    0x4(%rsp),%rbx
jmp    11e6 <superviseloop+0x46>
xchg   %ax,%ax
mov    (%r12),%r13d
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %r13d,%edi
lea    0xe30(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %ebp,%ebp
je     1200 <superviseloop+0x60>
mov    (%r12),%edi
mov    (%r15),%rdx
lea    0xe08(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
nop
xor    %eax,%eax
call   10c0 <fork@plt>
cmp    $0xffffffff,%eax
je     11c0 <superviseloop+0x20>
test   %eax,%eax
je     129c <superviseloop+0xfc>
mov    %eax,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
test   %ebp,%ebp
je     1200 <superviseloop+0x60>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
je     1260 <superviseloop+0xc0>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     1260 <superviseloop+0xc0>
mov    (%r12),%r13d
mov    (%r15),%r14
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %r13d,%edi
lea    0xdd6(%rip),%rsi
mov    %r14,%rdx
jmp    128e <superviseloop+0xee>
nopl   (%rax)
mov    0x4(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %eax,%eax
je     11e6 <superviseloop+0x46>
mov    (%r12),%r14d
mov    (%r15),%r13
mov    0x4(%rsp),%edi
call   1050 <WTERMSIG@plt>
mov    %r14d,%edi
lea    0xdc8(%rip),%rsi
mov    %r13,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11e6 <superviseloop+0x46>
mov    (%r15),%rdi
mov    %r15,%rsi
call   1080 <execvp@plt>
mov    (%r12),%ebx
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd5d(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [12 x i8] c"Started %s\0A\00"
@.str.1 = [17 x i8] c"Fork failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"Exec failed: %s\0A\00"
@.str.3 = [37 x i8] c"%s exited abnormally with status %d\0A\00"
@.str.4 = [32 x i8] c"%s was terminated by signal %d\0A\00"
define void @superviseloop(i8** %0, i32 %1) {
%3 = alloca i8**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8** %0, i8*** %3
store i32 %1, i32* %4
br label %7
7: ; preds = %2, %72
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %3
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %10, %7
%17 = call i32 (...) @fork()
store i32 %17, i32* %5
switch i32 %17, label %34 [
i32 -1, label %18
i32 0, label %23
]
18: ; preds = %16
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %72
23: ; preds = %16
%24 = load i8**, i8*** %3
%25 = getelementptr inbounds i8*, i8** %24, i64 0
%26 = load i8*, i8** %25
%27 = load i8**, i8*** %3
%28 = call i32 @execvp(i8* %26, i8** %27)
%29 = load i32, i32* @stderr
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %16
%35 = load i32, i32* %5
%36 = call i32 @waitpid(i32 %35, i32* %6, i32 0)
%37 = load i32, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %55
39: ; preds = %34
%40 = load i32, i32* %6
%41 = call i32 @WIFEXITED(i32 %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %55
43: ; preds = %39
%44 = load i32, i32* %6
%45 = call i32 @WEXITSTATUS(i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %55
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = load i8**, i8*** %3
%50 = getelementptr inbounds i8*, i8** %49, i64 0
%51 = load i8*, i8** %50
%52 = load i32, i32* %6
%53 = call i32 @WEXITSTATUS(i32 %52)
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %51, i32 %53)
br label %71
55: ; preds = %43, %39, %34
%56 = load i32, i32* %4
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %70
58: ; preds = %55
%59 = load i32, i32* %6
%60 = call i32 @WIFSIGNALED(i32 %59)
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %70
62: ; preds = %58
%63 = load i32, i32* @stderr
%64 = load i8**, i8*** %3
%65 = getelementptr inbounds i8*, i8** %64, i64 0
%66 = load i8*, i8** %65
%67 = load i32, i32* %6
%68 = call i32 @WTERMSIG(i32 %67)
%69 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %66, i32 %68)
br label %70
70: ; preds = %62, %58, %55
br label %71
71: ; preds = %70, %47
br label %72
72: ; preds = %71, %18
br label %7
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"Started %s\0A\00"
@.str.1 = [17 x i8] c"Fork failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [17 x i8] c"Exec failed: %s\0A\00"
@.str.3 = [37 x i8] c"%s exited abnormally with status %d\0A\00"
@.str.4 = [32 x i8] c"%s was terminated by signal %d\0A\00"
define void @superviseloop(i8** %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp eq i32 %1, 0
br label %6
6: ; preds = %43, %2
br i1 %5, label %11, label %7
7: ; preds = %6
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %0
%10 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %6, %7
br label %12
12: ; preds = %11, %27
%13 = call i32 (...) @fork()
switch i32 %13, label %27 [
i32 -1, label %14
i32 0, label %19
]
14: ; preds = %12
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %43
19: ; preds = %12
%20 = load i8*, i8** %0
%21 = call i32 @execvp(i8* %20, i8** %0)
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %12
%28 = call i32 @waitpid(i32 %13, i32* %3, i32 0)
br i1 %5, label %12, label %29
29: ; preds = %27
%30 = load i32, i32* %3
%31 = call i32 @WIFEXITED(i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %44, label %33
33: ; preds = %29
%34 = load i32, i32* %3
%35 = call i32 @WEXITSTATUS(i32 %34)
%36 = icmp eq i32 %35, 0
br i1 %36, label %44, label %37
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %0
%40 = load i32, i32* %3
%41 = call i32 @WEXITSTATUS(i32 %40)
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i8* %39, i32 %41)
br label %43
43: ; preds = %37, %48, %44, %14
br label %6
44: ; preds = %29, %33
%45 = load i32, i32* %3
%46 = call i32 @WIFSIGNALED(i32 %45)
%47 = icmp eq i32 %46, 0
br i1 %47, label %43, label %48
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = load i8*, i8** %0
%51 = load i32, i32* %3
%52 = call i32 @WTERMSIG(i32 %51)
%53 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %50, i32 %52)
br label %43
}