#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*/ SOL_SOCKET;
int /*<<< orphan*/ SOL_TCP;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ TCP_KEEPCNT;
int /*<<< orphan*/ TCP_KEEPIDLE;
int /*<<< orphan*/ TCP_KEEPINTVL;
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int set_keepalive(int sock, int idel, int count, int interval) {
  int k = 1;
  setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &k, sizeof(int));
  setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &idel, sizeof(int));
  setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &count, sizeof(int));
  setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &interval, sizeof(int));
  return 0;
}


x86 assembly (clang, -O0):
<set_keepalive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2eb3(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x4(%rbp),%edi
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x4(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x4(%rbp),%edi
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e2e(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
xor    %eax,%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    0x2eb4(%rip),%rax
mov    (%rax),%esi
mov    0x2e73(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x2e8a(%rip),%r14
mov    (%r14),%esi
mov    0x2e60(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r14),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%r14),%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@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
@SOL_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@TCP_KEEPCNT = global i32 0
@TCP_KEEPINTVL = global i32 0
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
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 1, i32* %9
%10 = load i32, i32* %5
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_KEEPALIVE
%13 = call i32 @setsockopt(i32 %10, i32 %11, i32 %12, i32* %9, i32 4)
%14 = load i32, i32* %5
%15 = load i32, i32* @SOL_TCP
%16 = load i32, i32* @TCP_KEEPIDLE
%17 = call i32 @setsockopt(i32 %14, i32 %15, i32 %16, i32* %6, i32 4)
%18 = load i32, i32* %5
%19 = load i32, i32* @SOL_TCP
%20 = load i32, i32* @TCP_KEEPCNT
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %7, i32 4)
%22 = load i32, i32* %5
%23 = load i32, i32* @SOL_TCP
%24 = load i32, i32* @TCP_KEEPINTVL
%25 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i32* %8, i32 4)
ret i32 0
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@SOL_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@TCP_KEEPCNT = global i32 0
@TCP_KEEPINTVL = global i32 0
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 i32 @setsockopt(i32 %0, i32 %10, i32 %11, i32* %8, i32 4)
%13 = load i32, i32* @SOL_TCP
%14 = load i32, i32* @TCP_KEEPIDLE
%15 = call i32 @setsockopt(i32 %0, i32 %13, i32 %14, i32* %5, i32 4)
%16 = load i32, i32* @SOL_TCP
%17 = load i32, i32* @TCP_KEEPCNT
%18 = call i32 @setsockopt(i32 %0, i32 %16, i32 %17, i32* %6, i32 4)
%19 = load i32, i32* @SOL_TCP
%20 = load i32, i32* @TCP_KEEPINTVL
%21 = call i32 @setsockopt(i32 %0, i32 %19, i32 %20, i32* %7, i32 4)
ret i32 0
}