#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 scalar_t__ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int kill(scalar_t__, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int wait_program(pid_t pid, const char *pname, int sig) {
  int status;
  int ret = 0;
  if (sig) {
    ret = kill(pid, sig);
    if (ret) {
      fprintf(stderr, "Failed to send signal %d to %s (%lu): %s\n", sig, pname,
              (long)pid, strerror(errno));
      return ret;
    }
    fprintf(stderr, "Kill (%d): %s (%ld)\n", sig, pname, (long)pid);
  }
  waitpid(pid, &status, 0);
  if (WIFEXITED(status)) {
    ret = WEXITSTATUS(status);
    if (ret == 127)
      fprintf(stderr, "Failed to run '%s'\n", pname);
    else
      fprintf(stderr, "Exit (%d): %s\n", ret, pname);
  } else if (WIFSIGNALED(status) && sig && WTERMSIG(status) != sig) {
    fprintf(stderr, "'%s' killed by signal %d\n", pname, WTERMSIG(status));
    ret = -1;
  }
  return ret;
}


x86 assembly (clang, -O0):
<wait_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1239 <wait_program+0xb9>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <kill@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1217 <wait_program+0x97>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    %eax,%r9d
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1324 <wait_program+0x1a4>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    -0x20(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     12b2 <wait_program+0x132>
mov    -0x20(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x7f,-0x24(%rbp)
jne    128f <wait_program+0x10f>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    12ad <wait_program+0x12d>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    -0x18(%rbp),%rcx
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    131e <wait_program+0x19e>
mov    -0x20(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1319 <wait_program+0x199>
cmpl   $0x0,-0x1c(%rbp)
je     1319 <wait_program+0x199>
mov    -0x20(%rbp),%edi
call   1060 <WTERMSIG@plt>
cmp    -0x1c(%rbp),%eax
je     1319 <wait_program+0x199>
mov    0x2cfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rdx
mov    %eax,%ecx
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x24(%rbp)
jmp    131e <wait_program+0x19e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_program>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%rbp
test   %edx,%edx
je     11fc <wait_program+0x7c>
mov    %rbp,%rdi
mov    %ebx,%esi
call   1050 <kill@plt>
mov    %eax,%r15d
mov    0x2e34(%rip),%rax
mov    (%rax),%r12d
test   %r15d,%r15d
je     11e3 <wait_program+0x63>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe37(%rip),%rsi
mov    %r12d,%edi
mov    %ebx,%edx
mov    %r14,%rcx
mov    %rbp,%r8
mov    %eax,%r9d
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    12ae <wait_program+0x12e>
lea    0xe40(%rip),%rsi
mov    %r12d,%edi
mov    %ebx,%edx
mov    %r14,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    0xc(%rsp),%rsi
xor    %r15d,%r15d
mov    %rbp,%rdi
xor    %edx,%edx
call   1080 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     1250 <wait_program+0xd0>
call   1070 <WEXITSTATUS@plt>
mov    %eax,%r15d
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
cmp    $0x7f,%r15d
jne    129a <wait_program+0x11a>
lea    0xe01(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x7f,%r15d
jmp    12ae <wait_program+0x12e>
call   1030 <WIFSIGNALED@plt>
test   %ebx,%ebx
je     12ae <wait_program+0x12e>
test   %rax,%rax
je     12ae <wait_program+0x12e>
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
cmp    %ebx,%eax
je     12ae <wait_program+0x12e>
mov    0x2d6e(%rip),%rax
mov    (%rax),%ebx
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
lea    0xdde(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0xffffffff,%r15d
jmp    12ae <wait_program+0x12e>
lea    0xdb2(%rip),%rsi
mov    %r15d,%edx
mov    %r14,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [42 x i8] c"Failed to send signal %d to %s (%lu): %s\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Kill (%d): %s (%ld)\0A\00"
@.str.2 = [20 x i8] c"Failed to run '%s'\0A\00"
@.str.3 = [15 x i8] c"Exit (%d): %s\0A\00"
@.str.4 = [26 x i8] c"'%s' killed by signal %d\0A\00"
define i32 @wait_program(i64 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i64 %0, i64* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %33
12: ; preds = %3
%13 = load i64, i64* %5
%14 = load i32, i32* %7
%15 = call i32 @kill(i64 %13, i32 %14)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %12
%19 = load i32, i32* @stderr
%20 = load i32, i32* %7
%21 = load i8*, i8** %6
%22 = load i64, i64* %5
%23 = load i32, i32* @errno
%24 = call i32 @strerror(i32 %23)
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %20, i8* %21, i64 %22, i32 %24)
%26 = load i32, i32* %9
store i32 %26, i32* %4
br label %75
27: ; preds = %12
%28 = load i32, i32* @stderr
%29 = load i32, i32* %7
%30 = load i8*, i8** %6
%31 = load i64, i64* %5
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %29, i8* %30, i64 %31)
br label %33
33: ; preds = %27, %3
%34 = load i64, i64* %5
%35 = call i32 @waitpid(i64 %34, i32* %8, i32 0)
%36 = load i32, i32* %8
%37 = call i64 @WIFEXITED(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %54
39: ; preds = %33
%40 = load i32, i32* %8
%41 = call i32 @WEXITSTATUS(i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, 127
br i1 %43, label %44, label %48
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %6
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %53
48: ; preds = %39
%49 = load i32, i32* @stderr
%50 = load i32, i32* %9
%51 = load i8*, i8** %6
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %50, i8* %51)
br label %53
53: ; preds = %48, %44
br label %73
54: ; preds = %33
%55 = load i32, i32* %8
%56 = call i64 @WIFSIGNALED(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %72
58: ; preds = %54
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %72
61: ; preds = %58
%62 = load i32, i32* %8
%63 = call i32 @WTERMSIG(i32 %62)
%64 = load i32, i32* %7
%65 = icmp ne i32 %63, %64
br i1 %65, label %66, label %72
66: ; preds = %61
%67 = load i32, i32* @stderr
%68 = load i8*, i8** %6
%69 = load i32, i32* %8
%70 = call i32 @WTERMSIG(i32 %69)
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %68, i32 %70)
store i32 -1, i32* %9
br label %72
72: ; preds = %66, %61, %58, %54
br label %73
73: ; preds = %72, %53
%74 = load i32, i32* %9
store i32 %74, i32* %4
br label %75
75: ; preds = %73, %18
%76 = load i32, i32* %4
ret i32 %76
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [42 x i8] c"Failed to send signal %d to %s (%lu): %s\0A\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"Kill (%d): %s (%ld)\0A\00"
@.str.2 = [20 x i8] c"Failed to run '%s'\0A\00"
@.str.3 = [15 x i8] c"Exit (%d): %s\0A\00"
@.str.4 = [26 x i8] c"'%s' killed by signal %d\0A\00"
define i32 @wait_program(i64 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = icmp ne i32 %2, 0
br i1 %6, label %7, label %17
7: ; preds = %3
%8 = tail call i32 @kill(i64 %0, i32 %2)
%9 = icmp eq i32 %8, 0
%10 = load i32, i32* @stderr
br i1 %9, label %15, label %11
11: ; preds = %7
%12 = load i32, i32* @errno
%13 = tail call i32 @strerror(i32 %12)
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %2, i8* %1, i64 %0, i32 %13)
br label %44
15: ; preds = %7
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %2, i8* %1, i64 %0)
br label %17
17: ; preds = %15, %3
%18 = call i32 @waitpid(i64 %0, i32* %4, i32 0)
%19 = load i32, i32* %4
%20 = call i64 @WIFEXITED(i32 %19)
%21 = icmp eq i64 %20, 0
%22 = load i32, i32* %4
br i1 %21, label %31, label %23
23: ; preds = %17
%24 = call i32 @WEXITSTATUS(i32 %22)
%25 = icmp eq i32 %24, 127
%26 = load i32, i32* @stderr
br i1 %25, label %27, label %29
27: ; preds = %23
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %44
29: ; preds = %23
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %24, i8* %1)
br label %44
31: ; preds = %17
%32 = call i64 @WIFSIGNALED(i32 %22)
%33 = icmp ne i64 %32, 0
%34 = and i1 %6, %33
br i1 %34, label %35, label %44
35: ; preds = %31
%36 = load i32, i32* %4
%37 = call i32 @WTERMSIG(i32 %36)
%38 = icmp eq i32 %37, %2
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = load i32, i32* %4
%42 = call i32 @WTERMSIG(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32 %42)
br label %44
44: ; preds = %29, %27, %39, %35, %31, %11
%45 = phi i32 [ %8, %11 ], [ 127, %27 ], [ %24, %29 ], [ -1, %39 ], [ 0, %35 ], [ 0, %31 ]
ret i32 %45
}