#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_GETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int fcntl(int, int /*<<< orphan*/, ...);
int BIO_socket_nbio(int s, int mode) {
  int flags = fcntl(s, F_GETFD);
  if (mode && !(flags & O_NONBLOCK))
    return (fcntl(s, F_SETFL, flags | O_NONBLOCK) != -1);
  else if (!mode && (flags & O_NONBLOCK))
    return (fcntl(s, F_SETFL, flags & ~O_NONBLOCK) != -1);
  return (1);
}


x86 assembly (clang, -O0):
<BIO_socket_nbio>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1185 <BIO_socket_nbio+0x75>
mov    -0x10(%rbp),%eax
mov    0x2ea0(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1185 <BIO_socket_nbio+0x75>
mov    -0x8(%rbp),%edi
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    0x2e7f(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <BIO_socket_nbio+0xd7>
cmpl   $0x0,-0xc(%rbp)
jne    11db <BIO_socket_nbio+0xcb>
mov    -0x10(%rbp),%eax
mov    0x2e4f(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11db <BIO_socket_nbio+0xcb>
mov    -0x8(%rbp),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <BIO_socket_nbio+0xd7>
jmp    11e0 <BIO_socket_nbio+0xd0>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BIO_socket_nbio>:
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>
mov    0x2eba(%rip),%rcx
mov    (%rcx),%edx
test   %ebp,%ebp
je     1149 <BIO_socket_nbio+0x39>
mov    $0x1,%cl
test   %eax,%edx
jne    116f <BIO_socket_nbio+0x5f>
mov    0x2e97(%rip),%rcx
mov    (%rcx),%esi
or     %eax,%edx
mov    %ebx,%edi
jmp    115e <BIO_socket_nbio+0x4e>
test   %eax,%edx
je     116d <BIO_socket_nbio+0x5d>
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
not    %edx
and    %edx,%eax
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
setne  %cl
jmp    116f <BIO_socket_nbio+0x5f>
mov    $0x1,%cl
movzbl %cl,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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