#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 addrinfo {
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hint;

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int output_socket() {
  struct addrinfo *r, hint;
  int error;
  int sock;
  memset(&hint, 0, sizeof(hint));
  hint.ai_family = AF_INET6;
  hint.ai_socktype = SOCK_DGRAM;
  if ((error = getaddrinfo("localhost", NULL, &hint, &r)) != 0) {
    return -1;
  }
  sock = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
  freeaddrinfo(r);
  return sock;
}


x86 assembly (clang, -O0):
<output_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    0xe89(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x20(%rbp),%rdx
lea    -0x10(%rbp),%rcx
call   1040 <getaddrinfo@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11a0 <output_socket+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <output_socket+0x8b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1060 <socket@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<output_socket>:
push   %rbx
sub    $0x20,%rsp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1030 <memset@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
lea    0xe86(%rip),%rdi
lea    0x8(%rsp),%rcx
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    11b0 <output_socket+0x70>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%edi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1060 <socket@plt>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1050 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@AF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [10 x i8] c"localhost\00"
define i32 @output_socket() {
%1 = alloca i32
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo
%4 = alloca i32
%5 = alloca i32
%6 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 12)
%7 = load i32, i32* @AF_INET6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 2
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_DGRAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 1
store i32 %9, i32* %10
%11 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* null, %struct.addrinfo* %3, %struct.addrinfo** %2)
store i32 %11, i32* %4
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 -1, i32* %1
br label %28
14: ; preds = %0
%15 = load %struct.addrinfo*, %struct.addrinfo** %2
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i32 0, i32 2
%17 = load i32, i32* %16
%18 = load %struct.addrinfo*, %struct.addrinfo** %2
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = load %struct.addrinfo*, %struct.addrinfo** %2
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @socket(i32 %17, i32 %20, i32 %23)
store i32 %24, i32* %5
%25 = load %struct.addrinfo*, %struct.addrinfo** %2
%26 = call i32 @freeaddrinfo(%struct.addrinfo* %25)
%27 = load i32, i32* %5
store i32 %27, i32* %1
br label %28
28: ; preds = %14, %13
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@AF_INET6 = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [10 x i8] c"localhost\00"
define i32 @output_socket() {
%1 = alloca %struct.addrinfo*
%2 = alloca %struct.addrinfo
%3 = bitcast %struct.addrinfo** %1 to i8*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 12)
%6 = load i32, i32* @AF_INET6
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_DGRAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
store i32 %8, i32* %9
%10 = call i32 @getaddrinfo(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* null, %struct.addrinfo* %2, %struct.addrinfo** %1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %0
%13 = load %struct.addrinfo*, %struct.addrinfo** %1
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 2
%15 = load i32, i32* %14
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %13, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @socket(i32 %15, i32 %17, i32 %19)
%21 = load %struct.addrinfo*, %struct.addrinfo** %1
%22 = call i32 @freeaddrinfo(%struct.addrinfo* %21)
br label %23
23: ; preds = %0, %12
%24 = phi i32 [ %20, %12 ], [ -1, %0 ]
ret i32 %24
}