#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 block_socket(int sock, int block) {
  int flags;
  if (sock < 0)
    return -1;
  if ((flags = fcntl(sock, F_GETFL)) < 0)
    return -1;
  if (block)
    flags &= ~O_NONBLOCK;
  else
    flags |= O_NONBLOCK;
  if (fcntl(sock, F_SETFL, flags) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<block_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1134 <block_socket+0x24>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <block_socket+0xb2>
mov    -0x8(%rbp),%edi
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    115f <block_socket+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <block_socket+0xb2>
cmpl   $0x0,-0xc(%rbp)
je     1180 <block_socket+0x70>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    118f <block_socket+0x7f>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11bb <block_socket+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <block_socket+0xb2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<block_socket>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%ebp
test   %edi,%edi
js     1166 <block_socket+0x56>
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1166 <block_socket+0x56>
mov    0x2eab(%rip),%rcx
mov    (%rcx),%edx
mov    %edx,%ecx
not    %ecx
and    %eax,%ecx
or     %eax,%edx
test   %r14d,%r14d
cmovne %ecx,%edx
mov    0x2e84(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    %rax,%rbp
sar    $0x3f,%rbp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @block_socket(i32 %0, i32 %1) {
%3 = icmp slt i32 %0, 0
br i1 %3, label %20, label %4
4: ; preds = %2
%5 = load i32, i32* @F_GETFL
%6 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %5)
%7 = trunc i64 %6 to i32
%8 = icmp slt i32 %7, 0
br i1 %8, label %20, label %9
9: ; preds = %4
%10 = icmp eq i32 %1, 0
%11 = load i32, i32* @O_NONBLOCK
%12 = xor i32 %11, -1
%13 = and i32 %12, %7
%14 = or i32 %11, %7
%15 = select i1 %10, i32 %14, i32 %13
%16 = load i32, i32* @F_SETFL
%17 = tail call i64 (i32, i32, ...) @fcntl(i32 %0, i32 %16, i32 %15)
%18 = ashr i64 %17, 63
%19 = trunc i64 %18 to i32
br label %20
20: ; preds = %9, %4, %2
%21 = phi i32 [ -1, %2 ], [ -1, %4 ], [ %19, %9 ]
ret i32 %21
}