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


x86 assembly (clang, -O0):
<os_set_fd_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1151 <os_set_fd_block+0x41>
xor    %eax,%eax
mov    0x2e89(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <os_set_fd_block+0xab>
cmpl   $0x0,-0xc(%rbp)
je     1172 <os_set_fd_block+0x62>
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1181 <os_set_fd_block+0x71>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11b4 <os_set_fd_block+0xa4>
xor    %eax,%eax
mov    0x2e26(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bb <os_set_fd_block+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_set_fd_block>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     115d <os_set_fd_block+0x4d>
mov    0x2eb2(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
not    %ecx
and    %eax,%ecx
or     %eax,%edx
test   %r14d,%r14d
cmovne %ecx,%edx
mov    0x2e8b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1168 <os_set_fd_block+0x58>
mov    0x2e6c(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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