#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 */
typedef int uint16_t;
struct addrinfo {
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ service;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int errno;
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
struct addrinfo *socket_hostname_to_address(const char *hostname,
                                            uint16_t port) {
  char service[32];
  struct addrinfo hints;
  struct addrinfo *resolved = NULL;
  int rc;
  snprintf(service, sizeof(service), "%u", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  rc = getaddrinfo(hostname, service, &hints, &resolved);
  if (rc != 0) {
    errno = 72000000 + rc;
    return NULL;
  }
  return resolved;
}


x86 assembly (clang, -O0):
<socket_hostname_to_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x58(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x20,%esi
lea    0xea6(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     11d4 <socket_hostname_to_address+0xa4>
mov    -0x5c(%rbp),%ecx
add    $0x44aa200,%ecx
mov    0x2e03(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11dc <socket_hostname_to_address+0xac>
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_hostname_to_address>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %esi,%ecx
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0xeb2(%rip),%rdx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x20,%esi
call   1030 <snprintf@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <getaddrinfo@plt>
test   %eax,%eax
je     11c4 <socket_hostname_to_address+0x94>
add    $0x44aa200,%eax
mov    0x2e0a(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
jmp    11c9 <socket_hostname_to_address+0x99>
mov    0x8(%rsp),%rax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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