#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 in6_addr {
  int dummy;
};
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  void *ai_family;
  void *ai_flags;
};
typedef int /*<<< orphan*/ Hints;

/* Variables and functions */
void *AF_INET;
void *AF_INET6;
void *AF_UNSPEC;
void *AI_NUMERICHOST;
void *AI_NUMERICSERV;
int /*<<< orphan*/ ReadAndEditStr(char *, int);
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ getaddrinfo(char *, char *, struct addrinfo *,
                               struct addrinfo **);
int inet_pton(void *, char *, struct in6_addr *);
char *malloc(int);
int /*<<< orphan*/ memset(struct addrinfo *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main(int argc, char **argv) {
  int Sd = -1, Rc;
  struct in6_addr ServerAddr;
  struct addrinfo Hints, *Res = NULL;
  char *MsgWrite = malloc(100 * sizeof(char));
  char *MsgRead = malloc(100 * sizeof(char));
  memset(&Hints, 0x00, sizeof(Hints));
  Hints.ai_flags = AI_NUMERICSERV;
  Hints.ai_family = AF_UNSPEC;
  Hints.ai_socktype = SOCK_STREAM;
  Rc = inet_pton(AF_INET, argv[1], &ServerAddr);
  if (Rc == 1) {
    Hints.ai_family = AF_INET;
    Hints.ai_flags = AI_NUMERICHOST;
  } else {
    Rc = inet_pton(AF_INET6, argv[1], &ServerAddr);
    if (Rc == 1) {
      Hints.ai_family = AF_INET6;
      Hints.ai_flags = AI_NUMERICHOST;
    }
  }
  getaddrinfo(argv[1], argv[2], &Hints, &Res);
  Sd = socket(Res->ai_family, Res->ai_socktype, Res->ai_protocol);
  connect(Sd, Res->ai_addr, Res->ai_addrlen);
  do {
    ReadAndEditStr(MsgWrite, 100);
    write(Sd, MsgWrite, strlen(MsgWrite) + 1);
    read(Sd, MsgRead, 100);
    if (atoi(MsgRead) >= 0)
      printf("%s\n", MsgRead);
  } while (atoi(MsgRead) >= 0);
  close(Sd);
  freeaddrinfo(Res);
  free(MsgWrite);
  free(MsgRead);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x48(%rbp)
mov    $0x64,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x50(%rbp)
mov    $0x64,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x58(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <inet_pton@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    12b2 <main+0xc2>
mov    0x2d48(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d2a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    12fb <main+0x10b>
mov    0x2d0f(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <inet_pton@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    12f6 <main+0x106>
mov    0x2ce7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2ce1(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    12fb <main+0x10b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10f0 <getaddrinfo@plt>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   10e0 <connect@plt>
mov    -0x50(%rbp),%rdi
mov    $0x64,%esi
call   1060 <ReadAndEditStr@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    $0x64,%edx
call   10a0 <read@plt>
mov    -0x58(%rbp),%rdi
call   10d0 <atoi@plt>
cmp    $0x0,%rax
jl     13b7 <main+0x1c7>
mov    -0x58(%rbp),%rsi
lea    0xc4e(%rip),%rdi
call   1070 <printf@plt>
jmp    13bc <main+0x1cc>
mov    -0x58(%rbp),%rdi
call   10d0 <atoi@plt>
cmp    $0x0,%rax
jge    134b <main+0x15b>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x48(%rbp),%rdi
call   1100 <freeaddrinfo@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r15
movq   $0x0,0x8(%rsp)
mov    $0x64,%edi
call   10c0 <malloc@plt>
mov    %rax,%r14
mov    $0x64,%edi
call   10c0 <malloc@plt>
mov    %rax,%rbx
lea    0x18(%rsp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1080 <memset@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x30(%rsp)
mov    0x2d91(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x28(%rsp)
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2d7d(%rip),%rbp
mov    0x0(%rbp),%rdi
mov    0x8(%r15),%rsi
lea    0x10(%rsp),%rdx
call   10b0 <inet_pton@plt>
cmp    $0x1,%eax
je     1298 <main+0xa8>
mov    0x2d47(%rip),%rbp
mov    0x0(%rbp),%rdi
mov    0x8(%r15),%rsi
lea    0x10(%rsp),%rdx
call   10b0 <inet_pton@plt>
cmp    $0x1,%eax
jne    12b0 <main+0xc0>
mov    0x0(%rbp),%rax
mov    %rax,0x28(%rsp)
mov    0x2d28(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x30(%rsp)
mov    0x8(%r15),%rdi
mov    0x10(%r15),%rsi
lea    0x18(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   10f0 <getaddrinfo@plt>
mov    0x8(%rsp),%rax
mov    0x10(%rax),%rdi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1110 <socket@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   10e0 <connect@plt>
lea    0xd0b(%rip),%r15
jmp    130d <main+0x11d>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10d0 <atoi@plt>
test   %rax,%rax
js     1359 <main+0x169>
mov    %r14,%rdi
mov    $0x64,%esi
call   1060 <ReadAndEditStr@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %r14,%rsi
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   10a0 <read@plt>
mov    %rbx,%rdi
call   10d0 <atoi@plt>
test   %rax,%rax
js     1300 <main+0x110>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <printf@plt>
jmp    1300 <main+0x110>
mov    %ebp,%edi
call   1090 <close@plt>
mov    0x8(%rsp),%rdi
call   1100 <freeaddrinfo@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in6_addr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i8*, i8* }
@AI_NUMERICSERV = global i8* null
@AF_UNSPEC = global i8* null
@SOCK_STREAM = global i32 0
@AF_INET = global i8* null
@AI_NUMERICHOST = global i8* null
@AF_INET6 = global i8* null
@.str = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.in6_addr
%9 = alloca %struct.addrinfo
%10 = alloca %struct.addrinfo*
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store %struct.addrinfo* null, %struct.addrinfo** %10
%13 = call i8* @malloc(i32 100)
store i8* %13, i8** %11
%14 = call i8* @malloc(i32 100)
store i8* %14, i8** %12
%15 = call i32 @memset(%struct.addrinfo* %9, i32 0, i32 32)
%16 = load i8*, i8** @AI_NUMERICSERV
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 5
store i8* %16, i8** %17
%18 = load i8*, i8** @AF_UNSPEC
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 4
store i8* %18, i8** %19
%20 = load i32, i32* @SOCK_STREAM
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 3
store i32 %20, i32* %21
%22 = load i8*, i8** @AF_INET
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 @inet_pton(i8* %22, i8* %25, %struct.in6_addr* %8)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %34
29: ; preds = %2
%30 = load i8*, i8** @AF_INET
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 4
store i8* %30, i8** %31
%32 = load i8*, i8** @AI_NUMERICHOST
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 5
store i8* %32, i8** %33
br label %48
34: ; preds = %2
%35 = load i8*, i8** @AF_INET6
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 @inet_pton(i8* %35, i8* %38, %struct.in6_addr* %8)
store i32 %39, i32* %7
%40 = load i32, i32* %7
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %47
42: ; preds = %34
%43 = load i8*, i8** @AF_INET6
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 4
store i8* %43, i8** %44
%45 = load i8*, i8** @AI_NUMERICHOST
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 5
store i8* %45, i8** %46
br label %47
47: ; preds = %42, %34
br label %48
48: ; preds = %47, %29
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = load i8**, i8*** %5
%53 = getelementptr inbounds i8*, i8** %52, i64 2
%54 = load i8*, i8** %53
%55 = call i32 @getaddrinfo(i8* %51, i8* %54, %struct.addrinfo* %9, %struct.addrinfo** %10)
%56 = load %struct.addrinfo*, %struct.addrinfo** %10
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 4
%58 = load i8*, i8** %57
%59 = load %struct.addrinfo*, %struct.addrinfo** %10
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i32 0, i32 3
%61 = load i32, i32* %60
%62 = load %struct.addrinfo*, %struct.addrinfo** %10
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 2
%64 = load i32, i32* %63
%65 = call i32 @socket(i8* %58, i32 %61, i32 %64)
store i32 %65, i32* %6
%66 = load i32, i32* %6
%67 = load %struct.addrinfo*, %struct.addrinfo** %10
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %67, i32 0, i32 1
%69 = load i32, i32* %68
%70 = load %struct.addrinfo*, %struct.addrinfo** %10
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i32 @connect(i32 %66, i32 %69, i32 %72)
br label %74
74: ; preds = %93, %48
%75 = load i8*, i8** %11
%76 = call i32 @ReadAndEditStr(i8* %75, i32 100)
%77 = load i32, i32* %6
%78 = load i8*, i8** %11
%79 = load i8*, i8** %11
%80 = call i64 @strlen(i8* %79)
%81 = add nsw i64 %80, 1
%82 = call i32 @write(i32 %77, i8* %78, i64 %81)
%83 = load i32, i32* %6
%84 = load i8*, i8** %12
%85 = call i32 @read(i32 %83, i8* %84, i32 100)
%86 = load i8*, i8** %12
%87 = call i64 @atoi(i8* %86)
%88 = icmp sge i64 %87, 0
br i1 %88, label %89, label %92
89: ; preds = %74
%90 = load i8*, i8** %12
%91 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %90)
br label %92
92: ; preds = %89, %74
br label %93
93: ; preds = %92
%94 = load i8*, i8** %12
%95 = call i64 @atoi(i8* %94)
%96 = icmp sge i64 %95, 0
br i1 %96, label %74, label %97
97: ; preds = %93
%98 = load i32, i32* %6
%99 = call i32 @close(i32 %98)
%100 = load %struct.addrinfo*, %struct.addrinfo** %10
%101 = call i32 @freeaddrinfo(%struct.addrinfo* %100)
%102 = load i8*, i8** %11
%103 = call i32 @free(i8* %102)
%104 = load i8*, i8** %12
%105 = call i32 @free(i8* %104)
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
%struct.in6_addr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i8*, i8* }
@AI_NUMERICSERV = global i8* null
@AF_UNSPEC = global i8* null
@SOCK_STREAM = global i32 0
@AF_INET = global i8* null
@AI_NUMERICHOST = global i8* null
@AF_INET6 = global i8* null
@.str = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.in6_addr
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.in6_addr* %3 to i8*
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %5
%9 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%10 = tail call dereferenceable_or_null(100) i8* @malloc(i32 100)
%11 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 32)
%12 = load i8*, i8** @AI_NUMERICSERV
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i8* %12, i8** %13
%14 = load i8*, i8** @AF_UNSPEC
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i8* %14, i8** %15
%16 = load i32, i32* @SOCK_STREAM
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %16, i32* %17
%18 = load i8*, i8** @AF_INET
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @inet_pton(i8* %18, i8* %20, %struct.in6_addr* %3)
%22 = icmp eq i32 %21, 1
br i1 %22, label %28, label %23
23: ; preds = %2
%24 = load i8*, i8** @AF_INET6
%25 = load i8*, i8** %19
%26 = call i32 @inet_pton(i8* %24, i8* %25, %struct.in6_addr* %3)
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %32
28: ; preds = %23, %2
%29 = phi i8** [ @AF_INET, %2 ], [ @AF_INET6, %23 ]
%30 = load i8*, i8** %29
store i8* %30, i8** %15
%31 = load i8*, i8** @AI_NUMERICHOST
store i8* %31, i8** %13
br label %32
32: ; preds = %28, %23
%33 = load i8*, i8** %19
%34 = getelementptr inbounds i8*, i8** %1, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @getaddrinfo(i8* %33, i8* %35, %struct.addrinfo* %4, %struct.addrinfo** %5)
%37 = load %struct.addrinfo*, %struct.addrinfo** %5
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 4
%39 = load i8*, i8** %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 3
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 2
%43 = load i32, i32* %42
%44 = call i32 @socket(i8* %39, i32 %41, i32 %43)
%45 = load %struct.addrinfo*, %struct.addrinfo** %5
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i64 0, i32 1
%47 = load i32, i32* %46
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i64 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 @connect(i32 %44, i32 %47, i32 %49)
br label %51
51: ; preds = %61, %32
%52 = call i32 @ReadAndEditStr(i8* %9, i32 100)
%53 = call i64 @strlen(i8* dereferenceable(1) %9)
%54 = add nsw i64 %53, 1
%55 = call i32 @write(i32 %44, i8* %9, i64 %54)
%56 = call i32 @read(i32 %44, i8* %10, i32 100)
%57 = call i64 @atoi(i8* %10)
%58 = icmp sgt i64 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %51
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %61
61: ; preds = %51, %59
%62 = call i64 @atoi(i8* %10)
%63 = icmp sgt i64 %62, -1
br i1 %63, label %51, label %64
64: ; preds = %61
%65 = call i32 @close(i32 %44)
%66 = load %struct.addrinfo*, %struct.addrinfo** %5
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
%68 = call i32 @free(i8* %9)
%69 = call i32 @free(i8* %10)
ret i32 0
}