#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 SIGINT;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
int WTERMSIG(int);
int /*<<< orphan*/ printf(char *, scalar_t__, ...);
int /*<<< orphan*/ save_cmd_status(int);
char *strsignal(int);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
void waitpid_bg_cmds(void) {
  pid_t wpid;
  int status;
  do {
    wpid = waitpid(-1, &status, WNOHANG);
    if (wpid == (pid_t)-1) {
      break;
    } else if (wpid == 0) {
      break;
    } else if (WIFEXITED(status)) {
      status = WEXITSTATUS(status);
      printf("[1] + %d done\n", wpid);
    } else if (WIFSIGNALED(status)) {
      status = WTERMSIG(status);
      if (status != SIGINT)
        printf("[1] + %d killed by %s\n", wpid, strsignal(status));
    } else {
      status = 1;
    }
  } while (1);
  save_cmd_status(status);
}


x86 assembly (clang, -O0):
<waitpid_bg_cmds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0xc(%rbp),%rsi
call   1090 <waitpid@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0xffffffffffffffff,-0x8(%rbp)
jne    11b3 <waitpid_bg_cmds+0x33>
jmp    127a <waitpid_bg_cmds+0xfa>
cmpq   $0x0,-0x8(%rbp)
jne    11c3 <waitpid_bg_cmds+0x43>
jmp    127a <waitpid_bg_cmds+0xfa>
mov    -0xc(%rbp),%edi
call   10a0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11f7 <waitpid_bg_cmds+0x77>
mov    -0xc(%rbp),%edi
call   1070 <WEXITSTATUS@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125c <waitpid_bg_cmds+0xdc>
mov    -0xc(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1250 <waitpid_bg_cmds+0xd0>
mov    -0xc(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2dca(%rip),%rcx
cmp    (%rcx),%eax
je     124b <waitpid_bg_cmds+0xcb>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <strsignal@plt>
mov    -0x18(%rbp),%rsi
mov    %rax,%rdx
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1257 <waitpid_bg_cmds+0xd7>
movl   $0x1,-0xc(%rbp)
jmp    125c <waitpid_bg_cmds+0xdc>
jmp    1261 <waitpid_bg_cmds+0xe1>
jmp    1266 <waitpid_bg_cmds+0xe6>
jmp    126b <waitpid_bg_cmds+0xeb>
mov    $0x1,%al
test   $0x1,%al
jne    1188 <waitpid_bg_cmds+0x8>
jmp    127a <waitpid_bg_cmds+0xfa>
mov    -0xc(%rbp),%edi
call   1080 <save_cmd_status@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitpid_bg_cmds>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e4e(%rip),%r13
mov    0x0(%r13),%edx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
call   1090 <waitpid@plt>
mov    %rax,%rbx
add    $0x1,%rax
cmp    $0x2,%rax
jae    11ca <waitpid_bg_cmds+0x4a>
mov    0x4(%rsp),%edi
call   1080 <save_cmd_status@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e17(%rip),%rbp
lea    0xe37(%rip),%r14
lea    0x4(%rsp),%r12
lea    0xe1c(%rip),%r15
jmp    1224 <waitpid_bg_cmds+0xa4>
cs nopw 0x0(%rax,%rax,1)
call   1070 <WEXITSTATUS@plt>
mov    %eax,0x4(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x0(%r13),%edx
mov    $0xffffffff,%edi
mov    %r12,%rsi
call   1090 <waitpid@plt>
mov    %rax,%rbx
add    $0x1,%rax
cmp    $0x1,%rax
jbe    11b2 <waitpid_bg_cmds+0x32>
mov    0x4(%rsp),%edi
call   10a0 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
jne    11f0 <waitpid_bg_cmds+0x70>
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     126b <waitpid_bg_cmds+0xeb>
mov    0x4(%rsp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,0x4(%rsp)
cmp    0x0(%rbp),%eax
je     1206 <waitpid_bg_cmds+0x86>
mov    %eax,%edi
call   1060 <strsignal@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1206 <waitpid_bg_cmds+0x86>
movl   $0x1,0x4(%rsp)
jmp    1206 <waitpid_bg_cmds+0x86>

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [15 x i8] c"[1] + %d done\0A\00"
@SIGINT = global i32 0
@.str.1 = [23 x i8] c"[1] + %d killed by %s\0A\00"
define void @waitpid_bg_cmds() {
%1 = alloca i64
%2 = alloca i32
br label %3
3: ; preds = %43, %0
%4 = load i32, i32* @WNOHANG
%5 = call i64 @waitpid(i32 -1, i32* %2, i32 %4)
store i64 %5, i64* %1
%6 = load i64, i64* %1
%7 = icmp eq i64 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %3
br label %44
9: ; preds = %3
%10 = load i64, i64* %1
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %9
br label %44
13: ; preds = %9
%14 = load i32, i32* %2
%15 = call i64 @WIFEXITED(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = load i32, i32* %2
%19 = call i32 @WEXITSTATUS(i32 %18)
store i32 %19, i32* %2
%20 = load i64, i64* %1
%21 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 %20)
br label %40
22: ; preds = %13
%23 = load i32, i32* %2
%24 = call i64 @WIFSIGNALED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = load i32, i32* %2
%28 = call i32 @WTERMSIG(i32 %27)
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = load i32, i32* @SIGINT
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i64, i64* %1
%34 = load i32, i32* %2
%35 = call i8* @strsignal(i32 %34)
%36 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %33, i8* %35)
br label %37
37: ; preds = %32, %26
br label %39
38: ; preds = %22
store i32 1, i32* %2
br label %39
39: ; preds = %38, %37
br label %40
40: ; preds = %39, %17
br label %41
41: ; preds = %40
br label %42
42: ; preds = %41
br label %43
43: ; preds = %42
br i1 true, label %3, label %44
44: ; preds = %43, %12, %8
%45 = load i32, i32* %2
%46 = call i32 @save_cmd_status(i32 %45)
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [15 x i8] c"[1] + %d done\0A\00"
@SIGINT = global i32 0
@.str.1 = [23 x i8] c"[1] + %d killed by %s\0A\00"
define void @waitpid_bg_cmds() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @WNOHANG
%4 = call i64 @waitpid(i32 -1, i32* %1, i32 %3)
%5 = add i64 %4, 1
%6 = icmp ult i64 %5, 2
br i1 %6, label %33, label %7
7: ; preds = %0, %28
%8 = phi i64 [ %30, %28 ], [ %4, %0 ]
%9 = load i32, i32* %1
%10 = call i64 @WIFEXITED(i32 %9)
%11 = icmp eq i64 %10, 0
%12 = load i32, i32* %1
br i1 %11, label %16, label %13
13: ; preds = %7
%14 = call i32 @WEXITSTATUS(i32 %12)
store i32 %14, i32* %1
%15 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 %8)
br label %28
16: ; preds = %7
%17 = call i64 @WIFSIGNALED(i32 %12)
%18 = icmp eq i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = load i32, i32* %1
%21 = call i32 @WTERMSIG(i32 %20)
store i32 %21, i32* %1
%22 = load i32, i32* @SIGINT
%23 = icmp eq i32 %21, %22
br i1 %23, label %28, label %24
24: ; preds = %19
%25 = call i8* @strsignal(i32 %21)
%26 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i64 %8, i8* %25)
br label %28
27: ; preds = %16
store i32 1, i32* %1
br label %28
28: ; preds = %13, %19, %24, %27
%29 = load i32, i32* @WNOHANG
%30 = call i64 @waitpid(i32 -1, i32* %1, i32 %29)
%31 = add i64 %30, 1
%32 = icmp ult i64 %31, 2
br i1 %32, label %33, label %7
33: ; preds = %28, %0
%34 = load i32, i32* %1
%35 = call i32 @save_cmd_status(i32 %34)
ret void
}