#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 int /*<<< orphan*/ sigset_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_CRIT;
int /*<<< orphan*/ LOG_NDELAY;
int /*<<< orphan*/ LOG_USER;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ SIG_BLOCK;
scalar_t__ check_sock(int);
int /*<<< orphan*/ close(int);
scalar_t__ create_sock(int);
int /*<<< orphan*/ dprintf(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int open(char *, int, int);
int /*<<< orphan*/ openlog(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sigaddset(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int /*<<< orphan*/ sigwait(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *);
int main() {
  int i, fd;
  sigset_t set;
  int sig;
  for (i = 0; i < 2; i++)
    if (create_sock(i))
      return -1;
  fd = open("pid", O_WRONLY | O_CREAT, 0666);
  if (fd < 0)
    return 1;
  dprintf(fd, "%d\n", getpid());
  close(fd);
  openlog("test", LOG_NDELAY, LOG_USER);
  sigemptyset(&set);
  sigaddset(&set, SIGTERM);
  sigprocmask(SIG_BLOCK, &set, NULL);
  sigwait(&set, &sig);
  syslog(LOG_CRIT, "test message");
  for (i = 0; i < 2; i++)
    if (check_sock(i))
      return -1;
  printf("PASS\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1221 <main+0x51>
mov    -0x8(%rbp),%edi
call   1030 <create_sock@plt>
cmp    $0x0,%rax
je     120e <main+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <main+0x179>
jmp    1213 <main+0x43>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11e6 <main+0x16>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2dc7(%rip),%rax
or     (%rax),%esi
lea    0xdc6(%rip),%rdi
mov    $0x1b6,%edx
call   10c0 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    125d <main+0x8d>
movl   $0x1,-0x4(%rbp)
jmp    1349 <main+0x179>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xd8e(%rip),%rsi
call   1080 <dprintf@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
lea    0xd6c(%rip),%rdi
call   10e0 <openlog@plt>
lea    -0x10(%rbp),%rdi
call   1090 <sigemptyset@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   10f0 <sigaddset@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <sigprocmask@plt>
lea    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   10d0 <sigwait@plt>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd1e(%rip),%rsi
call   10a0 <syslog@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1336 <main+0x166>
mov    -0x8(%rbp),%edi
call   10b0 <check_sock@plt>
cmp    $0x0,%rax
je     1323 <main+0x153>
movl   $0xffffffff,-0x4(%rbp)
jmp    1349 <main+0x179>
jmp    1328 <main+0x158>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    12fb <main+0x12b>
lea    0xcdd(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %edi,%edi
call   1030 <create_sock@plt>
test   %rax,%rax
jne    11f3 <main+0x23>
mov    $0x1,%ebx
mov    $0x1,%edi
call   1030 <create_sock@plt>
test   %rax,%rax
je     1201 <main+0x31>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2da8(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xde6(%rip),%rdi
mov    $0x1b6,%edx
call   10c0 <open@plt>
test   %eax,%eax
js     11f8 <main+0x28>
mov    %eax,%ebp
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xdcc(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
call   1080 <dprintf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
mov    (%rax),%edx
lea    0xda7(%rip),%rdi
call   10e0 <openlog@plt>
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sigemptyset@plt>
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10f0 <sigaddset@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1040 <sigprocmask@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10d0 <sigwait@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xd5b(%rip),%rsi
call   10a0 <syslog@plt>
xor    %edi,%edi
call   10b0 <check_sock@plt>
test   %rax,%rax
jne    11f3 <main+0x23>
mov    $0x1,%edi
call   10b0 <check_sock@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11f8 <main+0x28>
lea    0xd34(%rip),%rdi
call   1050 <puts@plt>
xor    %ebx,%ebx
jmp    11f8 <main+0x28>

LLVM IR (-O0):
@.str = [4 x i8] c"pid\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [5 x i8] c"test\00"
@LOG_NDELAY = global i32 0
@LOG_USER = global i32 0
@SIGTERM = global i32 0
@SIG_BLOCK = global i32 0
@LOG_CRIT = global i32 0
@.str.3 = [13 x i8] c"test message\00"
@.str.4 = [6 x i8] c"PASS\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
br label %6
6: ; preds = %15, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %18
9: ; preds = %6
%10 = load i32, i32* %2
%11 = call i64 @create_sock(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %9
store i32 -1, i32* %1
br label %57
14: ; preds = %9
br label %15
15: ; preds = %14
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %6
18: ; preds = %6
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %21, i32 438)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %18
store i32 1, i32* %1
br label %57
26: ; preds = %18
%27 = load i32, i32* %3
%28 = call i32 (...) @getpid()
%29 = call i32 @dprintf(i32 %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %3
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* @LOG_NDELAY
%33 = load i32, i32* @LOG_USER
%34 = call i32 @openlog(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
%35 = call i32 @sigemptyset(i32* %4)
%36 = load i32, i32* @SIGTERM
%37 = call i32 @sigaddset(i32* %4, i32 %36)
%38 = load i32, i32* @SIG_BLOCK
%39 = call i32 @sigprocmask(i32 %38, i32* %4, i32* null)
%40 = call i32 @sigwait(i32* %4, i32* %5)
%41 = load i32, i32* @LOG_CRIT
%42 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %43
43: ; preds = %52, %26
%44 = load i32, i32* %2
%45 = icmp slt i32 %44, 2
br i1 %45, label %46, label %55
46: ; preds = %43
%47 = load i32, i32* %2
%48 = call i64 @check_sock(i32 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %46
store i32 -1, i32* %1
br label %57
51: ; preds = %46
br label %52
52: ; preds = %51
%53 = load i32, i32* %2
%54 = add nsw i32 %53, 1
store i32 %54, i32* %2
br label %43
55: ; preds = %43
%56 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %57
57: ; preds = %55, %50, %25, %13
%58 = load i32, i32* %1
ret i32 %58
}

LLVM IR (-O2):
@.str = [4 x i8] c"pid\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.1 = [4 x i8] c"%d\0A\00"
@.str.2 = [5 x i8] c"test\00"
@LOG_NDELAY = global i32 0
@LOG_USER = global i32 0
@SIGTERM = global i32 0
@SIG_BLOCK = global i32 0
@LOG_CRIT = global i32 0
@.str.3 = [13 x i8] c"test message\00"
@str = [5 x i8] c"PASS\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i64 @create_sock(i32 0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %38
7: ; preds = %0
%8 = tail call i64 @create_sock(i32 1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %38
10: ; preds = %7
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %13, i32 438)
%15 = icmp slt i32 %14, 0
br i1 %15, label %38, label %16
16: ; preds = %10
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 @dprintf(i32 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = tail call i32 @close(i32 %14)
%20 = load i32, i32* @LOG_NDELAY
%21 = load i32, i32* @LOG_USER
%22 = tail call i32 @openlog(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
%23 = call i32 @sigemptyset(i32* %1)
%24 = load i32, i32* @SIGTERM
%25 = call i32 @sigaddset(i32* %1, i32 %24)
%26 = load i32, i32* @SIG_BLOCK
%27 = call i32 @sigprocmask(i32 %26, i32* %1, i32* null)
%28 = call i32 @sigwait(i32* %1, i32* %2)
%29 = load i32, i32* @LOG_CRIT
%30 = call i32 @syslog(i32 %29, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%31 = call i64 @check_sock(i32 0)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %16
%34 = call i64 @check_sock(i32 1)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
br label %38
38: ; preds = %0, %7, %16, %33, %10, %36
%39 = phi i32 [ 0, %36 ], [ 1, %10 ], [ -1, %33 ], [ -1, %16 ], [ -1, %7 ], [ -1, %0 ]
ret i32 %39
}