#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 */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_ASYNC;
int O_NONBLOCK;
int errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int os_clear_fd_async(int fd) {
  int flags;
  flags = fcntl(fd, F_GETFL);
  if (flags < 0)
    return -errno;
  flags &= ~(O_ASYNC | O_NONBLOCK);
  if (fcntl(fd, F_SETFL, flags) < 0)
    return -errno;
  return 0;
}


x86 assembly (clang, -O0):
<os_clear_fd_async>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    114e <os_clear_fd_async+0x3e>
xor    %eax,%eax
mov    0x2e8c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11a3 <os_clear_fd_async+0x93>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    0x2e8a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    119c <os_clear_fd_async+0x8c>
xor    %eax,%eax
mov    0x2e3e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11a3 <os_clear_fd_async+0x93>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_clear_fd_async>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1158 <os_clear_fd_async+0x48>
mov    0x2e8e(%rip),%rcx
mov    0x2eaf(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    %eax,%edx
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1163 <os_clear_fd_async+0x53>
mov    0x2e71(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@errno = global i32 0
@O_ASYNC = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @os_clear_fd_async(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = load i32, i32* @F_GETFL
%7 = call i64 (i32, i32, ...) @fcntl(i32 %5, i32 %6)
%8 = trunc i64 %7 to i32
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
store i32 %13, i32* %2
br label %30
14: ; preds = %1
%15 = load i32, i32* @O_ASYNC
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = xor i32 %17, -1
%19 = load i32, i32* %4
%20 = and i32 %19, %18
store i32 %20, i32* %4
%21 = load i32, i32* %3
%22 = load i32, i32* @F_SETFL
%23 = load i32, i32* %4
%24 = call i64 (i32, i32, ...) @fcntl(i32 %21, i32 %22, i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %14
%27 = load i32, i32* @errno
%28 = sub nsw i32 0, %27
store i32 %28, i32* %2
br label %30
29: ; preds = %14
store i32 0, i32* %2
br label %30
30: ; preds = %29, %26, %11
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@errno = global i32 0
@O_ASYNC = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @os_clear_fd_async(i32 %0) {
%2 = load i32, i32* @F_GETFL
%3 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %2)
%4 = trunc i64 %3 to i32
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %21
9: ; preds = %1
%10 = load i32, i32* @O_ASYNC
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %10
%13 = xor i32 %12, -1
%14 = and i32 %13, %4
%15 = load i32, i32* @F_SETFL
%16 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %15, i32 %14)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
br label %21
21: ; preds = %9, %18, %6
%22 = phi i32 [ %8, %6 ], [ %20, %18 ], [ 0, %9 ]
ret i32 %22
}