#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;
int fcntl(int, int /*<<< orphan*/, int);
int set_nonblock_flag(int desc, int value) {
  int oldflags = fcntl(desc, F_GETFL, 0);
  int err;
  int ret;
  if (oldflags == -1)
    return -1;
  if (value != 0)
    oldflags |= O_NONBLOCK;
  else
    oldflags &= ~O_NONBLOCK;
  ret = fcntl(desc, F_SETFL, oldflags);
  if (ret == -1)
    err = errno;
  else
    err = 0;
  return err;
}


x86 assembly (clang, -O0):
<set_nonblock_flag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    114a <set_nonblock_flag+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <set_nonblock_flag+0xa9>
cmpl   $0x0,-0xc(%rbp)
je     1168 <set_nonblock_flag+0x58>
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    117a <set_nonblock_flag+0x6a>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ac <set_nonblock_flag+0x9c>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11b3 <set_nonblock_flag+0xa3>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_nonblock_flag>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1167 <set_nonblock_flag+0x57>
mov    0x2eb1(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
or     %eax,%ecx
not    %edx
and    %eax,%edx
test   %r14d,%r14d
cmovne %ecx,%edx
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    116c <set_nonblock_flag+0x5c>
mov    0x2e6d(%rip),%rax
mov    (%rax),%ebx
jmp    116c <set_nonblock_flag+0x5c>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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