#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;
int /*<<< orphan*/ close(int);
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 write(int, char *, int);
void writeTcp(char serverHostName[16], char serverPortNum[6], 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(serverHostName, serverPortNum, &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 = 34;
  put = 0;
  while (left > 0) {
    if ((num = write(sock, msg + put, left)) < 0) {
      perror("inet_wstream:write");
      exit(1);
    } else
      left -= num;
    put += num;
  }
  close(sock);
}


x86 assembly (clang, -O0):
<writeTcp>:
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    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    0x2e12(%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)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x68(%rbp),%rdx
lea    -0x70(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     125d <writeTcp+0xbd>
mov    0x2d9c(%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    0xdb2(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <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   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    129f <writeTcp+0xff>
lea    0xd81(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    12d0 <writeTcp+0x130>
lea    0xd64(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x22,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1339 <writeTcp+0x199>
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    1320 <writeTcp+0x180>
lea    0xd29(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <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    12de <writeTcp+0x13e>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeTcp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
movups %xmm0,0x20(%rsp)
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
jne    12a3 <writeTcp+0x103>
mov    (%rsp),%rax
mov    0x8(%rax),%rbx
mov    (%rax),%esi
mov    0x4(%rax),%edi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     12ce <writeTcp+0x12e>
mov    %eax,%r15d
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     12d7 <writeTcp+0x137>
xor    %ebp,%ebp
mov    $0x22,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebp,%rsi
add    %r14,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     128d <writeTcp+0xed>
add    %eax,%ebp
sub    %eax,%ebx
jg     1260 <writeTcp+0xc0>
mov    %r15d,%edi
call   1060 <close@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda6(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d26(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd46(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd3c(%rip),%rdi
jmp    1294 <writeTcp+0xf4>
lea    0xd47(%rip),%rdi
jmp    1294 <writeTcp+0xf4>

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 void @writeTcp(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 = load i8*, i8** %4
%27 = load i8*, i8** %5
%28 = call i32 @getaddrinfo(i8* %26, i8* %27, %struct.addrinfo* %13, %struct.addrinfo** %14)
store i32 %28, i32* %11
%29 = load i32, i32* %11
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %37
31: ; preds = %3
%32 = load i32, i32* @stderr
%33 = load i32, i32* %11
%34 = call i8* @gai_strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %3
%38 = load %struct.addrinfo*, %struct.addrinfo** %14
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 2
%40 = load i32*, i32** %39
%41 = bitcast i32* %40 to %struct.sockaddr_in*
store %struct.sockaddr_in* %41, %struct.sockaddr_in** %12
%42 = load %struct.addrinfo*, %struct.addrinfo** %14
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = load %struct.addrinfo*, %struct.addrinfo** %14
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @socket(i32 %44, i32 %47, i32 0)
store i32 %48, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %37
%51 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %37
%54 = load i32, i32* %7
%55 = load %struct.sockaddr_in*, %struct.sockaddr_in** %12
%56 = bitcast %struct.sockaddr_in* %55 to %struct.sockaddr*
%57 = call i64 @connect(i32 %54, %struct.sockaddr* %56, i32 4)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %53
%60 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %53
store i32 34, i32* %8
store i32 0, i32* %10
br label %63
63: ; preds = %82, %62
%64 = load i32, i32* %8
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %86
66: ; preds = %63
%67 = load i32, i32* %7
%68 = load i8*, i8** %6
%69 = load i32, i32* %10
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds i8, i8* %68, i64 %70
%72 = load i32, i32* %8
%73 = call i32 @write(i32 %67, i8* %71, i32 %72)
store i32 %73, i32* %9
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %66
%76 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %66
%79 = load i32, i32* %9
%80 = load i32, i32* %8
%81 = sub nsw i32 %80, %79
store i32 %81, i32* %8
br label %82
82: ; preds = %78
%83 = load i32, i32* %9
%84 = load i32, i32* %10
%85 = add nsw i32 %84, %83
store i32 %85, i32* %10
br label %63
86: ; preds = %63
%87 = load i32, i32* %7
%88 = call i32 @close(i32 %87)
ret void
}

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 void @writeTcp(i8* %0, i8* %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(i8* %0, i8* %1, %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 ], [ 34, %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 @close(i32 %33)
ret void
}