#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;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, 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);
int 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));
    return 1;
  }
  fprintf(
      stderr,
      "in writetcp, serverHostName is %s, serverPortNum is %s and msg is %s\n",
      serverHostName, serverPortNum, msg);
  server = (struct sockaddr_in *)addrlist->ai_addr;
  if ((sock = socket(addrlist->ai_family, addrlist->ai_socktype, 0)) < 0) {
    perror("inet_wstream:socket");
    return 1;
  }
  if (connect(sock, (struct sockaddr *)server, sizeof(struct sockaddr_in)) <
      0) {
    perror("inet_wstream:connect");
    return 1;
  }
  fprintf(stderr, "In writeTcp, msg is: %s and length is: %lu\n", msg,
          (sizeof(msg) / sizeof(msg[0])));
  left = 34;
  put = 0;
  while (left > 0) {
    if ((num = write(sock, msg + put, left)) < 0) {
      perror("inet_wstream:write");
      return 1;
    } else
      left -= num;
    put += num;
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<writeTcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x68(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x70(%rbp),%rdx
lea    -0x78(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1251 <writeTcp+0xc1>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x7c(%rbp),%edi
mov    %rax,%rdx
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <writeTcp+0x1f5>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x78(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x78(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x78(%rbp),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    12b8 <writeTcp+0x128>
lea    0xdb0(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <writeTcp+0x1f5>
mov    -0x24(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    12eb <writeTcp+0x15b>
lea    0xd91(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <writeTcp+0x1f5>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd81(%rip),%rsi
mov    $0x8,%ecx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x22,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    1376 <writeTcp+0x1e6>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x30(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    135d <writeTcp+0x1cd>
lea    0xd60(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1385 <writeTcp+0x1f5>
mov    -0x2c(%rbp),%ecx
mov    -0x28(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
jmp    1319 <writeTcp+0x189>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeTcp>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e1e(%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    %r14,%rsi
mov    %rbx,%rdx
call   10a0 <getaddrinfo@plt>
mov    0x2dc9(%rip),%r12
mov    (%r12),%ebx
test   %eax,%eax
je     1233 <writeTcp+0xa3>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xde3(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%eax
jmp    12c4 <writeTcp+0x134>
lea    0xdd7(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %r14,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rbx
mov    (%rax),%esi
mov    0x4(%rax),%edi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12d1 <writeTcp+0x141>
mov    %eax,%r14d
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     12da <writeTcp+0x14a>
mov    (%r12),%edi
lea    0xdf9(%rip),%rsi
xor    %ebp,%ebp
mov    $0x8,%ecx
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x22,%ebx
nopl   (%rax)
movslq %ebp,%rsi
add    %r15,%rsi
mov    %r14d,%edi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
js     12e3 <writeTcp+0x153>
add    %eax,%ebp
sub    %eax,%ebx
jg     12a0 <writeTcp+0x110>
mov    %r14d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7f(%rip),%rdi
jmp    12ea <writeTcp+0x15a>
lea    0xd8a(%rip),%rdi
jmp    12ea <writeTcp+0x15a>
lea    0xdc2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    12c4 <writeTcp+0x134>

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 = [70 x i8] c"in writetcp, serverHostName is %s, serverPortNum is %s and msg is %s\0A\00"
@.str.2 = [20 x i8] c"inet_wstream:socket\00"
@.str.3 = [21 x i8] c"inet_wstream:connect\00"
@.str.4 = [44 x i8] c"In writeTcp, msg is: %s and length is: %lu\0A\00"
@.str.5 = [19 x i8] c"inet_wstream:write\00"
define i32 @writeTcp(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.sockaddr_in*
%14 = alloca %struct.addrinfo
%15 = alloca %struct.addrinfo*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%16 = call i32 @memset(%struct.addrinfo* %14, i32 0, i32 48)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_STREAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @AI_NUMERICSERV
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 6
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 5
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 4
store i32* null, i32** %24
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 2
store i32* null, i32** %25
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 3
store i32* null, i32** %26
%27 = load i8*, i8** %5
%28 = load i8*, i8** %6
%29 = call i32 @getaddrinfo(i8* %27, i8* %28, %struct.addrinfo* %14, %struct.addrinfo** %15)
store i32 %29, i32* %12
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %3
%33 = load i32, i32* @stderr
%34 = load i32, i32* %12
%35 = call i8* @gai_strerror(i32 %34)
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %35)
store i32 1, i32* %4
br label %94
37: ; preds = %3
%38 = load i32, i32* @stderr
%39 = load i8*, i8** %5
%40 = load i8*, i8** %6
%41 = load i8*, i8** %7
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8* %40, i8* %41)
%43 = load %struct.addrinfo*, %struct.addrinfo** %15
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 2
%45 = load i32*, i32** %44
%46 = bitcast i32* %45 to %struct.sockaddr_in*
store %struct.sockaddr_in* %46, %struct.sockaddr_in** %13
%47 = load %struct.addrinfo*, %struct.addrinfo** %15
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %15
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 0
%52 = load i32, i32* %51
%53 = call i32 @socket(i32 %49, i32 %52, i32 0)
store i32 %53, i32* %8
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %37
%56 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %4
br label %94
57: ; preds = %37
%58 = load i32, i32* %8
%59 = load %struct.sockaddr_in*, %struct.sockaddr_in** %13
%60 = bitcast %struct.sockaddr_in* %59 to %struct.sockaddr*
%61 = call i64 @connect(i32 %58, %struct.sockaddr* %60, i32 4)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %57
%64 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %4
br label %94
65: ; preds = %57
%66 = load i32, i32* @stderr
%67 = load i8*, i8** %7
%68 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %67, i64 8)
store i32 34, i32* %9
store i32 0, i32* %11
br label %69
69: ; preds = %87, %65
%70 = load i32, i32* %9
%71 = icmp sgt i32 %70, 0
br i1 %71, label %72, label %91
72: ; preds = %69
%73 = load i32, i32* %8
%74 = load i8*, i8** %7
%75 = load i32, i32* %11
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = load i32, i32* %9
%79 = call i32 @write(i32 %73, i8* %77, i32 %78)
store i32 %79, i32* %10
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %72
%82 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %4
br label %94
83: ; preds = %72
%84 = load i32, i32* %10
%85 = load i32, i32* %9
%86 = sub nsw i32 %85, %84
store i32 %86, i32* %9
br label %87
87: ; preds = %83
%88 = load i32, i32* %10
%89 = load i32, i32* %11
%90 = add nsw i32 %89, %88
store i32 %90, i32* %11
br label %69
91: ; preds = %69
%92 = load i32, i32* %8
%93 = call i32 @close(i32 %92)
store i32 0, i32* %4
br label %94
94: ; preds = %91, %81, %63, %55, %32
%95 = load i32, i32* %4
ret i32 %95
}

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 = [70 x i8] c"in writetcp, serverHostName is %s, serverPortNum is %s and msg is %s\0A\00"
@.str.2 = [20 x i8] c"inet_wstream:socket\00"
@.str.3 = [21 x i8] c"inet_wstream:connect\00"
@.str.4 = [44 x i8] c"In writeTcp, msg is: %s and length is: %lu\0A\00"
@.str.5 = [19 x i8] c"inet_wstream:write\00"
define i32 @writeTcp(i8* %0, i8* %1, i8* %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
%19 = load i32, i32* @stderr
br i1 %18, label %23, label %20
20: ; preds = %3
%21 = call i8* @gai_strerror(i32 %17)
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %60
23: ; preds = %3
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1, i8* %2)
%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 %37
35: ; preds = %23
%36 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %60
37: ; preds = %23
%38 = call i64 @connect(i32 %33, %struct.sockaddr* %28, i32 4)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %60
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i8* %2, i64 8)
br label %45
45: ; preds = %42, %54
%46 = phi i32 [ 34, %42 ], [ %55, %54 ]
%47 = phi i32 [ 0, %42 ], [ %56, %54 ]
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %2, i64 %48
%50 = call i32 @write(i32 %33, i8* %49, i32 %46)
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %60
54: ; preds = %45
%55 = sub nsw i32 %46, %50
%56 = add nsw i32 %50, %47
%57 = icmp sgt i32 %55, 0
br i1 %57, label %45, label %58
58: ; preds = %54
%59 = call i32 @close(i32 %33)
br label %60
60: ; preds = %58, %52, %40, %35, %20
%61 = phi i32 [ 1, %20 ], [ 1, %35 ], [ 1, %40 ], [ 1, %52 ], [ 0, %58 ]
ret i32 %61
}