#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 sockaddr_in {
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, int /*<<< orphan*/, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ htons(short);
int /*<<< orphan*/ memcpy(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ panic(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int dial(const char *host, short port) {
  int f;
  struct sockaddr_in sin;
  struct addrinfo *ai, hai = {0};
  hai.ai_family = AF_INET;
  hai.ai_socktype = SOCK_STREAM;
  if (getaddrinfo(host, 0, &hai, &ai))
    panic("Cannot resolve host.");
  memcpy(&sin, ai->ai_addr, sizeof sin);
  sin.sin_port = htons(port);
  freeaddrinfo(ai);
  f = socket(AF_INET, SOCK_STREAM, 0);
  if (f < 0)
    panic("Cannot create socket.");
  if (connect(f, (struct sockaddr *)&sin, sizeof sin) < 0)
    panic("Cannot connect to host.");
  return f;
}


x86 assembly (clang, -O0):
<dial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
lea    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11e4 <dial+0x64>
lea    0xe21(%rip),%rdi
call   1040 <panic@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
movswl -0xa(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    123f <dial+0xbf>
lea    0xddb(%rip),%rdi
call   1040 <panic@plt>
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    1266 <dial+0xe6>
lea    0xdca(%rip),%rdi
call   1040 <panic@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dial>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e5e(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2e40(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
xor    %esi,%esi
call   1070 <getaddrinfo@plt>
test   %rax,%rax
je     11c1 <dial+0x51>
lea    0xe44(%rip),%rdi
call   1040 <panic@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
mov    $0x4,%edx
call   1050 <memcpy@plt>
movswl %bx,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    (%r14),%edi
mov    (%r15),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1209 <dial+0x99>
lea    0xe11(%rip),%rdi
call   1040 <panic@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1060 <connect@plt>
test   %rax,%rax
jns    1229 <dial+0xb9>
lea    0xe07(%rip),%rdi
call   1040 <panic@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Cannot resolve host.\00"
@.str.1 = [22 x i8] c"Cannot create socket.\00"
@.str.2 = [24 x i8] c"Cannot connect to host.\00"
define i32 @dial(i8* %0, i16 signext %1) {
%3 = alloca i8*
%4 = alloca i16
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo
store i8* %0, i8** %3
store i16 %1, i16* %4
%9 = bitcast %struct.addrinfo* %8 to i8*
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i8*, i8** %3
%15 = call i64 @getaddrinfo(i8* %14, i32 0, %struct.addrinfo* %8, %struct.addrinfo** %7)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @panic(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %7
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @memcpy(%struct.sockaddr_in* %6, i32 %22, i32 4)
%24 = load i16, i16* %4
%25 = call i32 @htons(i16 signext %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %25, i32* %26
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = call i32 @freeaddrinfo(%struct.addrinfo* %27)
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %19
%35 = call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %19
%37 = load i32, i32* %5
%38 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%39 = call i64 @connect(i32 %37, %struct.sockaddr* %38, i32 4)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @panic(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [21 x i8] c"Cannot resolve host.\00"
@.str.1 = [22 x i8] c"Cannot create socket.\00"
@.str.2 = [24 x i8] c"Cannot connect to host.\00"
define i32 @dial(i8* %0, i16 signext %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.addrinfo** %4 to i8*
%8 = bitcast %struct.addrinfo* %5 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i64 @getaddrinfo(i8* %0, i32 0, %struct.addrinfo* %5, %struct.addrinfo** %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @panic(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load %struct.addrinfo*, %struct.addrinfo** %4
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @memcpy(%struct.sockaddr_in* %3, i32 %20, i32 4)
%22 = call i32 @htons(i16 signext %1)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = load %struct.addrinfo*, %struct.addrinfo** %4
%25 = call i32 @freeaddrinfo(%struct.addrinfo* %24)
%26 = load i32, i32* @AF_INET
%27 = load i32, i32* @SOCK_STREAM
%28 = call i32 @socket(i32 %26, i32 %27, i32 0)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %17
%31 = call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %17
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @connect(i32 %28, %struct.sockaddr* %33, i32 4)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @panic(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
ret i32 %28
}