#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*/ IPPROTO_TCP;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ TCP_KEEPCNT;
int /*<<< orphan*/ TCP_KEEPIDLE;
int /*<<< orphan*/ TCP_KEEPINTVL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ net_err(char *, char *, int /*<<< orphan*/);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int tcp_keepalive(char *err, int fd, int interval) {
  int on = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof on) == -1) {
    net_err(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno));
    return -1;
  }
  int val = interval;
  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof val) == -1) {
    net_err(err, "setsockopt TCP_KEEPIDLE: %s", strerror(errno));
    return -1;
  }
  val = interval / 3;
  if (val == 0)
    val = 1;
  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof val) == -1) {
    net_err(err, "setsockopt TCP_KEEPINTVL: %s", strerror(errno));
    return -1;
  }
  val = 3;
  if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof val) == -1) {
    net_err(err, "setsockopt TCP_KEEPCNT: %s", strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tcp_keepalive>:
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 <tcp_keepalive+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 <net_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <tcp_keepalive+0x1d0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1211 <tcp_keepalive+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 <net_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <tcp_keepalive+0x1d0>
mov    -0x18(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1230 <tcp_keepalive+0x100>
movl   $0x1,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1291 <tcp_keepalive+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    0xdb8(%rip),%rsi
call   1040 <net_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <tcp_keepalive+0x1d0>
movl   $0x3,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
mov    0x2d25(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12f9 <tcp_keepalive+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    0xd6d(%rip),%rsi
call   1040 <net_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <tcp_keepalive+0x1d0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_keepalive>:
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     121f <tcp_keepalive+0xef>
mov    %ebx,(%rsp)
mov    0x2e63(%rip),%r15
mov    (%r15),%esi
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1239 <tcp_keepalive+0x109>
lea    0x2(%rbx),%ecx
mov    $0x1,%eax
cmp    $0x5,%ecx
jb     11c6 <tcp_keepalive+0x96>
movslq %ebx,%rax
imul   $0x55555556,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
add    %ecx,%eax
mov    %eax,(%rsp)
mov    (%r15),%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1242 <tcp_keepalive+0x112>
movl   $0x3,(%rsp)
mov    (%r15),%esi
mov    0x2dd5(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1269 <tcp_keepalive+0x139>
lea    0xe38(%rip),%rbx
jmp    1249 <tcp_keepalive+0x119>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
lea    0xdcc(%rip),%rsi
mov    %r14,%rdi
jmp    125d <tcp_keepalive+0x12d>
lea    0xddc(%rip),%rbx
jmp    1249 <tcp_keepalive+0x119>
lea    0xdef(%rip),%rbx
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <net_err@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 = [28 x i8] c"setsockopt TCP_KEEPIDLE: %s\00"
@TCP_KEEPINTVL = global i32 0
@.str.2 = [29 x i8] c"setsockopt TCP_KEEPINTVL: %s\00"
@TCP_KEEPCNT = global i32 0
@.str.3 = [27 x i8] c"setsockopt TCP_KEEPCNT: %s\00"
define i32 @tcp_keepalive(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 1, i32* %8
%10 = load i32, i32* %6
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_KEEPALIVE
%13 = call i32 @setsockopt(i32 %10, i32 %11, i32 %12, i32* %8, i32 4)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i32, i32* @errno
%18 = call i32 @strerror(i32 %17)
%19 = call i32 @net_err(i8* %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 -1, i32* %4
br label %61
20: ; preds = %3
%21 = load i32, i32* %7
store i32 %21, i32* %9
%22 = load i32, i32* %6
%23 = load i32, i32* @IPPROTO_TCP
%24 = load i32, i32* @TCP_KEEPIDLE
%25 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i32* %9, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %32
27: ; preds = %20
%28 = load i8*, i8** %5
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @net_err(i8* %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 -1, i32* %4
br label %61
32: ; preds = %20
%33 = load i32, i32* %7
%34 = sdiv i32 %33, 3
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 1, i32* %9
br label %38
38: ; preds = %37, %32
%39 = load i32, i32* %6
%40 = load i32, i32* @IPPROTO_TCP
%41 = load i32, i32* @TCP_KEEPINTVL
%42 = call i32 @setsockopt(i32 %39, i32 %40, i32 %41, i32* %9, i32 4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = load i8*, i8** %5
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = call i32 @net_err(i8* %45, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %47)
store i32 -1, i32* %4
br label %61
49: ; preds = %38
store i32 3, i32* %9
%50 = load i32, i32* %6
%51 = load i32, i32* @IPPROTO_TCP
%52 = load i32, i32* @TCP_KEEPCNT
%53 = call i32 @setsockopt(i32 %50, i32 %51, i32 %52, i32* %9, i32 4)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i8*, i8** %5
%57 = load i32, i32* @errno
%58 = call i32 @strerror(i32 %57)
%59 = call i32 @net_err(i8* %56, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i32 %58)
store i32 -1, i32* %4
br label %61
60: ; preds = %49
store i32 0, i32* %4
br label %61
61: ; preds = %60, %55, %44, %27, %15
%62 = load i32, i32* %4
ret i32 %62
}

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