#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 dummy;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ *ai_addr;
  int /*<<< orphan*/ *ai_next;
  int /*<<< orphan*/ *ai_canonname;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_NUMERICSERV;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int getTcpSendFd(char *hostName, char *portNum) {
  int sock, left, num, put, ecode;
  struct sockaddr_in *server;
  struct addrinfo hints, *addrlist;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_NUMERICSERV;
  hints.ai_protocol = 0;
  hints.ai_canonname = NULL;
  hints.ai_addr = NULL;
  hints.ai_next = NULL;
  ecode = getaddrinfo(hostName, portNum, &hints, &addrlist);
  if (ecode != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ecode));
    exit(1);
  }
  server = (struct sockaddr_in *)addrlist->ai_addr;
  if ((sock = socket(addrlist->ai_family, addrlist->ai_socktype, 0)) < 0) {
    perror("inet_wstream:socket");
    exit(1);
  }
  if (connect(sock, (struct sockaddr *)server, sizeof(struct sockaddr_in)) <
      0) {
    perror("inet_wstream:connect");
    exit(1);
  }
  return sock;
}


x86 assembly (clang, -O0):
<getTcpSendFd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x68(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1236 <getTcpSendFd+0xb6>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd9(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x68(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x68(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x68(%rbp),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1278 <getTcpSendFd+0xf8>
lea    0xda8(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    12a9 <getTcpSendFd+0x129>
lea    0xd8b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getTcpSendFd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movups %xmm0,0x28(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    1227 <getTcpSendFd+0xa7>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rbx
mov    (%rax),%esi
mov    0x4(%rax),%edi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1252 <getTcpSendFd+0xd2>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     125b <getTcpSendFd+0xdb>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2da2(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdc2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xdb8(%rip),%rdi
jmp    1262 <getTcpSendFd+0xe2>
lea    0xdc3(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32*, i32*, i32*, i64, i32 }
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_NUMERICSERV = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.1 = [20 x i8] c"inet_wstream:socket\00"
@.str.2 = [21 x i8] c"inet_wstream:connect\00"
define i32 @getTcpSendFd(i8* %0, i8* %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 48)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @AI_NUMERICSERV
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
%15 = bitcast i32** %14 to i8*
%16 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i8* @gai_strerror(i32 %16)
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load %struct.addrinfo*, %struct.addrinfo** %4
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%26 = bitcast i32** %25 to %struct.sockaddr**
%27 = load %struct.sockaddr*, %struct.sockaddr** %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %29, i32 %31, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %23
%35 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %23
%38 = call i64 @connect(i32 %32, %struct.sockaddr* %27, i32 4)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %37
ret i32 %32
}