#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;
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 /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
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 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)
    err(1, "connect");
  return gdb_begin(fd);
}


x86 assembly (clang, -O0):
<gdb_begin_inet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <inet_aton@plt>
cmp    $0x0,%rax
jne    11c9 <gdb_begin_inet+0x59>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe3c(%rip),%rsi
call   1030 <errx@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1200 <gdb_begin_inet+0x90>
mov    $0x1,%edi
lea    0xe19(%rip),%rsi
call   1060 <err@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
je     122c <gdb_begin_inet+0xbc>
mov    $0x1,%edi
lea    0xdf4(%rip),%rsi
call   1060 <err@plt>
mov    -0x1c(%rbp),%edi
call   1070 <gdb_begin@plt>
add    $0x20,%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   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e54(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <inet_aton@plt>
test   %rax,%rax
jne    11c1 <gdb_begin_inet+0x51>
lea    0xe4c(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1030 <errx@plt>
mov    (%r14),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11eb <gdb_begin_inet+0x7b>
lea    0xe33(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
je     1212 <gdb_begin_inet+0xa2>
lea    0xe13(%rip),%rsi
mov    $0x1,%edi
call   1060 <err@plt>
mov    %ebx,%edi
call   1070 <gdb_begin@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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"
@.str.2 = [8 x i8] c"connect\00"
define %struct.gdb_conn* @gdb_begin_inet(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%9 = load i32, i32* %4
%10 = call i32 @htons(i32 %9)
store i32 %10, i32* %8
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
%12 = load i32, i32* @AF_INET
store i32 %12, i32* %11
%13 = load i8*, i8** %3
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%15 = call i64 @inet_aton(i8* %13, i32* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %3
%19 = call i32 @errx(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %2
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
%29 = load i32, i32* %6
%30 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%31 = call i64 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %6
%37 = call %struct.gdb_conn* @gdb_begin(i32 %36)
ret %struct.gdb_conn* %37
}

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"
@.str.2 = [8 x i8] c"connect\00"
define %struct.gdb_conn* @gdb_begin_inet(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 0, i32* %5
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%7 = tail call i32 @htons(i32 %1)
store i32 %7, i32* %6
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%9 = load i32, i32* @AF_INET
store i32 %9, i32* %8
%10 = call i64 @inet_aton(i8* %0, i32* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @errx(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %14
14: ; preds = %12, %2
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %14
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i64 @connect(i32 %17, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = call i32 @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
%28 = call %struct.gdb_conn* @gdb_begin(i32 %17)
ret %struct.gdb_conn* %28
}