#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_in6 {
  int /*<<< orphan*/ sin6_addr;
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct sockaddr_in6 serv_addr;
  int sockfd;
  int str_len, ret;
  char msg[] = "hello, world";
  char buf[1024];
  if (argc != 3) {
    fprintf(stderr, "Usage: %s <ip6> <port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  sockfd = socket(AF_INET6, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket() failed");
    exit(EXIT_FAILURE);
  }
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin6_family = AF_INET6;
  serv_addr.sin6_port = htons(atoi(argv[2]));
  ret = inet_pton(AF_INET6, argv[1], &serv_addr.sin6_addr);
  if (ret <= 0) {
    if (ret == 0)
      fprintf(stderr, "Not in presentation format\n");
    else
      perror("inet_pton() failed");
    exit(EXIT_FAILURE);
  }
  if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    perror("connect() failed");
    exit(EXIT_FAILURE);
  }
  write(sockfd, msg, strlen(msg));
  str_len = read(sockfd, buf, sizeof(buf) - 1);
  buf[str_len] = '\0';
  printf("[recv %d bytes] %s\n", str_len, buf);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe00(%rip),%rax
mov    %rax,-0x39(%rbp)
mov    0xdfe(%rip),%eax
mov    %eax,-0x31(%rbp)
mov    0xdf9(%rip),%al
mov    %al,-0x2d(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     124c <main+0x6c>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    128c <main+0xac>
lea    0xdac(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <inet_pton@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jg     1326 <main+0x146>
cmpl   $0x0,-0x2c(%rbp)
jne    130c <main+0x12c>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd35(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
jmp    1318 <main+0x138>
lea    0xd3e(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
jne    135a <main+0x17a>
lea    0xd1d(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x44c(%rbp)
lea    -0x39(%rbp),%rax
mov    %rax,-0x448(%rbp)
lea    -0x39(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44c(%rbp),%edi
mov    -0x448(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
mov    %eax,-0x28(%rbp)
movslq -0x28(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
mov    -0x28(%rbp),%esi
lea    -0x440(%rbp),%rdx
lea    0xcb6(%rip),%rdi
call   1060 <printf@plt>
mov    -0x24(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%r14
movabs $0x646c726f77202c,%rax
mov    %rax,0x15(%rsp)
movabs $0x77202c6f6c6c6568,%rax
2c 20 77
mov    %rax,0x10(%rsp)
cmp    $0x3,%edi
jne    12f8 <main+0x118>
mov    0x2dc9(%rip),%r12
mov    (%r12),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
cmp    $0xffffffff,%eax
je     1314 <main+0x134>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x10(%r14),%rdi
call   10d0 <atoi@plt>
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r12),%edi
mov    0x8(%r14),%rsi
mov    %r15,%rdx
call   10b0 <inet_pton@plt>
test   %eax,%eax
jle    131d <main+0x13d>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10f0 <connect@plt>
cmp    $0xffffffff,%eax
je     1328 <main+0x148>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0x20(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
movslq %eax,%rsi
movb   $0x0,0x20(%rsp,%rsi,1)
lea    0xd9d(%rip),%rdi
mov    %r14,%rdx
call   1060 <printf@plt>
mov    %ebx,%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd02(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1334 <main+0x154>
lea    0xd0a(%rip),%rdi
jmp    132f <main+0x14f>
je     1342 <main+0x162>
lea    0xd2b(%rip),%rdi
jmp    132f <main+0x14f>
lea    0xd35(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
lea    0xce3(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1334 <main+0x154>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.msg = [13 x i8] c"hello, world\00"
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s <ip6> <port>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@.str.2 = [28 x i8] c"Not in presentation format\0A\00"
@.str.3 = [19 x i8] c"inet_pton() failed\00"
@.str.4 = [17 x i8] c"connect() failed\00"
@.str.5 = [20 x i8] c"[recv %d bytes] %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in6
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [13 x i8]
%11 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [13 x i8]* %10 to i8*
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %23
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %2
%24 = load i32, i32* @AF_INET6
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %23
%34 = call i32 @memset(%struct.sockaddr_in6* %6, i32 0, i32 12)
%35 = load i32, i32* @AF_INET6
%36 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
%41 = call i32 @htons(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 1
store i32 %41, i32* %42
%43 = load i32, i32* @AF_INET6
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i32 0, i32 0
%48 = call i32 @inet_pton(i32 %43, i8* %46, i32* %47)
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = icmp sle i32 %49, 0
br i1 %50, label %51, label %62
51: ; preds = %33
%52 = load i32, i32* %9
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %59
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %54
%60 = load i32, i32* @EXIT_FAILURE
%61 = call i32 @exit(i32 %60)
unreachable
62: ; preds = %33
%63 = load i32, i32* %7
%64 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
%65 = call i32 @connect(i32 %63, %struct.sockaddr* %64, i32 12)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %71
67: ; preds = %62
%68 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %62
%72 = load i32, i32* %7
%73 = getelementptr inbounds [13 x i8], [13 x i8]* %10, i64 0, i64 0
%74 = getelementptr inbounds [13 x i8], [13 x i8]* %10, i64 0, i64 0
%75 = call i32 @strlen(i8* %74)
%76 = call i32 @write(i32 %72, i8* %73, i32 %75)
%77 = load i32, i32* %7
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%79 = call i32 @read(i32 %77, i8* %78, i32 1023)
store i32 %79, i32* %8
%80 = load i32, i32* %8
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 %81
store i8 0, i8* %82
%83 = load i32, i32* %8
%84 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%85 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %83, i8* %84)
%86 = load i32, i32* %7
%87 = call i32 @close(i32 %86)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.msg = [13 x i8] c"hello, world\00"
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s <ip6> <port>\0A\00"
@EXIT_FAILURE = global i32 0
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@.str.2 = [28 x i8] c"Not in presentation format\0A\00"
@.str.3 = [19 x i8] c"inet_pton() failed\00"
@.str.4 = [17 x i8] c"connect() failed\00"
@.str.5 = [20 x i8] c"[recv %d bytes] %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in6
%4 = alloca [13 x i8]
%5 = alloca [1024 x i8]
%6 = bitcast %struct.sockaddr_in6* %3 to i8*
%7 = getelementptr inbounds [13 x i8], [13 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = icmp eq i32 %0, 3
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %2
%17 = load i32, i32* @AF_INET6
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* @EXIT_FAILURE
%24 = tail call i32 @exit(i32 %23)
unreachable
25: ; preds = %16
%26 = call i32 @memset(%struct.sockaddr_in6* %3, i32 0, i32 12)
%27 = load i32, i32* @AF_INET6
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1
store i32 %32, i32* %33
%34 = load i32, i32* @AF_INET6
%35 = getelementptr inbounds i8*, i8** %1, i64 1
%36 = load i8*, i8** %35
%37 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
%38 = call i32 @inet_pton(i32 %34, i8* %36, i32* %37)
%39 = icmp slt i32 %38, 1
br i1 %39, label %40, label %50
40: ; preds = %25
%41 = icmp eq i32 %38, 0
br i1 %41, label %42, label %45
42: ; preds = %40
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %47
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %45, %42
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %25
%51 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%52 = call i32 @connect(i32 %19, %struct.sockaddr* %51, i32 12)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %50
%59 = call i32 @strlen(i8* %7)
%60 = call i32 @write(i32 %19, i8* %7, i32 %59)
%61 = call i32 @read(i32 %19, i8* %8, i32 1023)
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %62
store i8 0, i8* %63
%64 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i32 %61, i8* %8)
%65 = call i32 @close(i32 %19)
ret i32 0
}