#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 */
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int /*<<< orphan*/ WSTOPSIG(int);
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ delete_job(scalar_t__);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
char *strsignal(int /*<<< orphan*/);
int waitpid(scalar_t__, int *, int /*<<< orphan*/);
void wait_for_job(pid_t pgid) {
  int status;
  int ret = waitpid(-pgid, &status, WUNTRACED);
  if (ret == -1) {
    perror("waitpid");
    return;
  }
  if (WIFSTOPPED(status)) {
    printf("Job %d stopped by signal: %s\n", (int)pgid,
           strsignal(WSTOPSIG(status)));
  } else if (WIFSIGNALED(status)) {
    printf("Job %d terminated by signal: %s\n", (int)pgid,
           strsignal(WTERMSIG(status)));
    delete_job(pgid);
  } else {
    delete_job(pgid);
  }
}


x86 assembly (clang, -O0):
<wait_for_job>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
xor    %eax,%eax
mov    %eax,%edi
sub    -0x8(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1080 <waitpid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11d4 <wait_for_job+0x44>
lea    0xe36(%rip),%rdi
call   1090 <perror@plt>
jmp    1269 <wait_for_job+0xd9>
mov    -0xc(%rbp),%edi
call   1050 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1213 <wait_for_job+0x83>
mov    -0x8(%rbp),%rax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   10b0 <WSTOPSIG@plt>
mov    %eax,%edi
call   1070 <strsignal@plt>
mov    -0x14(%rbp),%esi
mov    %rax,%rdx
lea    0xdff(%rip),%rdi
call   1030 <printf@plt>
jmp    1269 <wait_for_job+0xd9>
mov    -0xc(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     125b <wait_for_job+0xcb>
mov    -0x8(%rbp),%rax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%edi
call   1070 <strsignal@plt>
mov    -0x18(%rbp),%esi
mov    %rax,%rdx
lea    0xdde(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <delete_job@plt>
jmp    1264 <wait_for_job+0xd4>
mov    -0x8(%rbp),%rdi
call   10a0 <delete_job@plt>
jmp    1269 <wait_for_job+0xd9>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_job>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
neg    %rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
call   1080 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1221 <wait_for_job+0x91>
mov    0xc(%rsp),%edi
call   1050 <WIFSTOPPED@plt>
test   %rax,%rax
je     11e4 <wait_for_job+0x54>
mov    0xc(%rsp),%edi
call   10b0 <WSTOPSIG@plt>
mov    %eax,%edi
call   1070 <strsignal@plt>
lea    0xe30(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1030 <printf@plt>
jmp    121b <wait_for_job+0x8b>
mov    0xc(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     1213 <wait_for_job+0x83>
mov    0xc(%rsp),%edi
call   1060 <WTERMSIG@plt>
mov    %eax,%edi
call   1070 <strsignal@plt>
lea    0xe1d(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1030 <printf@plt>
mov    %rbx,%rdi
call   10a0 <delete_job@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdd8(%rip),%rdi
call   1090 <perror@plt>
jmp    121b <wait_for_job+0x8b>

LLVM IR (-O0):
@WUNTRACED = global i32 0
@.str = [8 x i8] c"waitpid\00"
@.str.1 = [30 x i8] c"Job %d stopped by signal: %s\0A\00"
@.str.2 = [33 x i8] c"Job %d terminated by signal: %s\0A\00"
define void @wait_for_job(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = sub nsw i64 0, %5
%7 = load i32, i32* @WUNTRACED
%8 = call i32 @waitpid(i64 %6, i32* %3, i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %41
13: ; preds = %1
%14 = load i32, i32* %3
%15 = call i64 @WIFSTOPPED(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %13
%18 = load i64, i64* %2
%19 = trunc i64 %18 to i32
%20 = load i32, i32* %3
%21 = call i32 @WSTOPSIG(i32 %20)
%22 = call i8* @strsignal(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %22)
br label %41
24: ; preds = %13
%25 = load i32, i32* %3
%26 = call i64 @WIFSIGNALED(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %24
%29 = load i64, i64* %2
%30 = trunc i64 %29 to i32
%31 = load i32, i32* %3
%32 = call i32 @WTERMSIG(i32 %31)
%33 = call i8* @strsignal(i32 %32)
%34 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %30, i8* %33)
%35 = load i64, i64* %2
%36 = call i32 @delete_job(i64 %35)
br label %40
37: ; preds = %24
%38 = load i64, i64* %2
%39 = call i32 @delete_job(i64 %38)
br label %40
40: ; preds = %37, %28
br label %41
41: ; preds = %11, %40, %17
ret void
}

LLVM IR (-O2):
@WUNTRACED = global i32 0
@.str = [8 x i8] c"waitpid\00"
@.str.1 = [30 x i8] c"Job %d stopped by signal: %s\0A\00"
@.str.2 = [33 x i8] c"Job %d terminated by signal: %s\0A\00"
define void @wait_for_job(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = sub nsw i64 0, %0
%5 = load i32, i32* @WUNTRACED
%6 = call i32 @waitpid(i64 %4, i32* %2, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %33
10: ; preds = %1
%11 = load i32, i32* %2
%12 = call i64 @WIFSTOPPED(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %20, label %14
14: ; preds = %10
%15 = trunc i64 %0 to i32
%16 = load i32, i32* %2
%17 = call i32 @WSTOPSIG(i32 %16)
%18 = call i8* @strsignal(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %18)
br label %33
20: ; preds = %10
%21 = load i32, i32* %2
%22 = call i64 @WIFSIGNALED(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %31, label %24
24: ; preds = %20
%25 = trunc i64 %0 to i32
%26 = load i32, i32* %2
%27 = call i32 @WTERMSIG(i32 %26)
%28 = call i8* @strsignal(i32 %27)
%29 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %25, i8* %28)
%30 = call i32 @delete_job(i64 %0)
br label %33
31: ; preds = %20
%32 = call i32 @delete_job(i64 %0)
br label %33
33: ; preds = %14, %31, %24, %8
ret void
}