#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*/ start;
typedef int /*<<< orphan*/ keepAlive;
typedef int /*<<< orphan*/ interval;
typedef int /*<<< orphan*/ count;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOL_TCP;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ TCP_KEEPCNT;
int /*<<< orphan*/ TCP_KEEPIDLE;
int /*<<< orphan*/ TCP_KEEPINTVL;
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
int set_tcp_keepAlive(int fd, int start, int interval, int count) {
  int keepAlive = 1;
  if (fd < 0 || start < 0 || interval < 0 || count < 0)
    return -1;
  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive,
                 sizeof(keepAlive)) == -1) {
    perror("setsockopt");
    return -1;
  }
  if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, (void *)&start, sizeof(start)) ==
      -1) {
    perror("setsockopt");
    return -1;
  }
  if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, (void *)&interval,
                 sizeof(interval)) == -1) {
    perror("setsockopt");
    return -1;
  }
  if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, (void *)&count, sizeof(count)) ==
      -1) {
    perror("setsockopt");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_tcp_keepAlive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1163 <set_tcp_keepAlive+0x43>
cmpl   $0x0,-0xc(%rbp)
jl     1163 <set_tcp_keepAlive+0x43>
cmpl   $0x0,-0x10(%rbp)
jl     1163 <set_tcp_keepAlive+0x43>
cmpl   $0x0,-0x14(%rbp)
jge    116f <set_tcp_keepAlive+0x4f>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <set_tcp_keepAlive+0x16a>
mov    -0x8(%rbp),%edi
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11b4 <set_tcp_keepAlive+0x94>
lea    0xe5d(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <set_tcp_keepAlive+0x16a>
mov    -0x8(%rbp),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11f9 <set_tcp_keepAlive+0xd9>
lea    0xe18(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <set_tcp_keepAlive+0x16a>
mov    -0x8(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    123e <set_tcp_keepAlive+0x11e>
lea    0xdd3(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <set_tcp_keepAlive+0x16a>
mov    -0x8(%rbp),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1283 <set_tcp_keepAlive+0x163>
lea    0xd8e(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128a <set_tcp_keepAlive+0x16a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_tcp_keepAlive>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,0x14(%rsp)
mov    %edx,0x10(%rsp)
mov    %ecx,0xc(%rsp)
movl   $0x1,0x8(%rsp)
or     %edi,%esi
or     %edx,%esi
mov    $0xffffffff,%eax
or     %ecx,%esi
js     11fa <set_tcp_keepAlive+0xda>
mov    %edi,%ebx
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11e9 <set_tcp_keepAlive+0xc9>
mov    0x2e64(%rip),%r14
mov    (%r14),%esi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11e9 <set_tcp_keepAlive+0xc9>
mov    (%r14),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11e9 <set_tcp_keepAlive+0xc9>
mov    (%r14),%esi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11fa <set_tcp_keepAlive+0xda>
lea    0xe10(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str = [11 x i8] c"setsockopt\00"
@SOL_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@TCP_KEEPINTVL = global i32 0
@TCP_KEEPCNT = global i32 0
define i32 @set_tcp_keepAlive(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 %3, i32* %7
%9 = bitcast i32* %8 to i8*
store i32 1, i32* %8
%10 = or i32 %1, %0
%11 = or i32 %10, %2
%12 = or i32 %11, %3
%13 = icmp sgt i32 %12, -1
br i1 %13, label %14, label %39
14: ; preds = %4
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_KEEPALIVE
%17 = call i32 @setsockopt(i32 %0, i32 %15, i32 %16, i8* %9, i32 4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %37, label %19
19: ; preds = %14
%20 = load i32, i32* @SOL_TCP
%21 = load i32, i32* @TCP_KEEPIDLE
%22 = bitcast i32* %5 to i8*
%23 = call i32 @setsockopt(i32 %0, i32 %20, i32 %21, i8* %22, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %37, label %25
25: ; preds = %19
%26 = load i32, i32* @SOL_TCP
%27 = load i32, i32* @TCP_KEEPINTVL
%28 = bitcast i32* %6 to i8*
%29 = call i32 @setsockopt(i32 %0, i32 %26, i32 %27, i8* %28, i32 4)
%30 = icmp eq i32 %29, -1
br i1 %30, label %37, label %31
31: ; preds = %25
%32 = load i32, i32* @SOL_TCP
%33 = load i32, i32* @TCP_KEEPCNT
%34 = bitcast i32* %7 to i8*
%35 = call i32 @setsockopt(i32 %0, i32 %32, i32 %33, i8* %34, i32 4)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %31, %25, %19, %14
%38 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %37, %31, %4
%40 = phi i32 [ -1, %4 ], [ 0, %31 ], [ -1, %37 ]
ret i32 %40
}