#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 */

/* Variables and functions */
scalar_t__ ENOSYS;
int /*<<< orphan*/ F_SETFD;
int __eventfd(unsigned int);
int __eventfd2(unsigned int, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int eventfd(unsigned int count, int flags) {
  int r = __eventfd2(count, flags);
  if (r == -1 && errno == ENOSYS) {
    r = __eventfd(count);
    if (r != -1 && flags) {
      int x;
      x = fcntl(r, F_SETFD, flags);
      if (x == -1)
        close(r);
    }
  }
  return r;
}


x86 assembly (clang, -O0):
<eventfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1050 <__eventfd2@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11d2 <eventfd+0x92>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e71(%rip),%rcx
cmp    (%rcx),%rax
jne    11d2 <eventfd+0x92>
mov    -0x4(%rbp),%edi
call   1060 <__eventfd@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
je     11cd <eventfd+0x8d>
cmpl   $0x0,-0x8(%rbp)
je     11cd <eventfd+0x8d>
mov    -0xc(%rbp),%edi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11c8 <eventfd+0x88>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
jmp    11cd <eventfd+0x8d>
jmp    11d2 <eventfd+0x92>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eventfd>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
call   1050 <__eventfd2@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11a2 <eventfd+0x62>
mov    0x2e74(%rip),%rax
mov    (%rax),%rax
mov    0x2e82(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    11a2 <eventfd+0x62>
mov    %ebp,%edi
call   1060 <__eventfd@plt>
mov    %eax,%ebx
test   %r14d,%r14d
je     11a2 <eventfd+0x62>
cmp    $0xffffffff,%ebx
je     11a2 <eventfd+0x62>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11a2 <eventfd+0x62>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ENOSYS = global i64 0
@F_SETFD = global i32 0
define i32 @eventfd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %3
%8 = load i32, i32* %4
%9 = call i32 @__eventfd2(i32 %7, i32 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %36
12: ; preds = %2
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOSYS
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %36
16: ; preds = %12
%17 = load i32, i32* %3
%18 = call i32 @__eventfd(i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %35
21: ; preds = %16
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = load i32, i32* %5
%26 = load i32, i32* @F_SETFD
%27 = load i32, i32* %4
%28 = call i32 @fcntl(i32 %25, i32 %26, i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* %5
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %31, %24
br label %35
35: ; preds = %34, %21, %16
br label %36
36: ; preds = %35, %12, %2
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
@errno = global i64 0
@ENOSYS = global i64 0
@F_SETFD = global i32 0
define i32 @eventfd(i32 %0, i32 %1) {
%3 = tail call i32 @__eventfd2(i32 %0, i32 %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %20
5: ; preds = %2
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOSYS
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %20
9: ; preds = %5
%10 = tail call i32 @__eventfd(i32 %0)
%11 = icmp ne i32 %10, -1
%12 = icmp ne i32 %1, 0
%13 = and i1 %12, %11
br i1 %13, label %14, label %20
14: ; preds = %9
%15 = load i32, i32* @F_SETFD
%16 = tail call i32 @fcntl(i32 %10, i32 %15, i32 %1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @close(i32 %10)
br label %20
20: ; preds = %14, %18, %9, %5, %2
%21 = phi i32 [ %10, %9 ], [ -1, %5 ], [ %3, %2 ], [ %10, %18 ], [ %10, %14 ]
ret i32 %21
}