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


x86 assembly (clang, -O0):
<test_setpgid_leader>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
call   10a0 <signal_setup@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1241 <test_setpgid_leader+0x81>
mov    $0x0,%al
call   1060 <setsid@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   10d0 <setpgid@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
call   10b0 <wait_for_signal@plt>
movl   $0x0,-0x4(%rbp)
jmp    12aa <test_setpgid_leader+0xea>
jmp    1246 <test_setpgid_leader+0x86>
mov    -0x10(%rbp),%rdi
call   1050 <getsid@plt>
cmp    -0x10(%rbp),%rax
je     1265 <test_setpgid_leader+0xa5>
mov    $0x0,%al
call   1070 <sched_yield@plt>
jmp    1246 <test_setpgid_leader+0x86>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %edi,%edi
call   10c0 <getpgid@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
call   10d0 <setpgid@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <wait_for_child_exit@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_setpgid_leader>:
push   %r14
push   %rbx
push   %rax
mov    0x2e1d(%rip),%r14
mov    (%r14),%edi
call   10a0 <signal_setup@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
je     1235 <test_setpgid_leader+0x75>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1050 <getsid@plt>
cmp    %rbx,%rax
je     1206 <test_setpgid_leader+0x46>
xor    %eax,%eax
call   1070 <sched_yield@plt>
jmp    11f0 <test_setpgid_leader+0x30>
xor    %edi,%edi
call   10c0 <getpgid@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   10d0 <setpgid@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    (%r14),%esi
mov    %rbx,%rdi
call   1090 <kill@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <wait_for_child_exit@plt>
jmp    126c <test_setpgid_leader+0xac>
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
call   1080 <assert@plt>
xor    %edi,%edi
xor    %esi,%esi
call   10d0 <setpgid@plt>
mov    %eax,%edi
call   1080 <assert@plt>
mov    (%r14),%edi
call   10b0 <wait_for_signal@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SIGUSR1 = global i32 0
define i32 @test_setpgid_leader() {
%1 = alloca i32
%2 = alloca i64
%3 = load i32, i32* @SIGUSR1
%4 = call i32 @signal_setup(i32 %3)
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %0
%9 = call i64 (...) @setsid()
%10 = call i64 (...) @getpid()
%11 = icmp eq i64 %9, %10
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = call i32 @setpgid(i64 0, i32 0)
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* @SIGUSR1
%17 = call i32 @wait_for_signal(i32 %16)
store i32 0, i32* %1
br label %36
18: ; preds = %0
br label %19
19: ; preds = %24, %18
%20 = load i64, i64* %2
%21 = call i64 @getsid(i64 %20)
%22 = load i64, i64* %2
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 (...) @sched_yield()
br label %19
26: ; preds = %19
%27 = load i64, i64* %2
%28 = call i32 @getpgid(i32 0)
%29 = call i32 @setpgid(i64 %27, i32 %28)
%30 = call i32 @assert(i32 %29)
%31 = load i64, i64* %2
%32 = load i32, i32* @SIGUSR1
%33 = call i32 @kill(i64 %31, i32 %32)
%34 = load i64, i64* %2
%35 = call i32 @wait_for_child_exit(i64 %34, i32 0)
store i32 0, i32* %1
br label %36
36: ; preds = %26, %8
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@SIGUSR1 = global i32 0
define i32 @test_setpgid_leader() {
%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 %8, label %5
5: ; preds = %0
%6 = tail call i64 @getsid(i64 %3)
%7 = icmp eq i64 %6, %3
br i1 %7, label %22, label %18
8: ; preds = %0
%9 = tail call i64 (...) @setsid()
%10 = tail call i64 (...) @getpid()
%11 = icmp eq i64 %9, %10
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = tail call i32 @setpgid(i64 0, i32 0)
%15 = tail call i32 @assert(i32 %14)
%16 = load i32, i32* @SIGUSR1
%17 = tail call i32 @wait_for_signal(i32 %16)
br label %29
18: ; preds = %5, %18
%19 = tail call i32 (...) @sched_yield()
%20 = tail call i64 @getsid(i64 %3)
%21 = icmp eq i64 %20, %3
br i1 %21, label %22, label %18
22: ; preds = %18, %5
%23 = tail call i32 @getpgid(i32 0)
%24 = tail call i32 @setpgid(i64 %3, i32 %23)
%25 = tail call i32 @assert(i32 %24)
%26 = load i32, i32* @SIGUSR1
%27 = tail call i32 @kill(i64 %3, i32 %26)
%28 = tail call i32 @wait_for_child_exit(i64 %3, i32 0)
br label %29
29: ; preds = %22, %8
ret i32 0
}