#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int sigwaitinfo(int /*<<< orphan*/ const *, int /*<<< orphan*/ *);
int sigwait(const sigset_t *restrict set, int *restrict sig) {
  int result;
  do {
    result = sigwaitinfo(set, NULL);
  } while (result < 0 && errno == EINTR);
  if (result < 0)
    return result;
  *sig = result;
  return 0;
}


x86 assembly (clang, -O0):
<sigwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <sigwaitinfo@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jge    1159 <sigwait+0x49>
mov    0x2e9a(%rip),%rax
mov    (%rax),%rax
mov    0x2e88(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1120 <sigwait+0x10>
cmpl   $0x0,-0x1c(%rbp)
jge    1179 <sigwait+0x69>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1189 <sigwait+0x79>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigwait>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ebb(%rip),%r15
mov    0x2eac(%rip),%r12
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <sigwaitinfo@plt>
test   %eax,%eax
jns    1147 <sigwait+0x37>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1130 <sigwait+0x20>
test   %eax,%eax
js     1150 <sigwait+0x40>
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
define i32 @sigwait(i32* noalias %0, i32* noalias %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
br label %7
7: ; preds = %17, %2
%8 = load i32*, i32** %4
%9 = call i32 @sigwaitinfo(i32* %8, i32* null)
store i32 %9, i32* %6
br label %10
10: ; preds = %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br label %17
17: ; preds = %13, %10
%18 = phi i1 [ false, %10 ], [ %16, %13 ]
br i1 %18, label %7, label %19
19: ; preds = %17
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i32, i32* %6
store i32 %23, i32* %3
br label %27
24: ; preds = %19
%25 = load i32, i32* %6
%26 = load i32*, i32** %5
store i32 %25, i32* %26
store i32 0, i32* %3
br label %27
27: ; preds = %24, %22
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i32 @sigwait(i32* noalias %0, i32* noalias nocapture writeonly %1) {
br label %3
3: ; preds = %3, %2
%4 = tail call i32 @sigwaitinfo(i32* %0, i32* null)
%5 = icmp slt i32 %4, 0
%6 = load i64, i64* @errno
%7 = load i64, i64* @EINTR
%8 = icmp eq i64 %6, %7
%9 = select i1 %5, i1 %8, i1 false
br i1 %9, label %3, label %10
10: ; preds = %3
br i1 %5, label %12, label %11
11: ; preds = %10
store i32 %4, i32* %1
br label %12
12: ; preds = %10, %11
%13 = phi i32 [ 0, %11 ], [ %4, %10 ]
ret i32 %13
}