#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 signalfd_siginfo {
  int ssi_signo;
  scalar_t__ ssi_pid;
  int ssi_status;
};
typedef int ssize_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int EXIT_FAILURE;
scalar_t__ SIGCHLD;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
int WTERMSIG(int);
int /*<<< orphan*/ _err_exit(char *, int, int /*<<< orphan*/, char *);
int close(int);
int /*<<< orphan*/ disable_core();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ kill(scalar_t__, scalar_t__);
int /*<<< orphan*/ raise(int);
int read(int, struct signalfd_siginfo *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ unblock_all_signals();
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
void wait_for_exit(int sfd, pid_t pid1) {
  int status = 0;
  int signal = 0;
  int ret = 0;
  int rc;
  for (;;) {
    struct signalfd_siginfo sig;
    ssize_t s;
    s = read(sfd, &sig, sizeof(struct signalfd_siginfo));
    if (s != sizeof(struct signalfd_siginfo))
      _err_exit("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                "outputs/2022-01-22/02-19-57/repos/Morgan-Stanley/"
                "treadmill-pid1/refs/heads/main/src/pid1.c",
                231, errno, "signalfd read");
    fprintf(stderr, "Received signal %d from %ld\n", sig.ssi_signo,
            (long)sig.ssi_pid);
    if ((sig.ssi_signo == SIGCHLD) && (sig.ssi_pid == pid1)) {
      status = sig.ssi_status;
      if (waitpid(pid1, NULL, WNOHANG) != pid1) {
        continue;
      }
      break;
    } else {
      kill(pid1, sig.ssi_signo);
    }
  }
  rc = close(sfd);
  if (rc != 0)
    _err_exit("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/Morgan-Stanley/treadmill-pid1/refs/"
              "heads/main/src/pid1.c",
              259, errno, "signalfd close");
  unblock_all_signals();
  if (WIFEXITED(status)) {
    ret = WEXITSTATUS(status);
    fprintf(stderr, "child has exited: %d\n", ret);
    exit(ret);
  } else if (WIFSIGNALED(status)) {
    signal = WTERMSIG(status);
    fprintf(stderr, "child terminated with signal: %d\n", signal);
    disable_core();
    raise(signal);
  } else {
    fprintf(stderr, "Unexpected child status: %d\n", status);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<wait_for_exit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x18,%edx
call   1070 <read@plt>
mov    %eax,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cmp    $0x18,%rax
je     1247 <wait_for_exit+0x67>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
lea    0xdca(%rip),%rdi
mov    $0xe7,%esi
lea    0xe51(%rip),%rcx
call   1060 <_err_exit@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%edx
mov    -0x30(%rbp),%rcx
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movslq -0x38(%rbp),%rax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%rax
jne    12b7 <wait_for_exit+0xd7>
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    12b7 <wait_for_exit+0xd7>
mov    -0x28(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10e0 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     12b2 <wait_for_exit+0xd2>
jmp    1204 <wait_for_exit+0x24>
jmp    12c9 <wait_for_exit+0xe9>
mov    -0x10(%rbp),%rdi
movslq -0x38(%rbp),%rsi
call   10a0 <kill@plt>
jmp    1204 <wait_for_exit+0x24>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     12ff <wait_for_exit+0x11f>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edx
lea    0xd12(%rip),%rdi
mov    $0x103,%esi
lea    0xdc4(%rip),%rcx
call   1060 <_err_exit@plt>
mov    $0x0,%al
call   10c0 <unblock_all_signals@plt>
mov    -0x14(%rbp),%edi
call   10f0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1345 <wait_for_exit+0x165>
mov    -0x14(%rbp),%edi
call   10d0 <WEXITSTATUS@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xd97(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1100 <exit@plt>
mov    -0x14(%rbp),%edi
call   1080 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1390 <wait_for_exit+0x1b0>
mov    -0x14(%rbp),%edi
call   10b0 <WTERMSIG@plt>
mov    %eax,-0x18(%rbp)
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    $0x0,%al
call   1040 <disable_core@plt>
mov    -0x18(%rbp),%edi
call   1030 <raise@plt>
jmp    13b8 <wait_for_exit+0x1d8>
mov    0x2c39(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xd62(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    0x2c0f(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>
jmp    13bd <wait_for_exit+0x1dd>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_exit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %rsp,%r14
lea    0xea3(%rip),%r15
jmp    1208 <wait_for_exit+0x28>
mov    %rbx,%rdi
call   10a0 <kill@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x18,%edx
call   1070 <read@plt>
cmp    $0x18,%eax
je     123e <wait_for_exit+0x5e>
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
lea    0xdd3(%rip),%rdi
mov    $0xe7,%esi
lea    0xe5a(%rip),%rcx
call   1060 <_err_exit@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%ebp
mov    0x8(%rsp),%r13
mov    %r15,%rsi
mov    %ebp,%edx
mov    %r13,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
movslq %ebp,%rsi
cmp    %rbx,%r13
jne    1200 <wait_for_exit+0x20>
mov    0x2d73(%rip),%rax
cmp    %rsi,(%rax)
jne    1200 <wait_for_exit+0x20>
mov    0x10(%rsp),%ebp
mov    0x2d6b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10e0 <waitpid@plt>
cmp    %rbx,%rax
jne    1208 <wait_for_exit+0x28>
mov    %r12d,%edi
call   1050 <close@plt>
test   %eax,%eax
je     12bf <wait_for_exit+0xdf>
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
lea    0xd52(%rip),%rdi
lea    0xe09(%rip),%rcx
mov    $0x103,%esi
call   1060 <_err_exit@plt>
xor    %eax,%eax
call   10c0 <unblock_all_signals@plt>
mov    %ebp,%edi
call   10f0 <WIFEXITED@plt>
test   %rax,%rax
jne    131c <wait_for_exit+0x13c>
mov    %ebp,%edi
call   1080 <WIFSIGNALED@plt>
test   %rax,%rax
je     1345 <wait_for_exit+0x165>
mov    %ebp,%edi
call   10b0 <WTERMSIG@plt>
mov    %eax,%ebx
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %eax,%eax
call   1040 <disable_core@plt>
mov    %ebx,%edi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <raise@plt>
mov    %ebp,%edi
call   10d0 <WEXITSTATUS@plt>
mov    %eax,%ebx
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %ebx,%edi
call   1100 <exit@plt>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    0x2c5b(%rip),%rax
mov    (%rax),%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.signalfd_siginfo = type { i32, i64, i32 }
@.str = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Morgan-Stanley/treadmill-pid1/refs/heads/main/src/pid1.c\00"
@errno = global i32 0
@.str.1 = [14 x i8] c"signalfd read\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Received signal %d from %ld\0A\00"
@SIGCHLD = global i64 0
@WNOHANG = global i32 0
@.str.3 = [15 x i8] c"signalfd close\00"
@.str.4 = [22 x i8] c"child has exited: %d\0A\00"
@.str.5 = [34 x i8] c"child terminated with signal: %d\0A\00"
@.str.6 = [29 x i8] c"Unexpected child status: %d\0A\00"
@EXIT_FAILURE = global i32 0
define void @wait_for_exit(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.signalfd_siginfo
%10 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
br label %11
11: ; preds = %53, %45, %2
%12 = load i32, i32* %3
%13 = call i32 @read(i32 %12, %struct.signalfd_siginfo* %9, i32 24)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = sext i32 %14 to i64
%16 = icmp ne i64 %15, 24
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = call i32 @_err_exit(i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str, i64 0, i64 0), i32 231, i32 %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %17, %11
%21 = load i32, i32* @stderr
%22 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %9, i32 0, i32 0
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %9, i32 0, i32 1
%25 = load i64, i64* %24
%26 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %23, i64 %25)
%27 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %9, i32 0, i32 0
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = load i64, i64* @SIGCHLD
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %47
32: ; preds = %20
%33 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %9, i32 0, i32 1
%34 = load i64, i64* %33
%35 = load i64, i64* %4
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %47
37: ; preds = %32
%38 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %9, i32 0, i32 2
%39 = load i32, i32* %38
store i32 %39, i32* %5
%40 = load i64, i64* %4
%41 = load i32, i32* @WNOHANG
%42 = call i64 @waitpid(i64 %40, i32* null, i32 %41)
%43 = load i64, i64* %4
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %46
45: ; preds = %37
br label %11
46: ; preds = %37
br label %54
47: ; preds = %32, %20
%48 = load i64, i64* %4
%49 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %9, i32 0, i32 0
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = call i32 @kill(i64 %48, i64 %51)
br label %53
53: ; preds = %47
br label %11
54: ; preds = %46
%55 = load i32, i32* %3
%56 = call i32 @close(i32 %55)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %54
%60 = load i32, i32* @errno
%61 = call i32 @_err_exit(i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str, i64 0, i64 0), i32 259, i32 %60, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %59, %54
%63 = call i32 (...) @unblock_all_signals()
%64 = load i32, i32* %5
%65 = call i64 @WIFEXITED(i32 %64)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %75
67: ; preds = %62
%68 = load i32, i32* %5
%69 = call i32 @WEXITSTATUS(i32 %68)
store i32 %69, i32* %7
%70 = load i32, i32* @stderr
%71 = load i32, i32* %7
%72 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %71)
%73 = load i32, i32* %7
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %62
%76 = load i32, i32* %5
%77 = call i64 @WIFSIGNALED(i32 %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %88
79: ; preds = %75
%80 = load i32, i32* %5
%81 = call i32 @WTERMSIG(i32 %80)
store i32 %81, i32* %6
%82 = load i32, i32* @stderr
%83 = load i32, i32* %6
%84 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %83)
%85 = call i32 (...) @disable_core()
%86 = load i32, i32* %6
%87 = call i32 @raise(i32 %86)
br label %94
88: ; preds = %75
%89 = load i32, i32* @stderr
%90 = load i32, i32* %5
%91 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %89, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %90)
%92 = load i32, i32* @EXIT_FAILURE
%93 = call i32 @exit(i32 %92)
unreachable
94: ; preds = %79
br label %95
95: ; preds = %94
ret void
}

LLVM IR (-O2):
%struct.signalfd_siginfo = type { i32, i64, i32 }
@.str = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/Morgan-Stanley/treadmill-pid1/refs/heads/main/src/pid1.c\00"
@errno = global i32 0
@.str.1 = [14 x i8] c"signalfd read\00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"Received signal %d from %ld\0A\00"
@SIGCHLD = global i64 0
@WNOHANG = global i32 0
@.str.3 = [15 x i8] c"signalfd close\00"
@.str.4 = [22 x i8] c"child has exited: %d\0A\00"
@.str.5 = [34 x i8] c"child terminated with signal: %d\0A\00"
@.str.6 = [29 x i8] c"Unexpected child status: %d\0A\00"
@EXIT_FAILURE = global i32 0
define void @wait_for_exit(i32 %0, i64 %1) {
%3 = alloca %struct.signalfd_siginfo
%4 = bitcast %struct.signalfd_siginfo* %3 to i8*
%5 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %3, i64 0, i32 0
%6 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %3, i64 0, i32 1
%7 = getelementptr inbounds %struct.signalfd_siginfo, %struct.signalfd_siginfo* %3, i64 0, i32 2
br label %8
8: ; preds = %31, %2
%9 = call i32 @read(i32 %0, %struct.signalfd_siginfo* %3, i32 24)
%10 = icmp eq i32 %9, 24
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = tail call i32 @_err_exit(i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str, i64 0, i64 0), i32 231, i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %11, %8
%15 = load i32, i32* @stderr
%16 = load i32, i32* %5
%17 = load i64, i64* %6
%18 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %16, i64 %17)
%19 = sext i32 %16 to i64
%20 = load i64, i64* @SIGCHLD
%21 = icmp eq i64 %20, %19
%22 = icmp eq i64 %17, %1
%23 = and i1 %22, %21
br i1 %23, label %24, label %29
24: ; preds = %14
%25 = load i32, i32* %7
%26 = load i32, i32* @WNOHANG
%27 = tail call i64 @waitpid(i64 %1, i32* null, i32 %26)
%28 = icmp eq i64 %27, %1
br i1 %28, label %32, label %31
29: ; preds = %14
%30 = tail call i32 @kill(i64 %1, i64 %19)
br label %31
31: ; preds = %24, %29
br label %8
32: ; preds = %24
%33 = tail call i32 @close(i32 %0)
%34 = icmp eq i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = load i32, i32* @errno
%37 = tail call i32 @_err_exit(i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str, i64 0, i64 0), i32 259, i32 %36, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %35, %32
%39 = tail call i32 (...) @unblock_all_signals()
%40 = tail call i64 @WIFEXITED(i32 %25)
%41 = icmp eq i64 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %38
%43 = tail call i32 @WEXITSTATUS(i32 %25)
%44 = load i32, i32* @stderr
%45 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%46 = tail call i32 @exit(i32 %43)
unreachable
47: ; preds = %38
%48 = tail call i64 @WIFSIGNALED(i32 %25)
%49 = icmp eq i64 %48, 0
br i1 %49, label %56, label %50
50: ; preds = %47
%51 = tail call i32 @WTERMSIG(i32 %25)
%52 = load i32, i32* @stderr
%53 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i32 %51)
%54 = tail call i32 (...) @disable_core()
%55 = tail call i32 @raise(i32 %51)
ret void
56: ; preds = %47
%57 = load i32, i32* @stderr
%58 = tail call i32 (i32, i8*, i32, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %25)
%59 = load i32, i32* @EXIT_FAILURE
%60 = tail call i32 @exit(i32 %59)
unreachable
}