#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 ENOSYS;
int FD_CLOEXEC;
int /*<<< orphan*/ const *F_SETFD;
int /*<<< orphan*/ const *F_SETFL;
int O_NONBLOCK;
int SFD_CLOEXEC;
int SFD_NONBLOCK;
int _NSIG;
int __syscall(int, int, int /*<<< orphan*/ const *, int, ...);
int __syscall_ret(int);
int signalfd(int fd, const sigset_t *sigs, int flags) {
  int ret = __syscall(355, fd, sigs, _NSIG / 8, flags);
  if (ret != -ENOSYS)
    return __syscall_ret(ret);
  ret = __syscall(349, fd, sigs, _NSIG / 8);
  if (ret >= 0) {
    if (flags & SFD_CLOEXEC)
      __syscall(55, ret, F_SETFD, FD_CLOEXEC);
    if (flags & SFD_NONBLOCK)
      __syscall(55, ret, F_SETFL, O_NONBLOCK);
  }
  return __syscall_ret(ret);
}


x86 assembly (clang, -O0):
<signalfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2eac(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x20(%rbp),%rdx
mov    %eax,%ecx
mov    -0x14(%rbp),%r8d
mov    $0x163,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e5d(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
je     118d <signalfd+0x6d>
mov    -0x18(%rbp),%edi
call   1040 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    1246 <signalfd+0x126>
mov    -0x8(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
mov    $0x15d,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     123b <signalfd+0x11b>
mov    -0x14(%rbp),%eax
mov    0x2e16(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11ff <signalfd+0xdf>
mov    -0x18(%rbp),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%rdx
mov    0x2db7(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    -0x14(%rbp),%eax
mov    0x2db7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1236 <signalfd+0x116>
mov    -0x18(%rbp),%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%rdx
mov    0x2db0(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
jmp    123b <signalfd+0x11b>
mov    -0x18(%rbp),%edi
call   1040 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signalfd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2eb9(%rip),%r12
mov    (%r12),%eax
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x3,%ecx
mov    $0x163,%edi
mov    %ebx,%esi
mov    %r15,%rdx
mov    %r14d,%r8d
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    %eax,%ebp
mov    0x2e6d(%rip),%rax
mov    %ebp,%ecx
add    (%rax),%ecx
jne    11ed <signalfd+0xcd>
mov    (%r12),%eax
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
sar    $0x3,%ecx
mov    $0x15d,%edi
mov    %ebx,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11ed <signalfd+0xcd>
mov    0x2e4e(%rip),%rax
test   %r14d,(%rax)
je     11c0 <signalfd+0xa0>
mov    0x2e12(%rip),%rax
mov    (%rax),%rdx
mov    0x2df8(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    0x2df9(%rip),%rax
test   %r14d,(%rax)
je     11ed <signalfd+0xcd>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rdx
mov    0x2dfb(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <__syscall_ret@plt>

LLVM IR (-O0):
@_NSIG = global i32 0
@ENOSYS = global i32 0
@SFD_CLOEXEC = global i32 0
@F_SETFD = global i32* null
@FD_CLOEXEC = global i32 0
@SFD_NONBLOCK = global i32 0
@F_SETFL = global i32* null
@O_NONBLOCK = global i32 0
define i32 @signalfd(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i32*, i32** %6
%11 = load i32, i32* @_NSIG
%12 = sdiv i32 %11, 8
%13 = load i32, i32* %7
%14 = call i32 (i32, i32, i32*, i32, ...) @__syscall(i32 355, i32 %9, i32* %10, i32 %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = load i32, i32* @ENOSYS
%17 = sub nsw i32 0, %16
%18 = icmp ne i32 %15, %17
br i1 %18, label %19, label %22
19: ; preds = %3
%20 = load i32, i32* %8
%21 = call i32 @__syscall_ret(i32 %20)
store i32 %21, i32* %4
br label %54
22: ; preds = %3
%23 = load i32, i32* %5
%24 = load i32*, i32** %6
%25 = load i32, i32* @_NSIG
%26 = sdiv i32 %25, 8
%27 = call i32 (i32, i32, i32*, i32, ...) @__syscall(i32 349, i32 %23, i32* %24, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp sge i32 %28, 0
br i1 %29, label %30, label %51
30: ; preds = %22
%31 = load i32, i32* %7
%32 = load i32, i32* @SFD_CLOEXEC
%33 = and i32 %31, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %30
%36 = load i32, i32* %8
%37 = load i32*, i32** @F_SETFD
%38 = load i32, i32* @FD_CLOEXEC
%39 = call i32 (i32, i32, i32*, i32, ...) @__syscall(i32 55, i32 %36, i32* %37, i32 %38)
br label %40
40: ; preds = %35, %30
%41 = load i32, i32* %7
%42 = load i32, i32* @SFD_NONBLOCK
%43 = and i32 %41, %42
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i32, i32* %8
%47 = load i32*, i32** @F_SETFL
%48 = load i32, i32* @O_NONBLOCK
%49 = call i32 (i32, i32, i32*, i32, ...) @__syscall(i32 55, i32 %46, i32* %47, i32 %48)
br label %50
50: ; preds = %45, %40
br label %51
51: ; preds = %50, %22
%52 = load i32, i32* %8
%53 = call i32 @__syscall_ret(i32 %52)
store i32 %53, i32* %4
br label %54
54: ; preds = %51, %19
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@_NSIG = global i32 0
@ENOSYS = global i32 0
@SFD_CLOEXEC = global i32 0
@F_SETFD = global i32* null
@FD_CLOEXEC = global i32 0
@SFD_NONBLOCK = global i32 0
@F_SETFL = global i32* null
@O_NONBLOCK = global i32 0
define i32 @signalfd(i32 %0, i32* %1, i32 %2) {
%4 = load i32, i32* @_NSIG
%5 = sdiv i32 %4, 8
%6 = tail call i32 (i32, i32, i32*, i32, ...) @__syscall(i32 355, i32 %0, i32* %1, i32 %5, i32 %2)
%7 = load i32, i32* @ENOSYS
%8 = sub nsw i32 0, %7
%9 = icmp eq i32 %6, %8
br i1 %9, label %10, label %31
10: ; preds = %3
%11 = load i32, i32* @_NSIG
%12 = sdiv i32 %11, 8
%13 = tail call i32 (i32, i32, i32*, i32, ...) @__syscall(i32 349, i32 %0, i32* %1, i32 %12)
%14 = icmp sgt i32 %13, -1
br i1 %14, label %15, label %31
15: ; preds = %10
%16 = load i32, i32* @SFD_CLOEXEC
%17 = and i32 %16, %2
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = load i32*, i32** @F_SETFD
%21 = load i32, i32* @FD_CLOEXEC
%22 = tail call i32 (i32, i32, i32*, i32, ...) @__syscall(i32 55, i32 %13, i32* %20, i32 %21)
br label %23
23: ; preds = %19, %15
%24 = load i32, i32* @SFD_NONBLOCK
%25 = and i32 %24, %2
%26 = icmp eq i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i32*, i32** @F_SETFL
%29 = load i32, i32* @O_NONBLOCK
%30 = tail call i32 (i32, i32, i32*, i32, ...) @__syscall(i32 55, i32 %13, i32* %28, i32 %29)
br label %31
31: ; preds = %10, %27, %23, %3
%32 = phi i32 [ %6, %3 ], [ %13, %23 ], [ %13, %27 ], [ %13, %10 ]
%33 = tail call i32 @__syscall_ret(i32 %32)
ret i32 %33
}