#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;
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int set_non_block(int fd, unsigned char enable) {
  int result = 0;
  int flags = fcntl(fd, F_GETFL);
  if (flags >= 0) {
    if (enable)
      flags |= O_NONBLOCK;
    else
      flags &= ~O_NONBLOCK;
    if (fcntl(fd, F_SETFL, flags) < 0)
      result = -1;
  } else
    result = -1;
  return result;
}


x86 assembly (clang, -O0):
<set_non_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jl     11a4 <set_non_block+0x94>
cmpb   $0x0,-0x5(%rbp)
je     1166 <set_non_block+0x56>
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1178 <set_non_block+0x68>
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    119f <set_non_block+0x8f>
movl   $0xffffffff,-0xc(%rbp)
jmp    11ab <set_non_block+0x9b>
movl   $0xffffffff,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_non_block>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2eb2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     115a <set_non_block+0x4a>
mov    0x2eb6(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
or     %eax,%ecx
not    %edx
and    %eax,%edx
test   %bpl,%bpl
cmovne %ecx,%edx
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
sar    $0x3f,%rax
jmp    115f <set_non_block+0x4f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @set_non_block(i32 %0, i8 zeroext %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 sgt i32 %5, -1
br i1 %6, label %7, label %18
7: ; preds = %2
%8 = icmp eq i8 %1, 0
%9 = load i32, i32* @O_NONBLOCK
%10 = or i32 %9, %5
%11 = xor i32 %9, -1
%12 = and i32 %11, %5
%13 = select i1 %8, i32 %12, i32 %10
%14 = load i32, i32* @F_SETFL
%15 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %14, i32 %13)
%16 = ashr i64 %15, 63
%17 = trunc i64 %16 to i32
br label %18
18: ; preds = %7, %2
%19 = phi i32 [ %17, %7 ], [ -1, %2 ]
ret i32 %19
}