#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 /*<<< orphan*/ ssh_set_error(char *);
int ssh_net_set_sock_blocking(int sock, int block) {
  int flags;
  flags = fcntl(sock, F_GETFL);
  if (flags < 0) {
    ssh_set_error("fcntl(): can't read socket flags");
    return -1;
  }
  if (block) {
    if ((flags & O_NONBLOCK) == 0)
      return 0;
    flags &= ~O_NONBLOCK;
  } else {
    if ((flags & O_NONBLOCK) != 0)
      return 0;
    flags |= O_NONBLOCK;
  }
  if (fcntl(sock, F_SETFL, flags) < 0) {
    ssh_set_error("fcntl(): can't set socket flags");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ssh_net_set_sock_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e98(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1166 <ssh_net_set_sock_blocking+0x46>
lea    0xeab(%rip),%rdi
call   1040 <ssh_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <ssh_net_set_sock_blocking+0xf7>
cmpl   $0x0,-0xc(%rbp)
je     11a8 <ssh_net_set_sock_blocking+0x88>
mov    -0x10(%rbp),%eax
mov    0x2e6e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1191 <ssh_net_set_sock_blocking+0x71>
movl   $0x0,-0x4(%rbp)
jmp    1217 <ssh_net_set_sock_blocking+0xf7>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    11d8 <ssh_net_set_sock_blocking+0xb8>
mov    -0x10(%rbp),%eax
mov    0x2e36(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c9 <ssh_net_set_sock_blocking+0xa9>
movl   $0x0,-0x4(%rbp)
jmp    1217 <ssh_net_set_sock_blocking+0xf7>
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1210 <ssh_net_set_sock_blocking+0xf0>
lea    0xe22(%rip),%rdi
call   1040 <ssh_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <ssh_net_set_sock_blocking+0xf7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssh_net_set_sock_blocking>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1152 <ssh_net_set_sock_blocking+0x32>
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%edx
test   %ebp,%ebp
je     115b <ssh_net_set_sock_blocking+0x3b>
test   %eax,%edx
je     118f <ssh_net_set_sock_blocking+0x6f>
not    %edx
and    %eax,%edx
jmp    1163 <ssh_net_set_sock_blocking+0x43>
lea    0xea7(%rip),%rdi
jmp    1183 <ssh_net_set_sock_blocking+0x63>
xor    %ebp,%ebp
test   %eax,%edx
jne    1191 <ssh_net_set_sock_blocking+0x71>
or     %eax,%edx
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
jns    1191 <ssh_net_set_sock_blocking+0x71>
lea    0xe9e(%rip),%rdi
call   1040 <ssh_set_error@plt>
mov    $0xffffffff,%ebp
jmp    1191 <ssh_net_set_sock_blocking+0x71>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [33 x i8] c"fcntl(): can't read socket flags\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.1 = [32 x i8] c"fcntl(): can't set socket flags\00"
define i32 @ssh_net_set_sock_blocking(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_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 slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @ssh_set_error(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
15: ; preds = %2
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %29
18: ; preds = %15
%19 = load i32, i32* %6
%20 = load i32, i32* @O_NONBLOCK
%21 = and i32 %19, %20
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 0, i32* %3
br label %48
24: ; preds = %18
%25 = load i32, i32* @O_NONBLOCK
%26 = xor i32 %25, -1
%27 = load i32, i32* %6
%28 = and i32 %27, %26
store i32 %28, i32* %6
br label %39
29: ; preds = %15
%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 %35
34: ; preds = %29
store i32 0, i32* %3
br label %48
35: ; preds = %29
%36 = load i32, i32* @O_NONBLOCK
%37 = load i32, i32* %6
%38 = or i32 %37, %36
store i32 %38, i32* %6
br label %39
39: ; preds = %35, %24
%40 = load i32, i32* %4
%41 = load i32, i32* @F_SETFL
%42 = load i32, i32* %6
%43 = call i64 (i32, i32, ...) @fcntl(i32 %40, i32 %41, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @ssh_set_error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %48
47: ; preds = %39
store i32 0, i32* %3
br label %48
48: ; preds = %47, %45, %34, %23, %13
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [33 x i8] c"fcntl(): can't read socket flags\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str.1 = [32 x i8] c"fcntl(): can't set socket flags\00"
define i32 @ssh_net_set_sock_blocking(i32 %0, i32 %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 slt i32 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %2
%8 = icmp eq i32 %1, 0
%9 = load i32, i32* @O_NONBLOCK
%10 = and i32 %9, %5
%11 = icmp eq i32 %10, 0
br i1 %8, label %16, label %12
12: ; preds = %7
br i1 %11, label %27, label %13
13: ; preds = %12
%14 = xor i32 %9, -1
%15 = and i32 %14, %5
br label %19
16: ; preds = %7
br i1 %11, label %17, label %27
17: ; preds = %16
%18 = or i32 %9, %5
br label %19
19: ; preds = %17, %13
%20 = phi i32 [ %15, %13 ], [ %18, %17 ]
%21 = load i32, i32* @F_SETFL
%22 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %21, i32 %20)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19, %2
%25 = phi i8* [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %19 ]
%26 = tail call i32 @ssh_set_error(i8* %25)
br label %27
27: ; preds = %24, %19, %16, %12
%28 = phi i32 [ 0, %12 ], [ 0, %16 ], [ 0, %19 ], [ -1, %24 ]
ret i32 %28
}