#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 /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ assert(int);
scalar_t__ fork();
scalar_t__ getpgid(scalar_t__);
scalar_t__ getpid();
scalar_t__ getppid();
scalar_t__ getsid(scalar_t__);
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ sched_yield();
int setpgid(scalar_t__, scalar_t__);
scalar_t__ setsid();
int /*<<< orphan*/ signal_setup(int /*<<< orphan*/);
int /*<<< orphan*/ wait_for_child_exit(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ wait_for_signal(int /*<<< orphan*/);
int test_setpgid_child(void) {
  signal_setup(SIGUSR1);
  pid_t cpid1 = fork();
  if (cpid1 == 0) {
    assert(setsid() == getpid());
    wait_for_signal(SIGUSR1);
    return 0;
  }
  pid_t cpid2 = fork();
  if (cpid2 == 0) {
    kill(getppid(), SIGUSR1);
    assert(setpgid(getppid(), 0));
    wait_for_signal(SIGUSR1);
    return 0;
  }
  while (getsid(cpid1) != cpid1)
    sched_yield();
  wait_for_signal(SIGUSR1);
  assert(!setpgid(cpid2, cpid2));
  assert(getpgid(cpid2) == cpid2);
  assert(!setpgid(cpid2, getpgid(0)));
  assert(getpgid(cpid2) == getpgid(0));
  assert(setpgid(cpid2, cpid1));
  assert(getpgid(cpid2) == getpgid(0));
  kill(cpid1, SIGUSR1);
  kill(cpid2, SIGUSR1);
  wait_for_child_exit(cpid1, 0);
  wait_for_child_exit(cpid2, 0);
  return 0;
}


x86 assembly (clang, -O0):
<test_setpgid_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   10a0 <signal_setup@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    123f <test_setpgid_child+0x6f>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   10b0 <wait_for_signal@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ea <test_setpgid_child+0x21a>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12a1 <test_setpgid_child+0xd1>
mov    $0x0,%al
call   10e0 <getppid@plt>
mov    %rax,%rdi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    $0x0,%al
call   10e0 <getppid@plt>
mov    %rax,%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10d0 <setpgid@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10b0 <wait_for_signal@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ea <test_setpgid_child+0x21a>
jmp    12a6 <test_setpgid_child+0xd6>
mov    -0x10(%rbp),%rdi
call   1050 <getsid@plt>
cmp    -0x10(%rbp),%rax
je     12c5 <test_setpgid_child+0xf5>
mov    $0x0,%al
call   1070 <sched_yield@plt>
jmp    12a6 <test_setpgid_child+0xd6>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10b0 <wait_for_signal@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10d0 <setpgid@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <getpgid@plt>
cmp    -0x18(%rbp),%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <getpgid@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   10d0 <setpgid@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <getpgid@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <getpgid@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10d0 <setpgid@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <getpgid@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <getpgid@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2c34(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    -0x18(%rbp),%rdi
mov    0x2c22(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <wait_for_child_exit@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1030 <wait_for_child_exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_setpgid_child>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e09(%rip),%r12
mov    (%r12),%edi
call   10a0 <signal_setup@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     12fc <test_setpgid_child+0x12c>
mov    %rax,%rbx
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     1318 <test_setpgid_child+0x148>
mov    %rax,%r14
xchg   %ax,%ax
mov    %rbx,%rdi
call   1050 <getsid@plt>
cmp    %rbx,%rax
je     1226 <test_setpgid_child+0x56>
xor    %eax,%eax
call   1070 <sched_yield@plt>
jmp    1210 <test_setpgid_child+0x40>
mov    (%r12),%edi
call   10b0 <wait_for_signal@plt>
mov    %r14,%rdi
mov    %r14,%rsi
call   10d0 <setpgid@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1080 <assert@plt>
mov    %r14,%rdi
call   10c0 <getpgid@plt>
xor    %edi,%edi
cmp    %r14,%rax
sete   %dil
call   1080 <assert@plt>
xor    %edi,%edi
call   10c0 <getpgid@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   10d0 <setpgid@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1080 <assert@plt>
mov    %r14,%rdi
call   10c0 <getpgid@plt>
mov    %rax,%r15
xor    %edi,%edi
call   10c0 <getpgid@plt>
xor    %edi,%edi
cmp    %rax,%r15
sete   %dil
call   1080 <assert@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10d0 <setpgid@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    %r14,%rdi
call   10c0 <getpgid@plt>
mov    %rax,%r15
xor    %edi,%edi
call   10c0 <getpgid@plt>
xor    %edi,%edi
cmp    %rax,%r15
sete   %dil
call   1080 <assert@plt>
mov    (%r12),%esi
mov    %rbx,%rdi
call   1090 <kill@plt>
mov    (%r12),%esi
mov    %r14,%rdi
call   1090 <kill@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <wait_for_child_exit@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <wait_for_child_exit@plt>
jmp    134c <test_setpgid_child+0x17c>
xor    %eax,%eax
call   1060 <setsid@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
xor    %edi,%edi
cmp    %rax,%rbx
sete   %dil
jmp    133e <test_setpgid_child+0x16e>
xor    %eax,%eax
call   10e0 <getppid@plt>
mov    (%r12),%esi
mov    %rax,%rdi
call   1090 <kill@plt>
xor    %eax,%eax
call   10e0 <getppid@plt>
mov    %rax,%rdi
xor    %esi,%esi
call   10d0 <setpgid@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    (%r12),%edi
call   10b0 <wait_for_signal@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SIGUSR1 = global i32 0
define i32 @test_setpgid_child() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = load i32, i32* @SIGUSR1
%5 = call i32 @signal_setup(i32 %4)
%6 = call i64 (...) @fork()
store i64 %6, i64* %2
%7 = load i64, i64* %2
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %0
%10 = call i64 (...) @setsid()
%11 = call i64 (...) @getpid()
%12 = icmp eq i64 %10, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* @SIGUSR1
%16 = call i32 @wait_for_signal(i32 %15)
store i32 0, i32* %1
br label %87
17: ; preds = %0
%18 = call i64 (...) @fork()
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = call i64 (...) @getppid()
%23 = load i32, i32* @SIGUSR1
%24 = call i32 @kill(i64 %22, i32 %23)
%25 = call i64 (...) @getppid()
%26 = call i32 @setpgid(i64 %25, i64 0)
%27 = call i32 @assert(i32 %26)
%28 = load i32, i32* @SIGUSR1
%29 = call i32 @wait_for_signal(i32 %28)
store i32 0, i32* %1
br label %87
30: ; preds = %17
br label %31
31: ; preds = %36, %30
%32 = load i64, i64* %2
%33 = call i64 @getsid(i64 %32)
%34 = load i64, i64* %2
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 (...) @sched_yield()
br label %31
38: ; preds = %31
%39 = load i32, i32* @SIGUSR1
%40 = call i32 @wait_for_signal(i32 %39)
%41 = load i64, i64* %3
%42 = load i64, i64* %3
%43 = call i32 @setpgid(i64 %41, i64 %42)
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
%46 = zext i1 %45 to i32
%47 = call i32 @assert(i32 %46)
%48 = load i64, i64* %3
%49 = call i64 @getpgid(i64 %48)
%50 = load i64, i64* %3
%51 = icmp eq i64 %49, %50
%52 = zext i1 %51 to i32
%53 = call i32 @assert(i32 %52)
%54 = load i64, i64* %3
%55 = call i64 @getpgid(i64 0)
%56 = call i32 @setpgid(i64 %54, i64 %55)
%57 = icmp ne i32 %56, 0
%58 = xor i1 %57, true
%59 = zext i1 %58 to i32
%60 = call i32 @assert(i32 %59)
%61 = load i64, i64* %3
%62 = call i64 @getpgid(i64 %61)
%63 = call i64 @getpgid(i64 0)
%64 = icmp eq i64 %62, %63
%65 = zext i1 %64 to i32
%66 = call i32 @assert(i32 %65)
%67 = load i64, i64* %3
%68 = load i64, i64* %2
%69 = call i32 @setpgid(i64 %67, i64 %68)
%70 = call i32 @assert(i32 %69)
%71 = load i64, i64* %3
%72 = call i64 @getpgid(i64 %71)
%73 = call i64 @getpgid(i64 0)
%74 = icmp eq i64 %72, %73
%75 = zext i1 %74 to i32
%76 = call i32 @assert(i32 %75)
%77 = load i64, i64* %2
%78 = load i32, i32* @SIGUSR1
%79 = call i32 @kill(i64 %77, i32 %78)
%80 = load i64, i64* %3
%81 = load i32, i32* @SIGUSR1
%82 = call i32 @kill(i64 %80, i32 %81)
%83 = load i64, i64* %2
%84 = call i32 @wait_for_child_exit(i64 %83, i32 0)
%85 = load i64, i64* %3
%86 = call i32 @wait_for_child_exit(i64 %85, i32 0)
store i32 0, i32* %1
br label %87
87: ; preds = %38, %21, %9
%88 = load i32, i32* %1
ret i32 %88
}

LLVM IR (-O2):
@SIGUSR1 = global i32 0
define i32 @test_setpgid_child() {
%1 = load i32, i32* @SIGUSR1
%2 = tail call i32 @signal_setup(i32 %1)
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %13
5: ; preds = %0
%6 = tail call i64 (...) @setsid()
%7 = tail call i64 (...) @getpid()
%8 = icmp eq i64 %6, %7
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = load i32, i32* @SIGUSR1
%12 = tail call i32 @wait_for_signal(i32 %11)
br label %66
13: ; preds = %0
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i64 @getsid(i64 %3)
%18 = icmp eq i64 %17, %3
br i1 %18, label %32, label %28
19: ; preds = %13
%20 = tail call i64 (...) @getppid()
%21 = load i32, i32* @SIGUSR1
%22 = tail call i32 @kill(i64 %20, i32 %21)
%23 = tail call i64 (...) @getppid()
%24 = tail call i32 @setpgid(i64 %23, i64 0)
%25 = tail call i32 @assert(i32 %24)
%26 = load i32, i32* @SIGUSR1
%27 = tail call i32 @wait_for_signal(i32 %26)
br label %66
28: ; preds = %16, %28
%29 = tail call i32 (...) @sched_yield()
%30 = tail call i64 @getsid(i64 %3)
%31 = icmp eq i64 %30, %3
br i1 %31, label %32, label %28
32: ; preds = %28, %16
%33 = load i32, i32* @SIGUSR1
%34 = tail call i32 @wait_for_signal(i32 %33)
%35 = tail call i32 @setpgid(i64 %14, i64 %14)
%36 = icmp eq i32 %35, 0
%37 = zext i1 %36 to i32
%38 = tail call i32 @assert(i32 %37)
%39 = tail call i64 @getpgid(i64 %14)
%40 = icmp eq i64 %39, %14
%41 = zext i1 %40 to i32
%42 = tail call i32 @assert(i32 %41)
%43 = tail call i64 @getpgid(i64 0)
%44 = tail call i32 @setpgid(i64 %14, i64 %43)
%45 = icmp eq i32 %44, 0
%46 = zext i1 %45 to i32
%47 = tail call i32 @assert(i32 %46)
%48 = tail call i64 @getpgid(i64 %14)
%49 = tail call i64 @getpgid(i64 0)
%50 = icmp eq i64 %48, %49
%51 = zext i1 %50 to i32
%52 = tail call i32 @assert(i32 %51)
%53 = tail call i32 @setpgid(i64 %14, i64 %3)
%54 = tail call i32 @assert(i32 %53)
%55 = tail call i64 @getpgid(i64 %14)
%56 = tail call i64 @getpgid(i64 0)
%57 = icmp eq i64 %55, %56
%58 = zext i1 %57 to i32
%59 = tail call i32 @assert(i32 %58)
%60 = load i32, i32* @SIGUSR1
%61 = tail call i32 @kill(i64 %3, i32 %60)
%62 = load i32, i32* @SIGUSR1
%63 = tail call i32 @kill(i64 %14, i32 %62)
%64 = tail call i32 @wait_for_child_exit(i64 %3, i32 0)
%65 = tail call i32 @wait_for_child_exit(i64 %14, i32 0)
br label %66
66: ; preds = %19, %32, %5
ret i32 0
}