#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 /*<<< orphan*/ err_ret(char *);
int /*<<< orphan*/ printf(char *, ...);
int waitpid(int, int *, int /*<<< orphan*/);
void sig_chld(int signo) {
  printf("caught SIGCHLD\n");
  int status = 0, ret = 0;
  ret = waitpid(-1, &status, 0);
  if (ret < 0)
    err_ret("waitpid error");
  else
    printf("wait a child with status %d\n", status);
}


x86 assembly (clang, -O0):
<sig_chld>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xebe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1185 <sig_chld+0x55>
lea    0xe95(%rip),%rdi
call   1050 <err_ret@plt>
jmp    1196 <sig_chld+0x66>
mov    -0x8(%rbp),%esi
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_chld>:
push   %rax
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %eax,%eax
js     117e <sig_chld+0x3e>
mov    0x4(%rsp),%esi
lea    0xe99(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
pop    %rax
ret
lea    0xe7b(%rip),%rdi
call   1060 <err_ret@plt>
pop    %rax
ret

LLVM IR (-O0):
@.str = [16 x i8] c"caught SIGCHLD\0A\00"
@.str.1 = [14 x i8] c"waitpid error\00"
@.str.2 = [29 x i8] c"wait a child with status %d\0A\00"
define void @sig_chld(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
store i32 0, i32* %4
%6 = call i32 @waitpid(i32 -1, i32* %3, i32 0)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @err_ret(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %14
11: ; preds = %1
%12 = load i32, i32* %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %11, %9
ret void
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"waitpid error\00"
@.str.2 = [29 x i8] c"wait a child with status %d\0A\00"
@str = [15 x i8] c"caught SIGCHLD\00"
define void @sig_chld(i32 %0) {
%2 = alloca i32
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = call i32 @waitpid(i32 -1, i32* %2, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @err_ret(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %12
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %9, %7
ret void
}