#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);
bool SetSocketBlockingEnabled(int fd, bool blocking) {
  if (fd < 0)
    return false;
  int flags = fcntl(fd, F_GETFL, 0);
  if (flags < 0)
    return false;
  flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
  return (fcntl(fd, F_SETFL, flags) == 0) ? true : false;
}


x86 assembly (clang, -O0):
<SetSocketBlockingEnabled>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1134 <SetSocketBlockingEnabled+0x24>
movl   $0x0,-0x4(%rbp)
jmp    11c0 <SetSocketBlockingEnabled+0xb0>
mov    -0x8(%rbp),%edi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1160 <SetSocketBlockingEnabled+0x50>
movl   $0x0,-0x4(%rbp)
jmp    11c0 <SetSocketBlockingEnabled+0xb0>
cmpl   $0x0,-0xc(%rbp)
je     1183 <SetSocketBlockingEnabled+0x73>
mov    -0x10(%rbp),%eax
mov    0x2e74(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1192 <SetSocketBlockingEnabled+0x82>
mov    -0x10(%rbp),%eax
mov    0x2e5b(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SetSocketBlockingEnabled>:
push   %rbp
push   %r14
push   %rbx
test   %edi,%edi
js     1165 <SetSocketBlockingEnabled+0x55>
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1167 <SetSocketBlockingEnabled+0x57>
mov    0x2eae(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
not    %ecx
and    %eax,%ecx
or     %eax,%edx
test   %r14d,%r14d
cmovne %ecx,%edx
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
jmp    1167 <SetSocketBlockingEnabled+0x57>
xor    %ebp,%ebp
mov    %ebp,%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
define i32 @SetSocketBlockingEnabled(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 = icmp slt i32 %7, 0
br i1 %8, label %9, label %10
9: ; preds = %2
store i32 0, i32* %3
br label %39
10: ; preds = %2
%11 = load i32, i32* %4
%12 = load i32, i32* @F_GETFL
%13 = call i64 @fcntl(i32 %11, i32 %12, i32 0)
%14 = trunc i64 %13 to i32
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %10
store i32 0, i32* %3
br label %39
18: ; preds = %10
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* %6
%23 = load i32, i32* @O_NONBLOCK
%24 = xor i32 %23, -1
%25 = and i32 %22, %24
br label %30
26: ; preds = %18
%27 = load i32, i32* %6
%28 = load i32, i32* @O_NONBLOCK
%29 = or i32 %27, %28
br label %30
30: ; preds = %26, %21
%31 = phi i32 [ %25, %21 ], [ %29, %26 ]
store i32 %31, i32* %6
%32 = load i32, i32* %4
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* %6
%35 = call i64 @fcntl(i32 %32, i32 %33, i32 %34)
%36 = icmp eq i64 %35, 0
%37 = zext i1 %36 to i64
%38 = select i1 %36, i32 1, i32 0
store i32 %38, i32* %3
br label %39
39: ; preds = %30, %17, %9
%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
define i32 @SetSocketBlockingEnabled(i32 %0, i32 %1) {
%3 = icmp slt i32 %0, 0
br i1 %3, label %20, label %4
4: ; preds = %2
%5 = load i32, i32* @F_GETFL
%6 = tail call i64 @fcntl(i32 %0, i32 %5, i32 0)
%7 = trunc i64 %6 to i32
%8 = icmp slt i32 %7, 0
br i1 %8, label %20, label %9
9: ; preds = %4
%10 = icmp eq i32 %1, 0
%11 = load i32, i32* @O_NONBLOCK
%12 = xor i32 %11, -1
%13 = and i32 %12, %7
%14 = or i32 %11, %7
%15 = select i1 %10, i32 %14, i32 %13
%16 = load i32, i32* @F_SETFL
%17 = tail call i64 @fcntl(i32 %0, i32 %16, i32 %15)
%18 = icmp eq i64 %17, 0
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %9, %4, %2
%21 = phi i32 [ 0, %2 ], [ %19, %9 ], [ 0, %4 ]
ret i32 %21
}