#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 */
typedef int /*<<< orphan*/ val;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ TCP_KEEPCNT;
int /*<<< orphan*/ TCP_KEEPIDLE;
int /*<<< orphan*/ TCP_KEEPINTVL;
int /*<<< orphan*/ anetSetError(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int anetKeepAlive(char *err, int fd, int interval) {
  int val = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1) {
    anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno));
    return -1;
  }
  val = interval;
  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {
    anetSetError(err, "setsockopt TCP_KEEPIDLE: %s\n", strerror(errno));
    return -1;
  }
  val = interval / 3;
  if (val == 0) {
    val = 1;
  }
  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {
    anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno));
    return -1;
  }
  val = 3;
  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
    anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<anetKeepAlive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11aa <anetKeepAlive+0x7a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%edx
lea    0xe67(%rip),%rsi
call   1040 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <anetKeepAlive+0x1d0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%eax
jge    1211 <anetKeepAlive+0xe1>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe1c(%rip),%rsi
call   1040 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <anetKeepAlive+0x1d0>
mov    -0x18(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1230 <anetKeepAlive+0x100>
movl   $0x1,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%eax
jge    1291 <anetKeepAlive+0x161>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xdb9(%rip),%rsi
call   1040 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <anetKeepAlive+0x1d0>
movl   $0x3,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    0x2d25(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%eax
jge    12f9 <anetKeepAlive+0x1c9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xd6f(%rip),%rsi
call   1040 <anetSetError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <anetKeepAlive+0x1d0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<anetKeepAlive>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
movl   $0x1,0x4(%rsp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1225 <anetKeepAlive+0xf5>
mov    %ebx,0x4(%rsp)
mov    0x2e62(%rip),%r15
mov    (%r15),%esi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     122e <anetKeepAlive+0xfe>
lea    0x2(%rbx),%ecx
mov    $0x1,%eax
cmp    $0x5,%ecx
jb     11c8 <anetKeepAlive+0x98>
movslq %ebx,%rax
imul   $0x55555556,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
mov    %eax,0x4(%rsp)
mov    (%r15),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
js     1237 <anetKeepAlive+0x107>
movl   $0x3,0x4(%rsp)
mov    (%r15),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    125e <anetKeepAlive+0x12e>
lea    0xe34(%rip),%rbx
jmp    123e <anetKeepAlive+0x10e>
lea    0xdd4(%rip),%rbx
jmp    123e <anetKeepAlive+0x10e>
lea    0xde7(%rip),%rbx
jmp    123e <anetKeepAlive+0x10e>
lea    0xdfb(%rip),%rbx
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <anetSetError@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str = [28 x i8] c"setsockopt SO_KEEPALIVE: %s\00"
@errno = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@.str.1 = [29 x i8] c"setsockopt TCP_KEEPIDLE: %s\0A\00"
@TCP_KEEPINTVL = global i32 0
@.str.2 = [30 x i8] c"setsockopt TCP_KEEPINTVL: %s\0A\00"
@TCP_KEEPCNT = global i32 0
@.str.3 = [28 x i8] c"setsockopt TCP_KEEPCNT: %s\0A\00"
define i32 @anetKeepAlive(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 1, i32* %8
%9 = load i32, i32* %6
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_KEEPALIVE
%12 = call i32 @setsockopt(i32 %9, i32 %10, i32 %11, i32* %8, i32 4)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = call i32 @anetSetError(i8* %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 -1, i32* %4
br label %60
19: ; preds = %3
%20 = load i32, i32* %7
store i32 %20, i32* %8
%21 = load i32, i32* %6
%22 = load i32, i32* @IPPROTO_TCP
%23 = load i32, i32* @TCP_KEEPIDLE
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i32* %8, i32 4)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %19
%27 = load i8*, i8** %5
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @anetSetError(i8* %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %29)
store i32 -1, i32* %4
br label %60
31: ; preds = %19
%32 = load i32, i32* %7
%33 = sdiv i32 %32, 3
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %37
36: ; preds = %31
store i32 1, i32* %8
br label %37
37: ; preds = %36, %31
%38 = load i32, i32* %6
%39 = load i32, i32* @IPPROTO_TCP
%40 = load i32, i32* @TCP_KEEPINTVL
%41 = call i32 @setsockopt(i32 %38, i32 %39, i32 %40, i32* %8, i32 4)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i8*, i8** %5
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 @anetSetError(i8* %44, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %46)
store i32 -1, i32* %4
br label %60
48: ; preds = %37
store i32 3, i32* %8
%49 = load i32, i32* %6
%50 = load i32, i32* @IPPROTO_TCP
%51 = load i32, i32* @TCP_KEEPCNT
%52 = call i32 @setsockopt(i32 %49, i32 %50, i32 %51, i32* %8, i32 4)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %48
%55 = load i8*, i8** %5
%56 = load i32, i32* @errno
%57 = call i32 @strerror(i32 %56)
%58 = call i32 @anetSetError(i8* %55, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %57)
store i32 -1, i32* %4
br label %60
59: ; preds = %48
store i32 0, i32* %4
br label %60
60: ; preds = %59, %54, %43, %26, %14
%61 = load i32, i32* %4
ret i32 %61
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str = [28 x i8] c"setsockopt SO_KEEPALIVE: %s\00"
@errno = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@.str.1 = [29 x i8] c"setsockopt TCP_KEEPIDLE: %s\0A\00"
@TCP_KEEPINTVL = global i32 0
@.str.2 = [30 x i8] c"setsockopt TCP_KEEPINTVL: %s\0A\00"
@TCP_KEEPCNT = global i32 0
@.str.3 = [28 x i8] c"setsockopt TCP_KEEPCNT: %s\0A\00"
define i32 @anetKeepAlive(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_KEEPALIVE
%8 = call i32 @setsockopt(i32 %1, i32 %6, i32 %7, i32* %4, i32 4)
%9 = icmp eq i32 %8, -1
br i1 %9, label %29, label %10
10: ; preds = %3
store i32 %2, i32* %4
%11 = load i32, i32* @IPPROTO_TCP
%12 = load i32, i32* @TCP_KEEPIDLE
%13 = call i32 @setsockopt(i32 %1, i32 %11, i32 %12, i32* %4, i32 4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %29, label %15
15: ; preds = %10
%16 = sdiv i32 %2, 3
%17 = add i32 %2, 2
%18 = icmp ult i32 %17, 5
%19 = select i1 %18, i32 1, i32 %16
store i32 %19, i32* %4
%20 = load i32, i32* @IPPROTO_TCP
%21 = load i32, i32* @TCP_KEEPINTVL
%22 = call i32 @setsockopt(i32 %1, i32 %20, i32 %21, i32* %4, i32 4)
%23 = icmp slt i32 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %15
store i32 3, i32* %4
%25 = load i32, i32* @IPPROTO_TCP
%26 = load i32, i32* @TCP_KEEPCNT
%27 = call i32 @setsockopt(i32 %1, i32 %25, i32 %26, i32* %4, i32 4)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %24, %15, %10, %3
%30 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %10 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), %15 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), %24 ]
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @anetSetError(i8* %0, i8* %30, i32 %32)
br label %34
34: ; preds = %29, %24
%35 = phi i32 [ 0, %24 ], [ -1, %29 ]
ret i32 %35
}