#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*/ enable;
typedef int /*<<< orphan*/ delay;

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ TCP_KEEPIDLE;
int api_error_translate(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, ...);
int api_tcp_keepalive(int fd, int enable, unsigned int delay) {
  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)))
    return api_error_translate(errno);
  if (enable &&
      setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &delay, sizeof(delay)))
    return api_error_translate(errno);
  return 0;
}


x86 assembly (clang, -O0):
<api_tcp_keepalive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ead(%rip),%rax
mov    (%rax),%esi
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1177 <api_tcp_keepalive+0x57>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1040 <api_error_translate@plt>
mov    %eax,-0x4(%rbp)
jmp    11ce <api_tcp_keepalive+0xae>
cmpl   $0x0,-0xc(%rbp)
je     11c7 <api_tcp_keepalive+0xa7>
mov    -0x8(%rbp),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e34(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11c7 <api_tcp_keepalive+0xa7>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1040 <api_error_translate@plt>
mov    %eax,-0x4(%rbp)
jmp    11ce <api_tcp_keepalive+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<api_tcp_keepalive>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    %esi,(%rsp)
mov    %edx,0x4(%rsp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%esi
mov    0x2e7c(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %rsp,%rcx
mov    $0x4,%r8d
xor    %eax,%eax
call   1030 <setsockopt@plt>
test   %rax,%rax
je     116e <api_tcp_keepalive+0x4e>
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1040 <api_error_translate@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
cmpl   $0x0,(%rsp)
je     1165 <api_tcp_keepalive+0x45>
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    1155 <api_tcp_keepalive+0x35>
jmp    1165 <api_tcp_keepalive+0x45>

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@errno = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
define i32 @api_tcp_keepalive(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i32, i32* %5
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_KEEPALIVE
%11 = call i64 (i32, i32, i32, ...) @setsockopt(i32 %8, i32 %9, i32 %10, i32* %6, i64 4)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %3
%14 = load i32, i32* @errno
%15 = call i32 @api_error_translate(i32 %14)
store i32 %15, i32* %4
br label %29
16: ; preds = %3
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load i32, i32* %5
%21 = load i32, i32* @IPPROTO_TCP
%22 = load i32, i32* @TCP_KEEPIDLE
%23 = call i64 (i32, i32, i32, ...) @setsockopt(i32 %20, i32 %21, i32 %22, i32* %7, i64 4)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i32, i32* @errno
%27 = call i32 @api_error_translate(i32 %26)
store i32 %27, i32* %4
br label %29
28: ; preds = %19, %16
store i32 0, i32* %4
br label %29
29: ; preds = %28, %25, %13
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@errno = global i32 0
@IPPROTO_TCP = global i32 0
@TCP_KEEPIDLE = global i32 0
define i32 @api_tcp_keepalive(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
store i32 %1, i32* %4
store i32 %2, i32* %5
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_KEEPALIVE
%8 = call i64 (i32, i32, i32, ...) @setsockopt(i32 %0, i32 %6, i32 %7, i32* %4, i64 4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %18
10: ; preds = %3
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %10
%14 = load i32, i32* @IPPROTO_TCP
%15 = load i32, i32* @TCP_KEEPIDLE
%16 = call i64 (i32, i32, i32, ...) @setsockopt(i32 %0, i32 %14, i32 %15, i32* %5, i64 4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13, %3
%19 = load i32, i32* @errno
%20 = call i32 @api_error_translate(i32 %19)
br label %21
21: ; preds = %18, %10, %13
%22 = phi i32 [ 0, %13 ], [ 0, %10 ], [ %20, %18 ]
ret i32 %22
}