#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*/ on;
typedef int /*<<< orphan*/ keep_interval;
typedef int /*<<< orphan*/ keep_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 *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int set_keepalive(int sock, int keep_count, int keep_idle, int keep_interval) {
  int on = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on)) < 0) {
    perror("SO_KEEPALIVE");
    return 0;
  }
  if (setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &keep_idle, sizeof(keep_count)) <
      0) {
    perror("TCP_KEEPIDLE");
    return 0;
  }
  if (setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &keep_count, sizeof(keep_count)) <
      0) {
    perror("TCP_KEEPCNT");
    return 0;
  }
  if (setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &keep_interval,
                 sizeof(keep_interval)) < 0) {
    perror("TCP_KEEPINTVL");
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<set_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)
mov    -0x8(%rbp),%edi
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1181 <set_keepalive+0x61>
lea    0xe90(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    125a <set_keepalive+0x13a>
mov    -0x8(%rbp),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11c7 <set_keepalive+0xa7>
lea    0xe57(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    125a <set_keepalive+0x13a>
mov    -0x8(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    120d <set_keepalive+0xed>
lea    0xe1e(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    125a <set_keepalive+0x13a>
mov    -0x8(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1253 <set_keepalive+0x133>
lea    0xde4(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    125a <set_keepalive+0x13a>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_keepalive>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %esi,0x14(%rsp)
mov    %edx,0x10(%rsp)
mov    %ecx,0xc(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11e9 <set_keepalive+0xc9>
mov    0x2e71(%rip),%r14
mov    (%r14),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11f2 <set_keepalive+0xd2>
mov    (%r14),%esi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     11fb <set_keepalive+0xdb>
mov    (%r14),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jns    1209 <set_keepalive+0xe9>
lea    0xe3f(%rip),%rdi
jmp    1202 <set_keepalive+0xe2>
lea    0xe10(%rip),%rdi
jmp    1202 <set_keepalive+0xe2>
lea    0xe14(%rip),%rdi
jmp    1202 <set_keepalive+0xe2>
lea    0xe18(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str = [13 x i8] c"SO_KEEPALIVE\00"
@SOL_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@.str.1 = [13 x i8] c"TCP_KEEPIDLE\00"
@TCP_KEEPCNT = global i32 0
@.str.2 = [12 x i8] c"TCP_KEEPCNT\00"
@TCP_KEEPINTVL = global i32 0
@.str.3 = [14 x i8] c"TCP_KEEPINTVL\00"
define i32 @set_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 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_KEEPALIVE
%14 = call i64 @setsockopt(i32 %11, i32 %12, i32 %13, i32* %10, i32 4)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %43
18: ; preds = %4
%19 = load i32, i32* %6
%20 = load i32, i32* @SOL_TCP
%21 = load i32, i32* @TCP_KEEPIDLE
%22 = call i64 @setsockopt(i32 %19, i32 %20, i32 %21, i32* %8, i32 4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %43
26: ; preds = %18
%27 = load i32, i32* %6
%28 = load i32, i32* @SOL_TCP
%29 = load i32, i32* @TCP_KEEPCNT
%30 = call i64 @setsockopt(i32 %27, i32 %28, i32 %29, i32* %7, i32 4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %5
br label %43
34: ; preds = %26
%35 = load i32, i32* %6
%36 = load i32, i32* @SOL_TCP
%37 = load i32, i32* @TCP_KEEPINTVL
%38 = call i64 @setsockopt(i32 %35, i32 %36, i32 %37, i32* %9, i32 4)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %5
br label %43
42: ; preds = %34
store i32 1, i32* %5
br label %43
43: ; preds = %42, %40, %32, %24, %16
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str = [13 x i8] c"SO_KEEPALIVE\00"
@SOL_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@.str.1 = [13 x i8] c"TCP_KEEPIDLE\00"
@TCP_KEEPCNT = global i32 0
@.str.2 = [12 x i8] c"TCP_KEEPCNT\00"
@TCP_KEEPINTVL = global i32 0
@.str.3 = [14 x i8] c"TCP_KEEPINTVL\00"
define i32 @set_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 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_KEEPALIVE
%12 = call i64 @setsockopt(i32 %0, i32 %10, i32 %11, i32* %8, i32 4)
%13 = icmp slt i64 %12, 0
br i1 %13, label %29, label %14
14: ; preds = %4
%15 = load i32, i32* @SOL_TCP
%16 = load i32, i32* @TCP_KEEPIDLE
%17 = call i64 @setsockopt(i32 %0, i32 %15, i32 %16, i32* %6, i32 4)
%18 = icmp slt i64 %17, 0
br i1 %18, label %29, label %19
19: ; preds = %14
%20 = load i32, i32* @SOL_TCP
%21 = load i32, i32* @TCP_KEEPCNT
%22 = call i64 @setsockopt(i32 %0, i32 %20, i32 %21, i32* %5, i32 4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %29, label %24
24: ; preds = %19
%25 = load i32, i32* @SOL_TCP
%26 = load i32, i32* @TCP_KEEPINTVL
%27 = call i64 @setsockopt(i32 %0, i32 %25, i32 %26, i32* %7, i32 4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24, %19, %14, %4
%30 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), %19 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %24 ]
%31 = call i32 @perror(i8* %30)
br label %32
32: ; preds = %29, %24
%33 = phi i32 [ 1, %24 ], [ 0, %29 ]
ret i32 %33
}