#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*/ msg;
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(int /*<<< orphan*/ *, int /*<<< orphan*/ *, 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 write(int, char *, int);
int sendTcp(char *hostName, char *portNum, char msg[34]) {
  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(NULL, NULL, &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);
  }
  left = sizeof(msg);
  put = 0;
  while (left > 0) {
    if ((num = write(sock, msg + put, left)) < 0) {
      perror("inet_wstream:write");
      exit(1);
    } else
      left -= num;
    put += num;
  }
  exit(0);
}


x86 assembly (clang, -O0):
<sendTcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x68(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x60(%rbp)
movq   $0x0,-0x58(%rbp)
xor    %eax,%eax
mov    %eax,%esi
lea    -0x68(%rbp),%rdx
lea    -0x70(%rbp),%rcx
mov    %rsi,%rdi
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     124c <sendTcp+0xbc>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xdc3(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x70(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x70(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x70(%rbp),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    128e <sendTcp+0xfe>
lea    0xd92(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    12bf <sendTcp+0x12f>
lea    0xd75(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x8,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1328 <sendTcp+0x198>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
movslq -0x28(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    130f <sendTcp+0x17f>
lea    0xd3a(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12cd <sendTcp+0x13d>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<sendTcp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
movups %xmm0,0x20(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
xor    %esi,%esi
mov    %rbx,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    1219 <sendTcp+0x89>
mov    (%rsp),%rax
mov    0x8(%rax),%rbx
mov    (%rax),%esi
mov    0x4(%rax),%edi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
jns    1244 <sendTcp+0xb4>
lea    0xdfa(%rip),%rdi
jmp    1298 <sendTcp+0x108>
mov    0x2db0(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdd0(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
test   %rax,%rax
jns    1264 <sendTcp+0xd4>
lea    0xdc3(%rip),%rdi
jmp    1298 <sendTcp+0x108>
xor    %ebp,%ebp
mov    $0x8,%ebx
nopl   0x0(%rax,%rax,1)
movslq %ebp,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     1291 <sendTcp+0x101>
add    %eax,%ebp
sub    %eax,%ebx
jg     1270 <sendTcp+0xe0>
xor    %edi,%edi
call   1080 <exit@plt>
lea    0xda2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <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"
@.str.3 = [19 x i8] c"inet_wstream:write\00"
define i32 @sendTcp(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in*
%13 = alloca %struct.addrinfo
%14 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%15 = call i32 @memset(%struct.addrinfo* %13, i32 0, i32 48)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @AI_NUMERICSERV
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 6
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 5
store i64 0, i64* %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 4
store i32* null, i32** %23
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 2
store i32* null, i32** %24
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i32 0, i32 3
store i32* null, i32** %25
%26 = call i32 @getaddrinfo(i32* null, i32* null, %struct.addrinfo* %13, %struct.addrinfo** %14)
store i32 %26, i32* %11
%27 = load i32, i32* %11
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %3
%30 = load i32, i32* @stderr
%31 = load i32, i32* %11
%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 = %3
%36 = load %struct.addrinfo*, %struct.addrinfo** %14
%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** %12
%40 = load %struct.addrinfo*, %struct.addrinfo** %14
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 1
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %14
%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* %7
%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* %7
%53 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%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
store i32 8, i32* %8
store i32 0, i32* %10
br label %61
61: ; preds = %80, %60
%62 = load i32, i32* %8
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %84
64: ; preds = %61
%65 = load i32, i32* %7
%66 = load i8*, i8** %6
%67 = load i32, i32* %10
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = load i32, i32* %8
%71 = call i32 @write(i32 %65, i8* %69, i32 %70)
store i32 %71, i32* %9
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %64
%74 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %64
%77 = load i32, i32* %9
%78 = load i32, i32* %8
%79 = sub nsw i32 %78, %77
store i32 %79, i32* %8
br label %80
80: ; preds = %76
%81 = load i32, i32* %9
%82 = load i32, i32* %10
%83 = add nsw i32 %82, %81
store i32 %83, i32* %10
br label %61
84: ; preds = %61
%85 = call i32 @exit(i32 0)
unreachable
}

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"
@.str.3 = [19 x i8] c"inet_wstream:write\00"
define i32 @sendTcp(i8* nocapture readnone %0, i8* nocapture readnone %1, i8* nocapture %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 48)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* @AI_NUMERICSERV
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 6
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
%16 = bitcast i32** %15 to i8*
%17 = call i32 @getaddrinfo(i32* null, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %5)
%18 = icmp eq i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = call i8* @gai_strerror(i32 %17)
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %3
%25 = load %struct.addrinfo*, %struct.addrinfo** %5
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%27 = bitcast i32** %26 to %struct.sockaddr**
%28 = load %struct.sockaddr*, %struct.sockaddr** %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %30, i32 %32, i32 0)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %24
%39 = call i64 @connect(i32 %33, %struct.sockaddr* %28, i32 4)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %38, %54
%45 = phi i32 [ %55, %54 ], [ 8, %38 ]
%46 = phi i32 [ %56, %54 ], [ 0, %38 ]
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %2, i64 %47
%49 = call i32 @write(i32 %33, i8* %48, i32 %45)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %44
%55 = sub nsw i32 %45, %49
%56 = add nsw i32 %49, %46
%57 = icmp sgt i32 %55, 0
br i1 %57, label %44, label %58
58: ; preds = %54
%59 = call i32 @exit(i32 0)
unreachable
}