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


x86 assembly (clang, -O0):
<fd_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1150 <fd_block+0x40>
xor    %eax,%eax
mov    0x2e8c(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    11ba <fd_block+0xaa>
mov    -0x10(%rbp),%eax
mov    0x2e8e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1172 <fd_block+0x62>
movq   $0x0,-0x8(%rbp)
jmp    11ba <fd_block+0xaa>
mov    -0xc(%rbp),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11b2 <fd_block+0xa2>
xor    %eax,%eax
mov    0x2e2a(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    11ba <fd_block+0xaa>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fd_block>:
push   %rbx
mov    %edi,%ebx
mov    0x2eae(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1152 <fd_block+0x42>
mov    0x2eba(%rip),%rcx
mov    (%rcx),%ecx
test   %eax,%ecx
je     114e <fd_block+0x3e>
mov    0x2e9d(%rip),%rdx
mov    (%rdx),%esi
not    %ecx
and    %ecx,%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1152 <fd_block+0x42>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e77(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%rax
pop    %rbx
ret

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

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