#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 sigaction {
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_mask;
  int /*<<< orphan*/ sa_handler;
};
typedef int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIG_IGN;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WSTOPSIG(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sigaction(int /*<<< orphan*/, struct sigaction *,
                             int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int wait4(int, int *, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int pid, wpid;
  int status;
  sigset_t ss;
  struct sigaction sa;
  if (pid = fork()) {
    while (!(wpid = wait4(pid, &status, 0, 0))) {
      fprintf(stderr, "parent: continuing to wait...\n");
      sleep(3);
    }
    if (wpid == pid) {
      fprintf(stderr, "parent: waited on kid\n");
      fprintf(stderr, "parent: status = 0x%x\n", status);
      if (WIFSIGNALED(status))
        fprintf(stderr, "parent: signaled with %d\n", WTERMSIG(status));
      else if (WIFEXITED(status))
        fprintf(stderr, "parent: exited with status %d\n", WEXITSTATUS(status));
      else if (WIFSTOPPED(status))
        fprintf(stderr, "parent: stopped with %d\n", WSTOPSIG(status));
      else
        fprintf(stderr, "parent: bad status\n");
      exit(0);
    } else {
      perror("wait4 failed");
      exit(1);
    }
  } else {
    fprintf(stderr, "child: I'm here\n");
    fprintf(stderr, "ignoring SIGTERM\n");
    sa.sa_handler = SIG_IGN;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGTERM, &sa, NULL);
    sleep(10);
    fprintf(stderr, "child: exiting\n");
    exit(7);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
je     1391 <main+0x1b1>
jmp    120e <main+0x2e>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1090 <wait4@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1236 <main+0x56>
jmp    125c <main+0x7c>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
jmp    120e <main+0x2e>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    137b <main+0x19b>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     12d7 <main+0xf7>
mov    0x2d1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%edx
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1374 <main+0x194>
mov    -0x1c(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1315 <main+0x135>
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    -0x38(%rbp),%edi
mov    %eax,%edx
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    136f <main+0x18f>
mov    -0x1c(%rbp),%edi
call   1070 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1353 <main+0x173>
mov    0x2ca2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
call   10f0 <WSTOPSIG@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%edx
lea    0xd3f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    136a <main+0x18a>
mov    0x2c76(%rip),%rax
mov    (%rax),%edi
lea    0xd3c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    136f <main+0x18f>
jmp    1374 <main+0x194>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd31(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2c38(%rip),%rax
mov    (%rax),%edi
lea    0xd1f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c21(%rip),%rax
mov    (%rax),%edi
lea    0xd19(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,-0x30(%rbp)
mov    0x2c01(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigaction@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    0x2bc9(%rip),%rax
mov    (%rax),%edi
lea    0xcd3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x7,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jne    127a <main+0x9a>
mov    0x2dcf(%rip),%rbx
mov    (%rbx),%edi
lea    0xeb6(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xeb7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdi
call   1060 <sigemptyset@plt>
movq   $0x0,0x8(%rsp)
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigaction@plt>
mov    $0xa,%edi
call   10e0 <sleep@plt>
mov    (%rbx),%edi
lea    0xe7a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x7,%edi
call   10d0 <exit@plt>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <wait4@plt>
test   %eax,%eax
jne    12da <main+0xfa>
mov    0x2d39(%rip),%r12
lea    0xd62(%rip),%r14
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r12),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x3,%edi
call   10e0 <sleep@plt>
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <wait4@plt>
test   %eax,%eax
je     12b0 <main+0xd0>
cmp    %ebx,%eax
jne    134c <main+0x16c>
mov    0x2ceb(%rip),%rbx
mov    (%rbx),%edi
lea    0xd31(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
mov    0x8(%rsp),%edx
lea    0xd34(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x8(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
jne    1362 <main+0x182>
mov    0x8(%rsp),%edi
call   10c0 <WIFEXITED@plt>
test   %rax,%rax
jne    1376 <main+0x196>
mov    0x8(%rsp),%edi
call   1070 <WIFSTOPPED@plt>
mov    (%rbx),%ebx
test   %rax,%rax
jne    138a <main+0x1aa>
lea    0xd63(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd60(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    (%rbx),%ebx
mov    0x8(%rsp),%edi
call   1080 <WTERMSIG@plt>
lea    0xcd9(%rip),%rsi
jmp    139a <main+0x1ba>
mov    (%rbx),%ebx
mov    0x8(%rsp),%edi
call   10a0 <WEXITSTATUS@plt>
lea    0xcdf(%rip),%rsi
jmp    139a <main+0x1ba>
mov    0x8(%rsp),%edi
call   10f0 <WSTOPSIG@plt>
lea    0xcec(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i64, i32, i32 }
@stderr = global i32 0
@.str = [31 x i8] c"parent: continuing to wait...\0A\00"
@.str.1 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.2 = [23 x i8] c"parent: status = 0x%x\0A\00"
@.str.3 = [26 x i8] c"parent: signaled with %d\0A\00"
@.str.4 = [31 x i8] c"parent: exited with status %d\0A\00"
@.str.5 = [25 x i8] c"parent: stopped with %d\0A\00"
@.str.6 = [20 x i8] c"parent: bad status\0A\00"
@.str.7 = [13 x i8] c"wait4 failed\00"
@.str.8 = [17 x i8] c"child: I'm here\0A\00"
@.str.9 = [18 x i8] c"ignoring SIGTERM\0A\00"
@SIG_IGN = global i32 0
@SIGTERM = global i32 0
@.str.10 = [16 x i8] c"child: exiting\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 %struct.sigaction
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 (...) @fork()
store i32 %11, i32* %6
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %69
13: ; preds = %2
br label %14
14: ; preds = %19, %13
%15 = load i32, i32* %6
%16 = call i32 @wait4(i32 %15, i32* %8, i32 0, i32 0)
store i32 %16, i32* %7
%17 = icmp ne i32 %16, 0
%18 = xor i1 %17, true
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @sleep(i32 3)
br label %14
23: ; preds = %14
%24 = load i32, i32* %7
%25 = load i32, i32* %6
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %66
27: ; preds = %23
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @stderr
%31 = load i32, i32* %8
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %8
%34 = call i64 @WIFSIGNALED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %27
%37 = load i32, i32* @stderr
%38 = load i32, i32* %8
%39 = call i32 @WTERMSIG(i32 %38)
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %39)
br label %64
41: ; preds = %27
%42 = load i32, i32* %8
%43 = call i64 @WIFEXITED(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = load i32, i32* %8
%48 = call i32 @WEXITSTATUS(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %48)
br label %63
50: ; preds = %41
%51 = load i32, i32* %8
%52 = call i64 @WIFSTOPPED(i32 %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = load i32, i32* %8
%57 = call i32 @WSTOPSIG(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %57)
br label %62
59: ; preds = %50
%60 = load i32, i32* @stderr
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %59, %54
br label %63
63: ; preds = %62, %45
br label %64
64: ; preds = %63, %36
%65 = call i32 @exit(i32 0)
unreachable
66: ; preds = %23
%67 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %2
%70 = load i32, i32* @stderr
%71 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%74 = load i32, i32* @SIG_IGN
%75 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 2
store i32 %74, i32* %75
%76 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 1
%77 = call i32 @sigemptyset(i32* %76)
%78 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %10, i32 0, i32 0
store i64 0, i64* %78
%79 = load i32, i32* @SIGTERM
%80 = call i32 @sigaction(i32 %79, %struct.sigaction* %10, i32* null)
%81 = call i32 @sleep(i32 10)
%82 = load i32, i32* @stderr
%83 = call i32 (i32, i8*, ...) @fprintf(i32 %82, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
%84 = call i32 @exit(i32 7)
unreachable
}

LLVM IR (-O2):
%struct.sigaction = type { i64, i32, i32 }
@stderr = global i32 0
@.str = [31 x i8] c"parent: continuing to wait...\0A\00"
@.str.1 = [23 x i8] c"parent: waited on kid\0A\00"
@.str.2 = [23 x i8] c"parent: status = 0x%x\0A\00"
@.str.3 = [26 x i8] c"parent: signaled with %d\0A\00"
@.str.4 = [31 x i8] c"parent: exited with status %d\0A\00"
@.str.5 = [25 x i8] c"parent: stopped with %d\0A\00"
@.str.6 = [20 x i8] c"parent: bad status\0A\00"
@.str.7 = [13 x i8] c"wait4 failed\00"
@.str.8 = [17 x i8] c"child: I'm here\0A\00"
@.str.9 = [18 x i8] c"ignoring SIGTERM\0A\00"
@SIG_IGN = global i32 0
@SIGTERM = global i32 0
@.str.10 = [16 x i8] c"child: exiting\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sigaction
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sigaction* %4 to i8*
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
br i1 %8, label %60, label %9
9: ; preds = %2
%10 = call i32 @wait4(i32 %7, i32* %3, i32 0, i32 0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9, %12
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @sleep(i32 3)
%16 = call i32 @wait4(i32 %7, i32* %3, i32 0, i32 0)
%17 = icmp eq i32 %16, 0
br i1 %17, label %12, label %18
18: ; preds = %12, %9
%19 = phi i32 [ %10, %9 ], [ %16, %12 ]
%20 = icmp eq i32 %19, %7
br i1 %20, label %21, label %57
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @stderr
%25 = load i32, i32* %3
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %3
%28 = call i64 @WIFSIGNALED(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %21
%31 = load i32, i32* @stderr
%32 = load i32, i32* %3
%33 = call i32 @WTERMSIG(i32 %32)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %55
35: ; preds = %21
%36 = load i32, i32* %3
%37 = call i64 @WIFEXITED(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %44, label %39
39: ; preds = %35
%40 = load i32, i32* @stderr
%41 = load i32, i32* %3
%42 = call i32 @WEXITSTATUS(i32 %41)
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %42)
br label %55
44: ; preds = %35
%45 = load i32, i32* %3
%46 = call i64 @WIFSTOPPED(i32 %45)
%47 = icmp eq i64 %46, 0
%48 = load i32, i32* @stderr
br i1 %47, label %53, label %49
49: ; preds = %44
%50 = load i32, i32* %3
%51 = call i32 @WSTOPSIG(i32 %50)
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %51)
br label %55
53: ; preds = %44
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %39, %53, %49, %30
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %18
%58 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %2
%61 = load i32, i32* @stderr
%62 = tail call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%63 = load i32, i32* @stderr
%64 = tail call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%65 = load i32, i32* @SIG_IGN
%66 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 2
store i32 %65, i32* %66
%67 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 1
%68 = call i32 @sigemptyset(i32* %67)
%69 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %4, i64 0, i32 0
store i64 0, i64* %69
%70 = load i32, i32* @SIGTERM
%71 = call i32 @sigaction(i32 %70, %struct.sigaction* %4, i32* null)
%72 = call i32 @sleep(i32 10)
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
%75 = call i32 @exit(i32 7)
unreachable
}