#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 /*<<< orphan*/ printf(char *);
int tcpsocket_set_nonblock(int socketfd, int nonblock) {
  int nRet = -1;
  int nFlags = fcntl(socketfd, F_GETFL, 0);
  if (nonblock > 0) {
    printf("tcpsocket_set_nonblock. add O_NONBLOCK.\n");
    nFlags |= O_NONBLOCK;
  } else {
    printf("tcpsocket_set_nonblock. remove O_NONBLOCK\n");
    nFlags &= (~O_NONBLOCK);
  }
  nRet = fcntl(socketfd, F_SETFL, nFlags);
  return nRet;
}


x86 assembly (clang, -O0):
<tcpsocket_set_nonblock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1175 <tcpsocket_set_nonblock+0x55>
lea    0xea4(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1193 <tcpsocket_set_nonblock+0x73>
lea    0xead(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpsocket_set_nonblock>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebx
mov    %edi,%r14d
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%ebp
test   %ebx,%ebx
jle    1156 <tcpsocket_set_nonblock+0x36>
lea    0xee4(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e96(%rip),%rax
or     (%rax),%ebp
jmp    116f <tcpsocket_set_nonblock+0x4f>
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,%ebp
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
mov    %ebp,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <fcntl@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [41 x i8] c"tcpsocket_set_nonblock. add O_NONBLOCK.\0A\00"
@O_NONBLOCK = global i32 0
@.str.1 = [43 x i8] c"tcpsocket_set_nonblock. remove O_NONBLOCK\0A\00"
@F_SETFL = global i32 0
define i32 @tcpsocket_set_nonblock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 -1, i32* %5
%7 = load i32, i32* %3
%8 = load i32, i32* @F_GETFL
%9 = call i32 @fcntl(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @O_NONBLOCK
%15 = load i32, i32* %6
%16 = or i32 %15, %14
store i32 %16, i32* %6
br label %23
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @O_NONBLOCK
%20 = xor i32 %19, -1
%21 = load i32, i32* %6
%22 = and i32 %21, %20
store i32 %22, i32* %6
br label %23
23: ; preds = %17, %12
%24 = load i32, i32* %3
%25 = load i32, i32* @F_SETFL
%26 = load i32, i32* %6
%27 = call i32 @fcntl(i32 %24, i32 %25, i32 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
ret i32 %28
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@str = [42 x i8] c"tcpsocket_set_nonblock. remove O_NONBLOCK\00"
@str.2 = [40 x i8] c"tcpsocket_set_nonblock. add O_NONBLOCK.\00"
define i32 @tcpsocket_set_nonblock(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFL
%4 = tail call i32 @fcntl(i32 %0, i32 %3, i32 0)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.2, i64 0, i64 0))
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %8, %4
br label %15
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%12 = load i32, i32* @O_NONBLOCK
%13 = xor i32 %12, -1
%14 = and i32 %4, %13
br label %15
15: ; preds = %10, %6
%16 = phi i32 [ %9, %6 ], [ %14, %10 ]
%17 = load i32, i32* @F_SETFL
%18 = tail call i32 @fcntl(i32 %0, i32 %17, i32 %16)
ret i32 %18
}