#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__ ENOSYS;
int /*<<< orphan*/ F_SETFD;
int NSIG;
int _NSIG;
int __signalfd(int, int /*<<< orphan*/ const *, int);
int __signalfd4(int, int /*<<< orphan*/ const *, int, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int signalfd(int fd, const sigset_t *mask, int flags) {
  int r = __signalfd4(fd, mask, _NSIG / 8, flags);
  if (r == -1 && errno == ENOSYS) {
    r = __signalfd(fd, mask, NSIG / 8);
    if (r != -1 && flags) {
      int x;
      x = fcntl(r, F_SETFD, flags);
      if (x == -1)
        close(r);
    }
  }
  return r;
}


x86 assembly (clang, -O0):
<signalfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e90(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
mov    -0x14(%rbp),%ecx
call   1060 <__signalfd4@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1204 <signalfd+0xc4>
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
mov    0x2e4e(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <signalfd+0xc4>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
call   1050 <__signalfd@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
je     11ff <signalfd+0xbf>
cmpl   $0x0,-0x14(%rbp)
je     11ff <signalfd+0xbf>
mov    -0x18(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11fa <signalfd+0xba>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    11ff <signalfd+0xbf>
jmp    1204 <signalfd+0xc4>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signalfd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
lea    0x7(%rax),%edx
test   %eax,%eax
cmovns %eax,%edx
sar    $0x3,%edx
mov    %r14d,%ecx
call   1060 <__signalfd4@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11d6 <signalfd+0x96>
mov    0x2e4f(%rip),%rax
mov    (%rax),%rax
mov    0x2e5d(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    11d6 <signalfd+0x96>
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
lea    0x7(%rax),%edx
test   %eax,%eax
cmovns %eax,%edx
sar    $0x3,%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   1050 <__signalfd@plt>
mov    %eax,%ebp
test   %r14d,%r14d
je     11d6 <signalfd+0x96>
cmp    $0xffffffff,%ebp
je     11d6 <signalfd+0x96>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11d6 <signalfd+0x96>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_NSIG = global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@NSIG = global i32 0
@F_SETFD = 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* %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = load i32*, i32** %5
%11 = load i32, i32* @_NSIG
%12 = sdiv i32 %11, 8
%13 = load i32, i32* %6
%14 = call i32 @__signalfd4(i32 %9, i32* %10, i32 %12, i32 %13)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %44
17: ; preds = %3
%18 = load i64, i64* @errno
%19 = load i64, i64* @ENOSYS
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %44
21: ; preds = %17
%22 = load i32, i32* %4
%23 = load i32*, i32** %5
%24 = load i32, i32* @NSIG
%25 = sdiv i32 %24, 8
%26 = call i32 @__signalfd(i32 %22, i32* %23, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %43
29: ; preds = %21
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = load i32, i32* %7
%34 = load i32, i32* @F_SETFD
%35 = load i32, i32* %6
%36 = call i32 @fcntl(i32 %33, i32 %34, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %32
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39, %32
br label %43
43: ; preds = %42, %29, %21
br label %44
44: ; preds = %43, %17, %3
%45 = load i32, i32* %7
ret i32 %45
}

LLVM IR (-O2):
@_NSIG = global i32 0
@errno = global i64 0
@ENOSYS = global i64 0
@NSIG = global i32 0
@F_SETFD = 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 @__signalfd4(i32 %0, i32* %1, i32 %5, i32 %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %25
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOSYS
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %25
12: ; preds = %8
%13 = load i32, i32* @NSIG
%14 = sdiv i32 %13, 8
%15 = tail call i32 @__signalfd(i32 %0, i32* %1, i32 %14)
%16 = icmp ne i32 %15, -1
%17 = icmp ne i32 %2, 0
%18 = and i1 %17, %16
br i1 %18, label %19, label %25
19: ; preds = %12
%20 = load i32, i32* @F_SETFD
%21 = tail call i32 @fcntl(i32 %15, i32 %20, i32 %2)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @close(i32 %15)
br label %25
25: ; preds = %19, %23, %12, %8, %3
%26 = phi i32 [ %15, %12 ], [ -1, %8 ], [ %6, %3 ], [ %15, %23 ], [ %15, %19 ]
ret i32 %26
}