#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 */
struct sigaction {
  int /*<<< orphan*/ sa_handler;
  scalar_t__ sa_flags;
  int /*<<< orphan*/ sa_mask;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ ExecvHelper(char *, char const **);
int /*<<< orphan*/ LogErrno(char *);
int /*<<< orphan*/ SIGUSR1;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ setsid();
scalar_t__ sigaction(int /*<<< orphan*/, struct sigaction *,
                     int /*<<< orphan*/ *);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
void StartPgrp(void) {
  if (setsid() == (pid_t)-1) {
    LogErrno("setsid");
  }
  pid_t pid = fork();
  if (pid == -1) {
    LogErrno("StartPgrp -> fork; expect potential race in KillPgrp");
  } else if (pid == 0) {
    struct sigaction sa;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sa.sa_handler = SIG_IGN;
    if (sigaction(SIGUSR1, &sa, NULL) != 0) {
      LogErrno("sigaction(SIGUSR1)");
    }
    {
      const char *args[2] = {"pgrp_placeholder", NULL};
      ExecvHelper("pgrp_placeholder", args);
      sleep(2);
      _exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<StartPgrp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    $0x0,%al
call   1060 <setsid@plt>
cmp    $0xffffffffffffffff,%rax
jne    11a5 <StartPgrp+0x25>
lea    0xe60(%rip),%rdi
call   1050 <LogErrno@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0xffffffffffffffff,-0x8(%rbp)
jne    11cc <StartPgrp+0x4c>
lea    0xe45(%rip),%rdi
call   1050 <LogErrno@plt>
jmp    1267 <StartPgrp+0xe7>
cmpq   $0x0,-0x8(%rbp)
jne    1262 <StartPgrp+0xe2>
lea    -0x20(%rbp),%rdi
add    $0x10,%rdi
call   1070 <sigemptyset@plt>
movq   $0x0,-0x18(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigaction@plt>
cmp    $0x0,%rax
je     1224 <StartPgrp+0xa4>
lea    0xe1d(%rip),%rdi
call   1050 <LogErrno@plt>
mov    0x2bc5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2bc2(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x30(%rbp),%rsi
lea    0xe0a(%rip),%rdi
call   1080 <ExecvHelper@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
jmp    1267 <StartPgrp+0xe7>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<StartPgrp>:
sub    $0x38,%rsp
xor    %eax,%eax
call   1060 <setsid@plt>
cmp    $0xffffffffffffffff,%rax
jne    119d <StartPgrp+0x1d>
lea    0xe68(%rip),%rdi
call   1050 <LogErrno@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
cmp    $0xffffffffffffffff,%rax
je     11b4 <StartPgrp+0x34>
test   %rax,%rax
je     11c4 <StartPgrp+0x44>
add    $0x38,%rsp
ret
lea    0xe4c(%rip),%rdi
add    $0x38,%rsp
jmp    1050 <LogErrno@plt>
lea    0x18(%rsp),%rdi
call   1070 <sigemptyset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
xor    %edx,%edx
call   1040 <sigaction@plt>
test   %rax,%rax
je     120a <StartPgrp+0x8a>
lea    0xe37(%rip),%rdi
call   1050 <LogErrno@plt>
movaps 0x2bdf(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0xe32(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1080 <ExecvHelper@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.sigaction = type { i32, i64, i32 }
@.str = [7 x i8] c"setsid\00"
@.str.1 = [53 x i8] c"StartPgrp -> fork; expect potential race in KillPgrp\00"
@SIG_IGN = global i32 0
@SIGUSR1 = global i32 0
@.str.2 = [19 x i8] c"sigaction(SIGUSR1)\00"
@.str.3 = [17 x i8] c"pgrp_placeholder\00"
@__const.StartPgrp.args = [2 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i32 0, i32 0), i8* null]
@EXIT_FAILURE = global i32 0
define void @StartPgrp() {
%1 = alloca i64
%2 = alloca %struct.sigaction
%3 = alloca [2 x i8*]
%4 = call i64 (...) @setsid()
%5 = icmp eq i64 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @LogErrno(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = call i64 (...) @fork()
store i64 %9, i64* %1
%10 = load i64, i64* %1
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = call i32 @LogErrno(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %36
14: ; preds = %8
%15 = load i64, i64* %1
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %14
%18 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 2
%19 = call i32 @sigemptyset(i32* %18)
%20 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 1
store i64 0, i64* %20
%21 = load i32, i32* @SIG_IGN
%22 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %2, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @SIGUSR1
%24 = call i64 @sigaction(i32 %23, %struct.sigaction* %2, i32* null)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = call i32 @LogErrno(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %17
%29 = bitcast [2 x i8*]* %3 to i8*
%30 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
%31 = call i32 @ExecvHelper(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8** %30)
%32 = call i32 @sleep(i32 2)
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @_exit(i32 %33)
unreachable
35: ; preds = %14
br label %36
36: ; preds = %35, %12
ret void
}

LLVM IR (-O2):
%struct.sigaction = type { i32, i64, i32 }
@.str = [7 x i8] c"setsid\00"
@.str.1 = [53 x i8] c"StartPgrp -> fork; expect potential race in KillPgrp\00"
@SIG_IGN = global i32 0
@SIGUSR1 = global i32 0
@.str.2 = [19 x i8] c"sigaction(SIGUSR1)\00"
@.str.3 = [17 x i8] c"pgrp_placeholder\00"
@__const.StartPgrp.args = [2 x i8*] [i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i32 0, i32 0), i8* null]
@EXIT_FAILURE = global i32 0
define void @StartPgrp() {
%1 = alloca %struct.sigaction
%2 = alloca [2 x i8*]
%3 = tail call i64 (...) @setsid()
%4 = icmp eq i64 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @LogErrno(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %0
%8 = tail call i64 (...) @fork()
switch i64 %8, label %30 [
i64 -1, label %9
i64 0, label %11
]
9: ; preds = %7
%10 = tail call i32 @LogErrno(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0))
br label %30
11: ; preds = %7
%12 = bitcast %struct.sigaction* %1 to i8*
%13 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 2
%14 = call i32 @sigemptyset(i32* %13)
%15 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 1
store i64 0, i64* %15
%16 = load i32, i32* @SIG_IGN
%17 = getelementptr inbounds %struct.sigaction, %struct.sigaction* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @SIGUSR1
%19 = call i64 @sigaction(i32 %18, %struct.sigaction* %1, i32* null)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %11
%22 = call i32 @LogErrno(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %11
%24 = bitcast [2 x i8*]* %2 to i8*
%25 = getelementptr inbounds [2 x i8*], [2 x i8*]* %2, i64 0, i64 0
%26 = call i32 @ExecvHelper(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8** %25)
%27 = call i32 @sleep(i32 2)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @_exit(i32 %28)
unreachable
30: ; preds = %7, %9
ret void
}