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

/* 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 setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int fnet_set_keepalive(int fd, int idle_time, int interval, int count) {
  int on_keep = 1;
  int s = -1;
  s = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on_keep, sizeof(on_keep));
  if (s)
    return s;
  s = setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle_time, sizeof(idle_time));
  if (s)
    return s;
  s = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));
  if (s)
    return s;
  return setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &count, sizeof(count));
}


x86 assembly (clang, -O0):
<fnet_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)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     116e <fnet_set_keepalive+0x5e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120d <fnet_set_keepalive+0xfd>
mov    -0x8(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
mov    0x2e3f(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11aa <fnet_set_keepalive+0x9a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120d <fnet_set_keepalive+0xfd>
mov    -0x8(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11e6 <fnet_set_keepalive+0xd6>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120d <fnet_set_keepalive+0xfd>
mov    -0x8(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fnet_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>
test   %eax,%eax
jne    11bc <fnet_set_keepalive+0xac>
mov    0x2e86(%rip),%r14
mov    (%r14),%esi
mov    0x2e5c(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    11bc <fnet_set_keepalive+0xac>
mov    (%r14),%esi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    11bc <fnet_set_keepalive+0xac>
mov    (%r14),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@TCP_KEEPINTVL = global i32 0
@TCP_KEEPCNT = global i32 0
define i32 @fnet_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
%11 = 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
store i32 -1, i32* %11
%12 = load i32, i32* %6
%13 = load i32, i32* @SOL_SOCKET
%14 = load i32, i32* @SO_KEEPALIVE
%15 = call i32 @setsockopt(i32 %12, i32 %13, i32 %14, i32* %10, i32 4)
store i32 %15, i32* %11
%16 = load i32, i32* %11
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %4
%19 = load i32, i32* %11
store i32 %19, i32* %5
br label %43
20: ; preds = %4
%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* %7, i32 4)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = load i32, i32* %11
store i32 %28, i32* %5
br label %43
29: ; preds = %20
%30 = load i32, i32* %6
%31 = load i32, i32* @IPPROTO_TCP
%32 = load i32, i32* @TCP_KEEPINTVL
%33 = call i32 @setsockopt(i32 %30, i32 %31, i32 %32, i32* %8, i32 4)
store i32 %33, i32* %11
%34 = load i32, i32* %11
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = load i32, i32* %11
store i32 %37, i32* %5
br label %43
38: ; preds = %29
%39 = load i32, i32* %6
%40 = load i32, i32* @IPPROTO_TCP
%41 = load i32, i32* @TCP_KEEPCNT
%42 = call i32 @setsockopt(i32 %39, i32 %40, i32 %41, i32* %9, i32 4)
store i32 %42, i32* %5
br label %43
43: ; preds = %38, %36, %27, %18
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
@TCP_KEEPINTVL = global i32 0
@TCP_KEEPCNT = global i32 0
define i32 @fnet_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 = icmp eq i32 %12, 0
br i1 %13, label %14, label %28
14: ; preds = %4
%15 = load i32, i32* @IPPROTO_TCP
%16 = load i32, i32* @TCP_KEEPIDLE
%17 = call i32 @setsockopt(i32 %0, i32 %15, i32 %16, i32* %5, i32 4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %14
%20 = load i32, i32* @IPPROTO_TCP
%21 = load i32, i32* @TCP_KEEPINTVL
%22 = call i32 @setsockopt(i32 %0, i32 %20, i32 %21, i32* %6, i32 4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @IPPROTO_TCP
%26 = load i32, i32* @TCP_KEEPCNT
%27 = call i32 @setsockopt(i32 %0, i32 %25, i32 %26, i32* %7, i32 4)
br label %28
28: ; preds = %19, %14, %4, %24
%29 = phi i32 [ %27, %24 ], [ %12, %4 ], [ %17, %14 ], [ %22, %19 ]
ret i32 %29
}