#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 */
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ optval;
typedef int /*<<< orphan*/ h;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int s;
  int optval;
  socklen_t optlen = sizeof(optval);
  struct addrinfo *ai = 0;
  struct addrinfo h;
  char buf[128];
  int rv = 0;
  char *addr = "8.8.8.8";
  char *port = "53";
  if (argc == 3) {
    addr = argv[1];
    port = argv[2];
  } else if (argc == 2) {
    addr = argv[1];
  }
  memset(&h, 0, sizeof(h));
  h.ai_family = AF_INET;
  h.ai_socktype = SOCK_STREAM;
  if ((rv = getaddrinfo(addr, port, &h, &ai)) != 0) {
    fprintf(stderr, gai_strerror(rv));
    exit(1);
  }
  if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket()");
    exit(EXIT_FAILURE);
  }
  rv = connect(s, ai->ai_addr, ai->ai_addrlen);
  if (rv != 0) {
    perror("connect()");
    exit(EXIT_FAILURE);
  }
  read(s, buf, 16);
  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"));
  if (optval) {
  }
  close(s);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0xc4(%rbp)
lea    0xe07(%rip),%rax
mov    %rax,-0xd0(%rbp)
lea    0xe01(%rip),%rax
mov    %rax,-0xd8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    123b <main+0x7b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xd0(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0xd8(%rbp)
jmp    1259 <main+0x99>
cmpl   $0x2,-0x8(%rbp)
jne    1254 <main+0x94>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xd0(%rbp)
jmp    1259 <main+0x99>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0xd0(%rbp),%rdi
mov    -0xd8(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0xc4(%rbp)
cmp    $0x0,%eax
je     12dd <main+0x11d>
mov    0x2d1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xdc(%rbp)
mov    -0xc4(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0xdc(%rbp),%edi
mov    %rax,%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    131c <main+0x15c>
lea    0xd02(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   10c0 <connect@plt>
mov    %eax,-0xc4(%rbp)
cmpl   $0x0,-0xc4(%rbp)
je     135e <main+0x19e>
lea    0xcc9(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x10,%edx
call   1070 <read@plt>
mov    -0x14(%rbp),%edi
mov    0x2c6c(%rip),%rax
mov    (%rax),%esi
mov    0x2c1b(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
lea    -0x1c(%rbp),%r8
call   1080 <getsockopt@plt>
cmp    $0x0,%rax
jge    13c0 <main+0x200>
lea    0xc79(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2bff(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%ecx
lea    0xc78(%rip),%rsi
lea    0xc6e(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
lea    0xc4c(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
je     13f3 <main+0x233>
jmp    13f3 <main+0x233>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2bae(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xa8,%rsp
movl   $0x4,(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe23(%rip),%rbx
cmp    $0x2,%edi
je     11f9 <main+0x39>
cmp    $0x3,%edi
jne    11ff <main+0x3f>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%rbx
jmp    1206 <main+0x46>
mov    0x8(%rsi),%r14
jmp    1206 <main+0x46>
lea    0xdfa(%rip),%r14
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2d9a(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10d0 <getaddrinfo@plt>
test   %eax,%eax
jne    1271 <main+0xb1>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
mov    (%r12),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    1295 <main+0xd5>
lea    0xd9f(%rip),%rdi
jmp    12ff <main+0x13f>
mov    0x2d58(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    %ebx,%edi
mov    %rax,%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   10c0 <connect@plt>
test   %eax,%eax
jne    12f8 <main+0x138>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1070 <read@plt>
mov    0x2d24(%rip),%rax
mov    (%rax),%esi
mov    0x2cd3(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %rsp,%r8
mov    %ebx,%edi
call   1080 <getsockopt@plt>
test   %rax,%rax
jns    1312 <main+0x152>
lea    0xd34(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1304 <main+0x144>
lea    0xd15(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,0x4(%rsp)
lea    0xd24(%rip),%rax
lea    0xd1a(%rip),%rsi
cmove  %rax,%rsi
lea    0xcfb(%rip),%rdi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
@.str = [8 x i8] c"8.8.8.8\00"
@.str.1 = [3 x i8] c"53\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@PF_INET = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [10 x i8] c"connect()\00"
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.4 = [13 x i8] c"getsockopt()\00"
@.str.5 = [20 x i8] c"SO_KEEPALIVE is %s\0A\00"
@.str.6 = [3 x i8] c"ON\00"
@.str.7 = [4 x i8] c"OFF\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo
%11 = alloca [128 x i8]
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 4, i32* %8
store %struct.addrinfo* null, %struct.addrinfo** %9
store i32 0, i32* %12
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %13
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8** %14
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 3
br i1 %16, label %17, label %24
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %13
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
store i8* %23, i8** %14
br label %32
24: ; preds = %2
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 2
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %13
br label %31
31: ; preds = %27, %24
br label %32
32: ; preds = %31, %17
%33 = call i32 @memset(%struct.addrinfo* %10, i32 0, i32 16)
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 3
store i32 %34, i32* %35
%36 = load i32, i32* @SOCK_STREAM
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %10, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i8*, i8** %13
%39 = load i8*, i8** %14
%40 = call i32 @getaddrinfo(i8* %38, i8* %39, %struct.addrinfo* %10, %struct.addrinfo** %9)
store i32 %40, i32* %12
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %32
%43 = load i32, i32* @stderr
%44 = load i32, i32* %12
%45 = call i8* @gai_strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %32
%49 = load i32, i32* @PF_INET
%50 = load i32, i32* @SOCK_STREAM
%51 = call i32 @socket(i32 %49, i32 %50, i32 0)
store i32 %51, i32* %6
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %48
%54 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %48
%58 = load i32, i32* %6
%59 = load %struct.addrinfo*, %struct.addrinfo** %9
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i32 0, i32 1
%61 = load i32, i32* %60
%62 = load %struct.addrinfo*, %struct.addrinfo** %9
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = call i32 @connect(i32 %58, i32 %61, i32 %64)
store i32 %65, i32* %12
%66 = load i32, i32* %12
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %72
68: ; preds = %57
%69 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%70 = load i32, i32* @EXIT_FAILURE
%71 = call i32 @exit(i32 %70)
unreachable
72: ; preds = %57
%73 = load i32, i32* %6
%74 = getelementptr inbounds [128 x i8], [128 x i8]* %11, i64 0, i64 0
%75 = call i32 @read(i32 %73, i8* %74, i32 16)
%76 = load i32, i32* %6
%77 = load i32, i32* @SOL_SOCKET
%78 = load i32, i32* @SO_KEEPALIVE
%79 = call i64 @getsockopt(i32 %76, i32 %77, i32 %78, i32* %7, i32* %8)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %87
81: ; preds = %72
%82 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%83 = load i32, i32* %6
%84 = call i32 @close(i32 %83)
%85 = load i32, i32* @EXIT_FAILURE
%86 = call i32 @exit(i32 %85)
unreachable
87: ; preds = %72
%88 = load i32, i32* %7
%89 = icmp ne i32 %88, 0
%90 = zext i1 %89 to i64
%91 = select i1 %89, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0)
%92 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %91)
%93 = load i32, i32* %7
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %96
95: ; preds = %87
br label %96
96: ; preds = %95, %87
%97 = load i32, i32* %6
%98 = call i32 @close(i32 %97)
%99 = load i32, i32* @EXIT_SUCCESS
%100 = call i32 @exit(i32 %99)
unreachable
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@.str = [8 x i8] c"8.8.8.8\00"
@.str.1 = [3 x i8] c"53\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@PF_INET = global i32 0
@.str.2 = [9 x i8] c"socket()\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [10 x i8] c"connect()\00"
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.4 = [13 x i8] c"getsockopt()\00"
@.str.5 = [20 x i8] c"SO_KEEPALIVE is %s\0A\00"
@.str.6 = [3 x i8] c"ON\00"
@.str.7 = [4 x i8] c"OFF\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo
%7 = alloca [128 x i8]
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%10 = bitcast %struct.addrinfo** %5 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %5
%11 = bitcast %struct.addrinfo* %6 to i8*
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
switch i32 %0, label %21 [
i32 3, label %13
i32 2, label %18
]
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
br label %21
18: ; preds = %2
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
br label %21
21: ; preds = %2, %18, %13
%22 = phi i8* [ %15, %13 ], [ %20, %18 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %2 ]
%23 = phi i8* [ %17, %13 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %18 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %2 ]
%24 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 16)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 3
store i32 %25, i32* %26
%27 = load i32, i32* @SOCK_STREAM
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @getaddrinfo(i8* %22, i8* %23, %struct.addrinfo* %6, %struct.addrinfo** %5)
%30 = icmp eq i32 %29, 0
br i1 %30, label %36, label %31
31: ; preds = %21
%32 = load i32, i32* @stderr
%33 = call i8* @gai_strerror(i32 %29)
%34 = call i32 @fprintf(i32 %32, i8* %33)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %21
%37 = load i32, i32* @PF_INET
%38 = load i32, i32* @SOCK_STREAM
%39 = call i32 @socket(i32 %37, i32 %38, i32 0)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %36
%46 = load %struct.addrinfo*, %struct.addrinfo** %5
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i64 0, i32 1
%48 = load i32, i32* %47
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i64 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @connect(i32 %39, i32 %48, i32 %50)
%52 = icmp eq i32 %51, 0
br i1 %52, label %57, label %53
53: ; preds = %45
%54 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %45
%58 = call i32 @read(i32 %39, i8* %12, i32 16)
%59 = load i32, i32* @SOL_SOCKET
%60 = load i32, i32* @SO_KEEPALIVE
%61 = call i64 @getsockopt(i32 %39, i32 %59, i32 %60, i32* %3, i32* %4)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 @close(i32 %39)
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %57
%69 = load i32, i32* %3
%70 = icmp eq i32 %69, 0
%71 = select i1 %70, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0)
%72 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %71)
%73 = call i32 @close(i32 %39)
%74 = load i32, i32* @EXIT_SUCCESS
%75 = call i32 @exit(i32 %74)
unreachable
}