#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_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct addrinfo hints, *res;
  int sockfd;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if (getaddrinfo(argv[1], argv[2], &hints, &res) < 0) {
    printf("Failed to get address info: %s", strerror(errno));
    exit(errno);
  }
  if ((sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) <
      0) {
    printf("Failed to create socket: %s", strerror(errno));
    exit(errno);
  }
  if (connect(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
    printf("Failed to connect: %s", strerror(errno));
    exit(errno);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
jge    1200 <main+0x90>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe13(%rip),%rdi
call   1030 <printf@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x30(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    1251 <main+0xe1>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xde1(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
cmp    $0x0,%rax
jge    129b <main+0x12b>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xdb3(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rsi
lea    0x8(%rsp),%rcx
mov    %r14,%rdx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
js     11f9 <main+0x89>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1090 <socket@plt>
test   %eax,%eax
js     1210 <main+0xa0>
mov    0x8(%rsp),%rcx
mov    (%rcx),%edx
mov    0x4(%rcx),%esi
mov    %eax,%edi
call   1060 <connect@plt>
test   %rax,%rax
js     1227 <main+0xb7>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dd0(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
lea    0xdf2(%rip),%rdi
jmp    123c <main+0xcc>
mov    0x2db9(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
lea    0xdfa(%rip),%rdi
jmp    123c <main+0xcc>
mov    0x2da2(%rip),%rbx
mov    (%rbx),%edi
call   1080 <strerror@plt>
lea    0xdff(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    (%rbx),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"Failed to get address info: %s\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"Failed to create socket: %s\00"
@.str.2 = [22 x i8] c"Failed to connect: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 20)
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = call i64 @getaddrinfo(i8* %16, i8* %19, %struct.addrinfo* %6, %struct.addrinfo** %7)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = load i32, i32* @errno
%24 = call i8* @strerror(i32 %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @errno
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %2
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 4
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %7
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 3
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %7
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 2
%37 = load i32, i32* %36
%38 = call i32 @socket(i32 %31, i32 %34, i32 %37)
store i32 %38, i32* %8
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %28
%41 = load i32, i32* @errno
%42 = call i8* @strerror(i32 %41)
%43 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @errno
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %28
%47 = load i32, i32* %8
%48 = load %struct.addrinfo*, %struct.addrinfo** %7
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 1
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %7
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @connect(i32 %47, i32 %50, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %46
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %58)
%60 = load i32, i32* @errno
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %46
ret i32 0
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [31 x i8] c"Failed to get address info: %s\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"Failed to create socket: %s\00"
@.str.2 = [22 x i8] c"Failed to connect: %s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 20)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = call i64 @getaddrinfo(i8* %13, i8* %15, %struct.addrinfo* %3, %struct.addrinfo** %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @errno
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %24
%35 = load i32, i32* @errno
%36 = call i8* @strerror(i32 %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @errno
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %24
%41 = load %struct.addrinfo*, %struct.addrinfo** %4
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i64 0, i32 1
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i64 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @connect(i32 %32, i32 %43, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %40
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = load i32, i32* @errno
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %40
ret i32 0
}