#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 fcntl(int, int /*<<< orphan*/, int);
int set_noblock(int fd, void *optval, int optlen) {
  int rc;
  int flags;
  int on = *((int *)optval);
  if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
    flags = 0;
  flags = on ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK);
  rc = fcntl(fd, F_SETFL, flags);
  return rc;
}


x86 assembly (clang, -O0):
<set_noblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1151 <set_noblock+0x41>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     116f <set_noblock+0x5f>
mov    -0x1c(%rbp),%eax
mov    0x2e83(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
jmp    1183 <set_noblock+0x73>
mov    -0x1c(%rbp),%eax
mov    0x2e6f(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_noblock>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebx
mov    0x2eb3(%rip),%rax
mov    (%rax),%eax
mov    (%rsi),%r14d
xor    %ebp,%ebp
mov    %eax,%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
cmovns %eax,%ebp
mov    0x2eaf(%rip),%rax
mov    (%rax),%edx
mov    %edx,%eax
or     %ebp,%eax
not    %edx
and    %ebp,%edx
test   %r14d,%r14d
cmovne %eax,%edx
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fcntl@plt>

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

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @set_noblock(i32 %0, i8* nocapture %1, i32 %2) {
%4 = bitcast i8* %1 to i32*
%5 = load i32, i32* %4
%6 = load i32, i32* @F_GETFL
%7 = tail call i32 @fcntl(i32 %0, i32 %6, i32 0)
%8 = icmp sgt i32 %7, 0
%9 = select i1 %8, i32 %7, i32 0
%10 = icmp eq i32 %5, 0
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %9
%13 = xor i32 %11, -1
%14 = and i32 %9, %13
%15 = select i1 %10, i32 %14, i32 %12
%16 = load i32, i32* @F_SETFL
%17 = tail call i32 @fcntl(i32 %0, i32 %16, i32 %15)
ret i32 %17
}