#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s6_addr;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  TYPE_1__ sin6_addr;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servAddr;
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ clientStream(int, char *);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getSockName(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printSocketAddress(struct sockaddr_in6);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ sysError(char *);
int /*<<< orphan*/ usrError(char *);
int main(int argc, char **argv) {
  if (argc != 4)
    usrError("Usage:\t Client <address> <port> <string>");
  char *address = argv[1];
  in_port_t port = atoi(argv[2]);
  char *string = argv[3];
  int clntSock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
  if (clntSock < 0)
    sysError("failed to create socket");
  struct sockaddr_in6 servAddr;
  memset(&servAddr, 0, sizeof(servAddr));
  servAddr.sin6_family = AF_INET6;
  int ret = inet_pton(AF_INET6, address, &servAddr.sin6_addr.s6_addr);
  if (ret < 0)
    sysError("unknown address family");
  else if (ret == 0)
    usrError("invalid address type");
  servAddr.sin6_port = htons(port);
  if (connect(clntSock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
    sysError("connection failed");
  getSockName(clntSock);
  fprintf(stdout, "\nConnecting to ");
  printSocketAddress(servAddr);
  clientStream(clntSock, string);
  close(clntSock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11fc <main+0x2c>
lea    0xe09(%rip),%rdi
call   1040 <usrError@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    125d <main+0x8d>
lea    0xdd1(%rip),%rdi
call   1030 <sysError@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
add    $0x4,%rdx
call   10a0 <inet_pton@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12b1 <main+0xe1>
lea    0xd9a(%rip),%rdi
call   1030 <sysError@plt>
jmp    12cc <main+0xfc>
cmpl   $0x0,-0x3c(%rbp)
jne    12c7 <main+0xf7>
lea    0xd96(%rip),%rdi
call   1040 <usrError@plt>
jmp    12cc <main+0xfc>
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    12fe <main+0x12e>
lea    0xd74(%rip),%rdi
call   1030 <sysError@plt>
mov    -0x2c(%rbp),%edi
call   10b0 <getSockName@plt>
mov    0x2cbb(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x48(%rbp),%esi
call   10e0 <printSocketAddress@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1080 <clientStream@plt>
mov    -0x2c(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbp
cmp    $0x4,%edi
je     11f0 <main+0x20>
lea    0xe15(%rip),%rdi
call   1040 <usrError@plt>
mov    0x8(%rbp),%rbx
mov    0x10(%rbp),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r15d
mov    0x18(%rbp),%r14
mov    0x2dd5(%rip),%r12
mov    (%r12),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1238 <main+0x68>
lea    0xdf6(%rip),%rdi
call   1030 <sysError@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r12),%edi
mov    %edi,0x8(%rsp)
lea    0x4(%rsp),%rdx
mov    %rbx,%rsi
call   10a0 <inet_pton@plt>
test   %eax,%eax
js     1270 <main+0xa0>
jne    127c <main+0xac>
lea    0xdef(%rip),%rdi
call   1040 <usrError@plt>
jmp    127c <main+0xac>
lea    0xdca(%rip),%rdi
call   1030 <sysError@plt>
mov    %r15d,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jns    12a7 <main+0xd7>
lea    0xdcb(%rip),%rdi
call   1030 <sysError@plt>
mov    %ebp,%edi
call   10b0 <getSockName@plt>
mov    0x2d13(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
call   1090 <fprintf@plt>
mov    (%rsp),%rdi
mov    0x8(%rsp),%esi
call   10e0 <printSocketAddress@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1080 <clientStream@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [41 x i8] c"Usage:\09 Client <address> <port> <string>\00"
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"failed to create socket\00"
@.str.2 = [23 x i8] c"unknown address family\00"
@.str.3 = [21 x i8] c"invalid address type\00"
@.str.4 = [18 x i8] c"connection failed\00"
@stdout = global i32 0
@.str.5 = [16 x i8] c"\0AConnecting to \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in6
%11 = alloca i32
%12 = alloca { i64, i32 }
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 4
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @usrError(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %6
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %7
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 3
%27 = load i8*, i8** %26
store i8* %27, i8** %8
%28 = load i32, i32* @AF_INET6
%29 = load i32, i32* @SOCK_STREAM
%30 = load i32, i32* @IPPROTO_TCP
%31 = call i32 @socket(i32 %28, i32 %29, i32 %30)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %17
%35 = call i32 @sysError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %17
%37 = call i32 @memset(%struct.sockaddr_in6* %10, i32 0, i32 12)
%38 = load i32, i32* @AF_INET6
%39 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 2
store i32 %38, i32* %39
%40 = load i32, i32* @AF_INET6
%41 = load i8*, i8** %6
%42 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 1
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
%44 = call i32 @inet_pton(i32 %40, i8* %41, i32* %43)
store i32 %44, i32* %11
%45 = load i32, i32* %11
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %36
%48 = call i32 @sysError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %55
49: ; preds = %36
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @usrError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %54
54: ; preds = %52, %49
br label %55
55: ; preds = %54, %47
%56 = load i32, i32* %7
%57 = call i32 @htons(i32 %56)
%58 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 0
store i32 %57, i32* %58
%59 = load i32, i32* %9
%60 = bitcast %struct.sockaddr_in6* %10 to %struct.sockaddr*
%61 = call i64 @connect(i32 %59, %struct.sockaddr* %60, i32 12)
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %55
%64 = call i32 @sysError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %55
%66 = load i32, i32* %9
%67 = call i32 @getSockName(i32 %66)
%68 = load i32, i32* @stdout
%69 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%70 = bitcast { i64, i32 }* %12 to i8*
%71 = bitcast %struct.sockaddr_in6* %10 to i8*
%72 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %12, i32 0, i32 0
%73 = load i64, i64* %72
%74 = getelementptr inbounds { i64, i32 }, { i64, i32 }* %12, i32 0, i32 1
%75 = load i32, i32* %74
%76 = call i32 @printSocketAddress(i64 %73, i32 %75)
%77 = load i32, i32* %9
%78 = load i8*, i8** %8
%79 = call i32 @clientStream(i32 %77, i8* %78)
%80 = load i32, i32* %9
%81 = call i32 @close(i32 %80)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [41 x i8] c"Usage:\09 Client <address> <port> <string>\00"
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [24 x i8] c"failed to create socket\00"
@.str.2 = [23 x i8] c"unknown address family\00"
@.str.3 = [21 x i8] c"invalid address type\00"
@.str.4 = [18 x i8] c"connection failed\00"
@stdout = global i32 0
@.str.5 = [16 x i8] c"\0AConnecting to \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in6
%4 = icmp eq i32 %0, 4
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @usrError(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 3
%14 = load i8*, i8** %13
%15 = load i32, i32* @AF_INET6
%16 = load i32, i32* @SOCK_STREAM
%17 = load i32, i32* @IPPROTO_TCP
%18 = tail call i32 @socket(i32 %15, i32 %16, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %7
%21 = tail call i32 @sysError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %7
%23 = bitcast %struct.sockaddr_in6* %3 to i8*
%24 = call i32 @memset(%struct.sockaddr_in6* %3, i32 0, i32 12)
%25 = load i32, i32* @AF_INET6
%26 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1, i32 0
%28 = call i32 @inet_pton(i32 %25, i8* %9, i32* %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @sysError(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %36
32: ; preds = %22
%33 = icmp eq i32 %28, 0
br i1 %33, label %34, label %36
34: ; preds = %32
%35 = call i32 @usrError(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %32, %34, %30
%37 = call i32 @htons(i32 %12)
%38 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%40 = call i64 @connect(i32 %18, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = call i32 @sysError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %36
%45 = call i32 @getSockName(i32 %18)
%46 = load i32, i32* @stdout
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%48 = bitcast %struct.sockaddr_in6* %3 to i64*
%49 = load i64, i64* %48
%50 = load i32, i32* %26
%51 = call i32 @printSocketAddress(i64 %49, i32 %50)
%52 = call i32 @clientStream(i32 %18, i8* %14)
%53 = call i32 @close(i32 %18)
ret i32 0
}