#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*/ EXIT_FAILURE;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
unsigned long O_NONBLOCK;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int setblocking(int sock, int block) {
  unsigned long opts;
  opts = fcntl(sock, F_GETFL);
  if (opts < 0) {
    perror("fcntl(F_GETFL)");
    exit(EXIT_FAILURE);
  }
  opts = block ? (opts & ~O_NONBLOCK) : (opts | O_NONBLOCK);
  if (fcntl(sock, F_SETFL, opts) < 0) {
    perror("fcntl(F_SETFL)");
    exit(EXIT_FAILURE);
  }
  return 0;
}


x86 assembly (clang, -O0):
<setblocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jae    117a <setblocking+0x4a>
lea    0xe99(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
je     11a2 <setblocking+0x72>
mov    -0x10(%rbp),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%rcx
xor    $0xffffffffffffffff,%rcx
and    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    11b4 <setblocking+0x84>
mov    -0x10(%rbp),%rax
mov    0x2e3b(%rip),%rcx
or     (%rcx),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11f7 <setblocking+0xc7>
lea    0xe2b(%rip),%rdi
call   1040 <perror@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setblocking>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%rdx
mov    %rdx,%rcx
not    %rcx
and    %rax,%rcx
or     %rax,%rdx
test   %ebx,%ebx
cmovne %rcx,%rdx
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
js     1183 <setblocking+0x53>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [15 x i8] c"fcntl(F_GETFL)\00"
@EXIT_FAILURE = global i32 0
@O_NONBLOCK = global i64 0
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl(F_SETFL)\00"
define i32 @setblocking(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %3
%7 = load i32, i32* @F_GETFL
%8 = call i64 (i32, i32, ...) @fcntl(i32 %6, i32 %7)
store i64 %8, i64* %5
%9 = load i64, i64* %5
%10 = icmp ult i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%19 = load i64, i64* %5
%20 = load i64, i64* @O_NONBLOCK
%21 = xor i64 %20, -1
%22 = and i64 %19, %21
br label %27
23: ; preds = %15
%24 = load i64, i64* %5
%25 = load i64, i64* @O_NONBLOCK
%26 = or i64 %24, %25
br label %27
27: ; preds = %23, %18
%28 = phi i64 [ %22, %18 ], [ %26, %23 ]
store i64 %28, i64* %5
%29 = load i32, i32* %3
%30 = load i32, i32* @F_SETFL
%31 = load i64, i64* %5
%32 = call i64 (i32, i32, ...) @fcntl(i32 %29, i32 %30, i64 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %27
ret i32 0
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@EXIT_FAILURE = global i32 0
@O_NONBLOCK = global i64 0
@F_SETFL = global i32 0
@.str.1 = [15 x i8] c"fcntl(F_SETFL)\00"
define i32 @setblocking(i32 %0, i32 %1) {
%3 = load i32, i32* @F_GETFL
%4 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %3)
%5 = icmp eq i32 %1, 0
%6 = load i64, i64* @O_NONBLOCK
%7 = xor i64 %6, -1
%8 = and i64 %4, %7
%9 = or i64 %6, %4
%10 = select i1 %5, i64 %9, i64 %8
%11 = load i32, i32* @F_SETFL
%12 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %11, i64 %10)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
ret i32 0
}