#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_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ a;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo const *,
                struct addrinfo **);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ v4_to_v6(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int main(void) {
  struct addrinfo *result;
  struct addrinfo *res;
  const struct addrinfo hints = {.ai_family = AF_INET};
  int error, sock;
  error = getaddrinfo("www.example.com", NULL, &hints, &result);
  if (error != 0) {
    fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
    return EXIT_FAILURE;
  }
  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    perror("socket");
    return EXIT_FAILURE;
  }
  struct sockaddr_in6 a = {.sin6_family = AF_INET6, .sin6_port = htons(80)};
  v4_to_v6(&((struct sockaddr_in *)result->ai_addr)->sin_addr, &a.sin6_addr);
  freeaddrinfo(result);
  if ((error = connect(sock, (struct sockaddr *)&a, sizeof(a))) == -1) {
    perror("connect");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
lea    0xe46(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x28(%rbp),%rdx
lea    -0x10(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     120f <main+0x7f>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xe17(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d7 <main+0x147>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x30(%rbp)
cmp    $0xffffffff,%eax
jne    1258 <main+0xc8>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d7 <main+0x147>
movl   $0x0,-0x40(%rbp)
mov    $0x50,%edi
call   1030 <htons@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    -0x40(%rbp),%rsi
call   1060 <v4_to_v6@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x30(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    12cb <main+0x13b>
lea    0xd7c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2cf7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d7 <main+0x147>
mov    0x2cde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movq   $0x0,0x18(%rsp)
mov    0x2e41(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x20(%rsp)
lea    0xe4c(%rip),%rdi
lea    0x18(%rsp),%rdx
mov    %rsp,%rcx
xor    %esi,%esi
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     11f1 <main+0x61>
mov    0x2dfa(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe32(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    0x2dc9(%rip),%rax
jmp    126c <main+0xdc>
mov    (%rbx),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1276 <main+0xe6>
mov    %eax,%ebx
movl   $0x0,0x8(%rsp)
mov    $0x50,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%rsp),%rax
mov    (%rax),%rdi
lea    0x8(%rsp),%r14
mov    %r14,%rsi
call   1060 <v4_to_v6@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     127f <main+0xef>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdad(%rip),%rdi
jmp    1286 <main+0xf6>
lea    0xdab(%rip),%rdi
call   1070 <perror@plt>
jmp    11e8 <main+0x58>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32 }
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [16 x i8] c"www.example.com\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"error in getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@AF_INET6 = global i32 0
@.str.3 = [8 x i8] c"connect\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in6
store i32 0, i32* %1
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 0
store i64 0, i64* %8
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 1
%10 = load i32, i32* @AF_INET
store i32 %10, i32* %9
%11 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* null, %struct.addrinfo* %4, %struct.addrinfo** %2)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = load i32, i32* %5
%17 = call i8* @gai_strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %1
br label %53
20: ; preds = %0
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = load i32, i32* @IPPROTO_TCP
%24 = call i32 @socket(i32 %21, i32 %22, i32 %23)
store i32 %24, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @EXIT_FAILURE
store i32 %28, i32* %1
br label %53
29: ; preds = %20
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i32 0, i32 0
store i32 0, i32* %30
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i32 0, i32 1
%32 = call i32 @htons(i32 80)
store i32 %32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i32 0, i32 2
%34 = load i32, i32* @AF_INET6
store i32 %34, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %2
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 0
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to %struct.sockaddr_in*
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 0
%40 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %7, i32 0, i32 0
%41 = call i32 @v4_to_v6(i32* %39, i32* %40)
%42 = load %struct.addrinfo*, %struct.addrinfo** %2
%43 = call i32 @freeaddrinfo(%struct.addrinfo* %42)
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in6* %7 to %struct.sockaddr*
%46 = call i32 @connect(i32 %44, %struct.sockaddr* %45, i32 12)
store i32 %46, i32* %5
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %29
%49 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
store i32 %50, i32* %1
br label %53
51: ; preds = %29
%52 = load i32, i32* @EXIT_SUCCESS
store i32 %52, i32* %1
br label %53
53: ; preds = %51, %48, %26, %14
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32 }
%struct.sockaddr_in6 = type { i32, i32, i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [16 x i8] c"www.example.com\00"
@stderr = global i32 0
@.str.1 = [26 x i8] c"error in getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@AF_INET6 = global i32 0
@.str.3 = [8 x i8] c"connect\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca %struct.addrinfo*
%2 = alloca %struct.addrinfo
%3 = alloca %struct.sockaddr_in6
%4 = bitcast %struct.addrinfo** %1 to i8*
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 0
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
%8 = load i32, i32* @AF_INET
store i32 %8, i32* %7
%9 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32* null, %struct.addrinfo* %2, %struct.addrinfo** %1)
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i8* @gai_strerror(i32 %9)
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
br label %48
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = load i32, i32* @IPPROTO_TCP
%20 = call i32 @socket(i32 %17, i32 %18, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
br label %48
25: ; preds = %16
%26 = bitcast %struct.sockaddr_in6* %3 to i8*
%27 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 0
store i32 0, i32* %27
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 1
%29 = call i32 @htons(i32 80)
store i32 %29, i32* %28
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %3, i64 0, i32 2
%31 = load i32, i32* @AF_INET6
store i32 %31, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %1
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 0
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to %struct.sockaddr_in*
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %35, i64 0, i32 0
%37 = call i32 @v4_to_v6(i32* %36, i32* %27)
%38 = load %struct.addrinfo*, %struct.addrinfo** %1
%39 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
%40 = bitcast %struct.sockaddr_in6* %3 to %struct.sockaddr*
%41 = call i32 @connect(i32 %20, %struct.sockaddr* %40, i32 12)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %25
%44 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %25, %43
%46 = phi i32* [ @EXIT_FAILURE, %43 ], [ @EXIT_SUCCESS, %25 ]
%47 = load i32, i32* %46
br label %48
48: ; preds = %45, %22, %11
%49 = phi i32 [ %15, %11 ], [ %24, %22 ], [ %47, %45 ]
ret i32 %49
}