#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 socklen_t;
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int s;
  int optval;
  socklen_t optlen = sizeof(optval);
  if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    perror("socket()");
    exit(EXIT_FAILURE);
  }
  if (getsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen) < 0) {
    perror("getsockopt()");
    close(s);
    exit(EXIT_FAILURE);
  }
  printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
  optval = 1;
  optlen = sizeof(optval);
  if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
    perror("setsockopt()");
    close(s);
    exit(EXIT_FAILURE);
  }
  printf("SO_KEEPALIVE set on socket\n");
  if (getsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, &optlen) < 0) {
    perror("getsockopt()");
    close(s);
    exit(EXIT_FAILURE);
  }
  printf("SO_KEEPALIVE is %s\n", (optval ? "ON" : "OFF"));
  close(s);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x4,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11cc <main+0x5c>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1060 <getsockopt@plt>
cmp    $0x0,%rax
jge    121a <main+0xaa>
lea    0xe0a(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%ecx
lea    0xe09(%rip),%rsi
lea    0xdff(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0xc(%rbp)
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%r8d
lea    -0xc(%rbp),%rcx
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    129c <main+0x12c>
lea    0xdb0(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
mov    0x2ceb(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1060 <getsockopt@plt>
cmp    $0x0,%rax
jge    12f8 <main+0x188>
lea    0xd2c(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%ecx
lea    0xd2b(%rip),%rsi
lea    0xd21(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
movl   $0x4,0xc(%rsp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
jns    11ca <main+0x4a>
lea    0xe40(%rip),%rdi
call   1080 <perror@plt>
jmp    1296 <main+0x116>
mov    %eax,%ebx
mov    0x2e15(%rip),%r15
mov    (%r15),%esi
mov    0x2dcb(%rip),%r12
mov    (%r12),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %eax,%edi
call   1070 <getsockopt@plt>
test   %rax,%rax
js     1283 <main+0x103>
cmpl   $0x0,0x8(%rsp)
lea    0xe26(%rip),%r13
lea    0xe1c(%rip),%r14
mov    %r14,%rsi
cmove  %r13,%rsi
lea    0xdfa(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movl   $0x1,0x8(%rsp)
movl   $0x4,0xc(%rsp)
mov    (%r15),%esi
mov    (%r12),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
test   %rax,%rax
jns    125a <main+0xda>
lea    0xdd9(%rip),%rdi
jmp    128a <main+0x10a>
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
mov    (%r15),%esi
mov    (%r12),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %ebx,%edi
call   1070 <getsockopt@plt>
test   %rax,%rax
jns    12a4 <main+0x124>
lea    0xd7f(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpl   $0x0,0x8(%rsp)
cmove  %r13,%r14
lea    0xd62(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [9 x i8] c"socket()\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.1 = [13 x i8] c"getsockopt()\00"
@.str.2 = [20 x i8] c"SO_KEEPALIVE is %s\0A\00"
@.str.3 = [3 x i8] c"ON\00"
@.str.4 = [4 x i8] c"OFF\00"
@.str.5 = [13 x i8] c"setsockopt()\00"
@.str.6 = [28 x i8] c"SO_KEEPALIVE set on socket\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 4, i32* %4
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = call i32 @socket(i32 %5, i32 %6, i32 %7)
store i32 %8, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i32, i32* %2
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_KEEPALIVE
%18 = call i64 @getsockopt(i32 %15, i32 %16, i32 %17, i32* %3, i32* %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %14
%27 = load i32, i32* %3
%28 = icmp ne i32 %27, 0
%29 = zext i1 %28 to i64
%30 = select i1 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %30)
store i32 1, i32* %3
store i32 4, i32* %4
%32 = load i32, i32* %2
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_KEEPALIVE
%35 = load i32, i32* %4
%36 = call i64 @setsockopt(i32 %32, i32 %33, i32 %34, i32* %3, i32 %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %26
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* %2
%41 = call i32 @close(i32 %40)
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %26
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%46 = load i32, i32* %2
%47 = load i32, i32* @SOL_SOCKET
%48 = load i32, i32* @SO_KEEPALIVE
%49 = call i64 @getsockopt(i32 %46, i32 %47, i32 %48, i32* %3, i32* %4)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* %2
%54 = call i32 @close(i32 %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %44
%58 = load i32, i32* %3
%59 = icmp ne i32 %58, 0
%60 = zext i1 %59 to i64
%61 = select i1 %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0)
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
%65 = load i32, i32* @EXIT_SUCCESS
%66 = call i32 @exit(i32 %65)
unreachable
}

LLVM IR (-O2):
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [9 x i8] c"socket()\00"
@EXIT_FAILURE = global i32 0
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.1 = [13 x i8] c"getsockopt()\00"
@.str.2 = [20 x i8] c"SO_KEEPALIVE is %s\0A\00"
@.str.3 = [3 x i8] c"ON\00"
@.str.4 = [4 x i8] c"OFF\00"
@.str.5 = [13 x i8] c"setsockopt()\00"
@EXIT_SUCCESS = global i32 0
@str = [27 x i8] c"SO_KEEPALIVE set on socket\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 4, i32* %2
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %0
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_KEEPALIVE
%17 = call i64 @getsockopt(i32 %8, i32 %15, i32 %16, i32* %1, i32* %2)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @close(i32 %8)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = load i32, i32* %1
%26 = icmp eq i32 %25, 0
%27 = select i1 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %27)
store i32 1, i32* %1
store i32 4, i32* %2
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_KEEPALIVE
%31 = call i64 @setsockopt(i32 %8, i32 %29, i32 %30, i32* %1, i32 4)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %24
%34 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%35 = call i32 @close(i32 %8)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %24
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%40 = load i32, i32* @SOL_SOCKET
%41 = load i32, i32* @SO_KEEPALIVE
%42 = call i64 @getsockopt(i32 %8, i32 %40, i32 %41, i32* %1, i32* %2)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%46 = call i32 @close(i32 %8)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %38
%50 = load i32, i32* %1
%51 = icmp eq i32 %50, 0
%52 = select i1 %51, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0)
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %52)
%54 = call i32 @close(i32 %8)
%55 = load i32, i32* @EXIT_SUCCESS
%56 = call i32 @exit(i32 %55)
unreachable
}