#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*/ perror(char *);
int set_blocking(int fd, bool blocking) {
  int flags;
  if ((flags = fcntl(fd, F_GETFL)) < 0) {
    perror("fcntl");
    return flags;
  }
  if (blocking) {
    flags &= ~O_NONBLOCK;
  } else {
    flags |= O_NONBLOCK;
  }
  if (fcntl(fd, F_SETFL, flags) < 0) {
    perror("fcntl");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_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)
cmp    $0x0,%eax
jge    1164 <set_blocking+0x44>
lea    0xeac(%rip),%rdi
call   1040 <perror@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d3 <set_blocking+0xb3>
cmpl   $0x0,-0xc(%rbp)
je     1185 <set_blocking+0x65>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1194 <set_blocking+0x74>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11cc <set_blocking+0xac>
lea    0xe45(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <set_blocking+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_blocking>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%ebp
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %rax,%rbx
test   %ebx,%ebx
js     1187 <set_blocking+0x67>
mov    0x2e9b(%rip),%rax
mov    (%rax),%edx
mov    %edx,%eax
not    %eax
and    %ebx,%eax
or     %ebx,%edx
test   %r15d,%r15d
cmovne %eax,%edx
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %rax,%rax
js     1182 <set_blocking+0x62>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebx
lea    0xe72(%rip),%rdi
call   1040 <perror@plt>
mov    %ebx,%r14d
jmp    1174 <set_blocking+0x54>

LLVM IR (-O0):
@F_GETFL = global i32 0
@.str = [6 x i8] c"fcntl\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @set_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 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %6
store i32 %14, i32* %3
br label %36
15: ; preds = %2
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %23
18: ; preds = %15
%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 %27
23: ; preds = %15
%24 = load i32, i32* @O_NONBLOCK
%25 = load i32, i32* %6
%26 = or i32 %25, %24
store i32 %26, i32* %6
br label %27
27: ; preds = %23, %18
%28 = load i32, i32* %4
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* %6
%31 = call i64 (i32, i32, ...) @fcntl(i32 %28, i32 %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %36
35: ; preds = %27
store i32 0, i32* %3
br label %36
36: ; preds = %35, %33, %12
%37 = load i32, i32* %3
ret i32 %37
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@.str = [6 x i8] c"fcntl\00"
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @set_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 %17, label %7
7: ; preds = %2
%8 = icmp eq i32 %1, 0
%9 = load i32, i32* @O_NONBLOCK
%10 = xor i32 %9, -1
%11 = and i32 %10, %5
%12 = or i32 %9, %5
%13 = select i1 %8, i32 %12, i32 %11
%14 = load i32, i32* @F_SETFL
%15 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %14, i32 %13)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %7, %2
%18 = phi i32 [ %5, %2 ], [ -1, %7 ]
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %17, %7
%21 = phi i32 [ 0, %7 ], [ %18, %17 ]
ret i32 %21
}