#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*/ uint16_t;
typedef int /*<<< orphan*/ tmp;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct gdb_conn {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr const *, int);
int /*<<< orphan*/ err(int, char *);
int /*<<< orphan*/ errx(int, char *, char const *);
struct gdb_conn *gdb_begin(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
struct gdb_conn *gdb_begin_inet(const char *addr, uint16_t port) {
  struct sockaddr_in sa = {
      .sin_family = AF_INET,
      .sin_port = htons(port),
  };
  if (inet_aton(addr, &sa.sin_addr) == 0)
    errx(1, "Invalid address: %s", addr);
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0)
    err(1, "socket");
  if (connect(fd, (const struct sockaddr *)&sa, sizeof(sa)) != 0) {
    close(fd);
    return NULL;
  }
  socklen_t tmp;
  tmp = 1;
  int r = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp));
  if (r) {
    perror("setsockopt");
    panic("setsockopt error");
  }
  tmp = 1;
  r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&tmp, sizeof(tmp));
  if (r) {
    perror("setsockopt");
    panic("setsockopt error");
  }
  return gdb_begin(fd);
}


x86 assembly (clang, -O0):
<gdb_begin_inet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <inet_aton@plt>
cmp    $0x0,%rax
jne    1209 <gdb_begin_inet+0x59>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdfc(%rip),%rsi
call   1040 <errx@plt>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1240 <gdb_begin_inet+0x90>
mov    $0x1,%edi
lea    0xdd9(%rip),%rsi
call   1090 <err@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
je     1270 <gdb_begin_inet+0xc0>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    131c <gdb_begin_inet+0x16c>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2d67(%rip),%rax
mov    (%rax),%esi
mov    0x2d26(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12c0 <gdb_begin_inet+0x110>
lea    0xd6c(%rip),%rdi
call   10b0 <perror@plt>
lea    0xd6b(%rip),%rdi
call   1060 <panic@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
mov    0x2d06(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1310 <gdb_begin_inet+0x160>
lea    0xd1c(%rip),%rdi
call   10b0 <perror@plt>
lea    0xd1b(%rip),%rdi
call   1060 <panic@plt>
mov    -0x24(%rbp),%edi
call   10a0 <gdb_begin@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gdb_begin_inet>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
movl   $0x0,0x8(%rsp)
mov    %esi,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e04(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <inet_aton@plt>
test   %rax,%rax
jne    1201 <gdb_begin_inet+0x51>
lea    0xe0c(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1040 <errx@plt>
mov    (%r14),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    122b <gdb_begin_inet+0x7b>
lea    0xdf3(%rip),%rsi
mov    $0x1,%edi
call   1090 <err@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
je     124c <gdb_begin_inet+0x9c>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    12b3 <gdb_begin_inet+0x103>
movl   $0x1,0x4(%rsp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    0x2d4c(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    12bb <gdb_begin_inet+0x10b>
movl   $0x1,0x4(%rsp)
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
mov    0x2d4c(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %eax,%eax
jne    12d5 <gdb_begin_inet+0x125>
mov    %ebx,%edi
call   10a0 <gdb_begin@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd59(%rip),%rdi
call   10b0 <perror@plt>
lea    0xd58(%rip),%rdi
call   1060 <panic@plt>
jmp    127c <gdb_begin_inet+0xcc>
lea    0xd3f(%rip),%rdi
call   10b0 <perror@plt>
lea    0xd3e(%rip),%rdi
call   1060 <panic@plt>
jmp    12ac <gdb_begin_inet+0xfc>

LLVM IR (-O0):
%struct.gdb_conn = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [20 x i8] c"Invalid address: %s\00"
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.2 = [11 x i8] c"setsockopt\00"
@.str.3 = [17 x i8] c"setsockopt error\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define %struct.gdb_conn* @gdb_begin_inet(i8* %0, i32 %1) {
%3 = alloca %struct.gdb_conn*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%12 = load i32, i32* %5
%13 = call i32 @htons(i32 %12)
store i32 %13, i32* %11
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%15 = load i32, i32* @AF_INET
store i32 %15, i32* %14
%16 = load i8*, i8** %4
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%18 = call i64 @inet_aton(i8* %16, i32* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32 @errx(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %20, %2
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
%32 = load i32, i32* %7
%33 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%34 = call i64 @connect(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %31
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
store %struct.gdb_conn* null, %struct.gdb_conn** %3
br label %64
39: ; preds = %31
store i32 1, i32* %8
%40 = load i32, i32* %7
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_KEEPALIVE
%43 = bitcast i32* %8 to i8*
%44 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i8* %43, i32 4)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %39
%48 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @panic(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %47, %39
store i32 1, i32* %8
%51 = load i32, i32* %7
%52 = load i32, i32* @IPPROTO_TCP
%53 = load i32, i32* @TCP_NODELAY
%54 = bitcast i32* %8 to i8*
%55 = call i32 @setsockopt(i32 %51, i32 %52, i32 %53, i8* %54, i32 4)
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %50
%59 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @panic(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %61
61: ; preds = %58, %50
%62 = load i32, i32* %7
%63 = call %struct.gdb_conn* @gdb_begin(i32 %62)
store %struct.gdb_conn* %63, %struct.gdb_conn** %3
br label %64
64: ; preds = %61, %36
%65 = load %struct.gdb_conn*, %struct.gdb_conn** %3
ret %struct.gdb_conn* %65
}

LLVM IR (-O2):
%struct.gdb_conn = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [20 x i8] c"Invalid address: %s\00"
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.2 = [11 x i8] c"setsockopt\00"
@.str.3 = [17 x i8] c"setsockopt error\00"
@IPPROTO_TCP = global i32 0
@TCP_NODELAY = global i32 0
define %struct.gdb_conn* @gdb_begin_inet(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%8 = tail call i32 @htons(i32 %1)
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%10 = load i32, i32* @AF_INET
store i32 %10, i32* %9
%11 = call i64 @inet_aton(i8* %0, i32* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @errx(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %13, %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i64 @connect(i32 %18, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 @close(i32 %18)
br label %47
28: ; preds = %22
%29 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%30 = load i32, i32* @SOL_SOCKET
%31 = load i32, i32* @SO_KEEPALIVE
%32 = call i32 @setsockopt(i32 %18, i32 %30, i32 %31, i8* %29, i32 4)
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @panic(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %34, %28
store i32 1, i32* %4
%38 = load i32, i32* @IPPROTO_TCP
%39 = load i32, i32* @TCP_NODELAY
%40 = call i32 @setsockopt(i32 %18, i32 %38, i32 %39, i8* %29, i32 4)
%41 = icmp eq i32 %40, 0
br i1 %41, label %45, label %42
42: ; preds = %37
%43 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%44 = call i32 @panic(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %42, %37
%46 = call %struct.gdb_conn* @gdb_begin(i32 %18)
br label %47
47: ; preds = %45, %26
%48 = phi %struct.gdb_conn* [ null, %26 ], [ %46, %45 ]
ret %struct.gdb_conn* %48
}