#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 /*<<< orphan*/ IPPROTO_TCP;
int O_NONBLOCK;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ assert(int);
int fcntl(int, int /*<<< orphan*/, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int tcp_set_option_internal(int fd, int opt) {
  assert(fd > 0);
  if (opt & 0x01) {
    int ival = 1;
    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &ival, sizeof(int)) == -1) {
      return -1;
    }
  }
  if (opt & 0x04) {
    int ival = 1;
    if (setsockopt(fd, IPPROTO_TCP, SO_KEEPALIVE, &ival, sizeof(int)) == -1) {
      return -1;
    }
  }
  if (opt & 0x02) {
    int flags;
    if ((flags = fcntl(fd, F_GETFL)) == -1) {
      return -1;
    }
    flags |= O_NONBLOCK;
    if (fcntl(fd, F_SETFL, flags) == -1) {
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<tcp_set_option_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11a3 <tcp_set_option_internal+0x73>
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    0x2e70(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    119e <tcp_set_option_internal+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <tcp_set_option_internal+0x147>
jmp    11a3 <tcp_set_option_internal+0x73>
mov    -0xc(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     11f7 <tcp_set_option_internal+0xc7>
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e15(%rip),%rax
mov    (%rax),%esi
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11f2 <tcp_set_option_internal+0xc2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <tcp_set_option_internal+0x147>
jmp    11f7 <tcp_set_option_internal+0xc7>
mov    -0xc(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1270 <tcp_set_option_internal+0x140>
mov    -0x8(%rbp),%edi
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1231 <tcp_set_option_internal+0x101>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <tcp_set_option_internal+0x147>
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    126b <tcp_set_option_internal+0x13b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1277 <tcp_set_option_internal+0x147>
jmp    1270 <tcp_set_option_internal+0x140>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_set_option_internal>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%ebx
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1050 <assert@plt>
test   $0x1,%bpl
je     117b <tcp_set_option_internal+0x4b>
movl   $0x1,0x4(%rsp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11f7 <tcp_set_option_internal+0xc7>
test   $0x4,%bpl
je     11af <tcp_set_option_internal+0x7f>
movl   $0x1,(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11f7 <tcp_set_option_internal+0xc7>
test   $0x2,%bpl
je     11f3 <tcp_set_option_internal+0xc3>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11fc <tcp_set_option_internal+0xcc>
mov    0x2e08(%rip),%rcx
or     (%rcx),%eax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11fc <tcp_set_option_internal+0xcc>
xor    %ebp,%ebp
jmp    11fc <tcp_set_option_internal+0xcc>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@SO_KEEPALIVE = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @tcp_set_option_internal(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 0
%11 = zext i1 %10 to i32
%12 = call i32 @assert(i32 %11)
%13 = load i32, i32* %5
%14 = and i32 %13, 1
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %2
store i32 1, i32* %6
%17 = load i32, i32* %4
%18 = load i32, i32* @IPPROTO_TCP
%19 = load i32, i32* @TCP_NODELAY
%20 = call i32 @setsockopt(i32 %17, i32 %18, i32 %19, i32* %6, i32 4)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %3
br label %58
23: ; preds = %16
br label %24
24: ; preds = %23, %2
%25 = load i32, i32* %5
%26 = and i32 %25, 4
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %24
store i32 1, i32* %7
%29 = load i32, i32* %4
%30 = load i32, i32* @IPPROTO_TCP
%31 = load i32, i32* @SO_KEEPALIVE
%32 = call i32 @setsockopt(i32 %29, i32 %30, i32 %31, i32* %7, i32 4)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %28
store i32 -1, i32* %3
br label %58
35: ; preds = %28
br label %36
36: ; preds = %35, %24
%37 = load i32, i32* %5
%38 = and i32 %37, 2
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %57
40: ; preds = %36
%41 = load i32, i32* %4
%42 = load i32, i32* @F_GETFL
%43 = call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %42)
store i32 %43, i32* %8
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %40
store i32 -1, i32* %3
br label %58
46: ; preds = %40
%47 = load i32, i32* @O_NONBLOCK
%48 = load i32, i32* %8
%49 = or i32 %48, %47
store i32 %49, i32* %8
%50 = load i32, i32* %4
%51 = load i32, i32* @F_SETFL
%52 = load i32, i32* %8
%53 = call i32 (i32, i32, ...) @fcntl(i32 %50, i32 %51, i32 %52)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %56
55: ; preds = %46
store i32 -1, i32* %3
br label %58
56: ; preds = %46
br label %57
57: ; preds = %56, %36
store i32 0, i32* %3
br label %58
58: ; preds = %57, %55, %45, %34, %22
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
@SO_KEEPALIVE = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @tcp_set_option_internal(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = icmp sgt i32 %0, 0
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = and i32 %1, 1
%9 = icmp eq i32 %8, 0
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%12 = load i32, i32* @IPPROTO_TCP
%13 = load i32, i32* @TCP_NODELAY
%14 = call i32 @setsockopt(i32 %0, i32 %12, i32 %13, i32* %3, i32 4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %39, label %16
16: ; preds = %10, %2
%17 = and i32 %1, 4
%18 = icmp eq i32 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %16
%20 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%21 = load i32, i32* @IPPROTO_TCP
%22 = load i32, i32* @SO_KEEPALIVE
%23 = call i32 @setsockopt(i32 %0, i32 %21, i32 %22, i32* %4, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %39, label %25
25: ; preds = %19, %16
%26 = and i32 %1, 2
%27 = icmp eq i32 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %25
%29 = load i32, i32* @F_GETFL
%30 = call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %39, label %32
32: ; preds = %28
%33 = load i32, i32* @O_NONBLOCK
%34 = or i32 %33, %30
%35 = load i32, i32* @F_SETFL
%36 = call i32 (i32, i32, ...) @fcntl(i32 %0, i32 %35, i32 %34)
%37 = icmp eq i32 %36, -1
br i1 %37, label %39, label %38
38: ; preds = %32, %25
br label %39
39: ; preds = %28, %19, %10, %32, %38
%40 = phi i32 [ -1, %10 ], [ -1, %19 ], [ -1, %32 ], [ 0, %38 ], [ -1, %28 ]
ret i32 %40
}