#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_flags;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AI_ADDRCONFIG;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
char *alloca(size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
char *strchr(char const *, char);
size_t strlen(char const *);
struct addrinfo *lookup_host(const char *address) {
  const char *colon = strchr(address, ':');
  if (!colon) {
    fprintf(stderr, "missing : in host/port name\n");
    return NULL;
  }
  size_t name_len = colon - address;
  char *name = alloca(name_len + 1);
  memcpy(name, address, name_len);
  name[name_len] = 0;
  size_t port_len = strlen(colon + 1);
  char *port = alloca(port_len + 1);
  memcpy(port, colon + 1, port_len);
  port[port_len] = 0;
  struct addrinfo hints;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  hints.ai_flags = AI_ADDRCONFIG;
  struct addrinfo *res;
  int error = getaddrinfo(name, port, &hints, &res);
  if (error) {
    fprintf(stderr, "Resolver error: %s\n", gai_strerror(error));
    return NULL;
  }
  return res;
}


x86 assembly (clang, -O0):
<lookup_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <lookup_host+0x4d>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <lookup_host+0x16c>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <alloca@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1080 <alloca@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    -0x30(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     12e4 <lookup_host+0x164>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x54(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xd4d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ec <lookup_host+0x16c>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_host>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r15
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     127e <lookup_host+0xfe>
mov    %rax,%r12
mov    %rax,%rbx
sub    %r15,%rbx
lea    0x1(%rbx),%rdi
call   1080 <alloca@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1090 <memcpy@plt>
movb   $0x0,(%r14,%rbx,1)
add    $0x1,%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
call   1080 <alloca@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1090 <memcpy@plt>
movb   $0x0,(%r15,%rbx,1)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x18(%rsp),%rcx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     1299 <lookup_host+0x119>
mov    0x2d72(%rip),%rcx
mov    (%rcx),%ebp
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdaf(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    129e <lookup_host+0x11e>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd72(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    129e <lookup_host+0x11e>
mov    0x18(%rsp),%rbx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [29 x i8] c"missing : in host/port name\0A\00"
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AI_ADDRCONFIG = global i32 0
@.str.1 = [20 x i8] c"Resolver error: %s\0A\00"
define %struct.addrinfo* @lookup_host(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca %struct.addrinfo
%10 = alloca %struct.addrinfo*
%11 = alloca i32
store i8* %0, i8** %3
%12 = load i8*, i8** %3
%13 = call i8* @strchr(i8* %12, i8 signext 58)
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %19, label %16
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store %struct.addrinfo* null, %struct.addrinfo** %2
br label %70
19: ; preds = %1
%20 = load i8*, i8** %4
%21 = load i8*, i8** %3
%22 = ptrtoint i8* %20 to i64
%23 = ptrtoint i8* %21 to i64
%24 = sub i64 %22, %23
store i64 %24, i64* %5
%25 = load i64, i64* %5
%26 = add i64 %25, 1
%27 = call i8* @alloca(i64 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = load i8*, i8** %3
%30 = load i64, i64* %5
%31 = call i32 @memcpy(i8* %28, i8* %29, i64 %30)
%32 = load i8*, i8** %6
%33 = load i64, i64* %5
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8 0, i8* %34
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = call i64 @strlen(i8* %36)
store i64 %37, i64* %7
%38 = load i64, i64* %7
%39 = add i64 %38, 1
%40 = call i8* @alloca(i64 %39)
store i8* %40, i8** %8
%41 = load i8*, i8** %8
%42 = load i8*, i8** %4
%43 = getelementptr inbounds i8, i8* %42, i64 1
%44 = load i64, i64* %7
%45 = call i32 @memcpy(i8* %41, i8* %43, i64 %44)
%46 = load i8*, i8** %8
%47 = load i64, i64* %7
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8 0, i8* %48
%49 = call i32 @memset(%struct.addrinfo* %9, i32 0, i32 16)
%50 = load i32, i32* @PF_UNSPEC
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 3
store i32 %50, i32* %51
%52 = load i32, i32* @SOCK_STREAM
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 2
store i32 %52, i32* %53
%54 = load i32, i32* @IPPROTO_TCP
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 1
store i32 %54, i32* %55
%56 = load i32, i32* @AI_ADDRCONFIG
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 0
store i32 %56, i32* %57
%58 = load i8*, i8** %6
%59 = load i8*, i8** %8
%60 = call i32 @getaddrinfo(i8* %58, i8* %59, %struct.addrinfo* %9, %struct.addrinfo** %10)
store i32 %60, i32* %11
%61 = load i32, i32* %11
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %19
%64 = load i32, i32* @stderr
%65 = load i32, i32* %11
%66 = call i8* @gai_strerror(i32 %65)
%67 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %66)
store %struct.addrinfo* null, %struct.addrinfo** %2
br label %70
68: ; preds = %19
%69 = load %struct.addrinfo*, %struct.addrinfo** %10
store %struct.addrinfo* %69, %struct.addrinfo** %2
br label %70
70: ; preds = %68, %63, %16
%71 = load %struct.addrinfo*, %struct.addrinfo** %2
ret %struct.addrinfo* %71
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@stderr = global i32 0
@.str = [29 x i8] c"missing : in host/port name\0A\00"
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@AI_ADDRCONFIG = global i32 0
@.str.1 = [20 x i8] c"Resolver error: %s\0A\00"
define %struct.addrinfo* @lookup_host(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 58)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %44
9: ; preds = %1
%10 = ptrtoint i8* %4 to i64
%11 = ptrtoint i8* %0 to i64
%12 = sub i64 %10, %11
%13 = add i64 %12, 1
%14 = tail call i8* @alloca(i64 %13)
%15 = tail call i32 @memcpy(i8* %14, i8* %0, i64 %12)
%16 = getelementptr inbounds i8, i8* %14, i64 %12
store i8 0, i8* %16
%17 = getelementptr inbounds i8, i8* %4, i64 1
%18 = tail call i64 @strlen(i8* %17)
%19 = add i64 %18, 1
%20 = tail call i8* @alloca(i64 %19)
%21 = tail call i32 @memcpy(i8* %20, i8* %17, i64 %18)
%22 = getelementptr inbounds i8, i8* %20, i64 %18
store i8 0, i8* %22
%23 = bitcast %struct.addrinfo* %2 to i8*
%24 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 16)
%25 = load i32, i32* @PF_UNSPEC
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %25, i32* %26
%27 = load i32, i32* @SOCK_STREAM
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* @IPPROTO_TCP
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @AI_ADDRCONFIG
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 0
store i32 %31, i32* %32
%33 = bitcast %struct.addrinfo** %3 to i8*
%34 = call i32 @getaddrinfo(i8* %14, i8* %20, %struct.addrinfo* %2, %struct.addrinfo** %3)
%35 = icmp eq i32 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %9
%37 = load i32, i32* @stderr
%38 = call i8* @gai_strerror(i32 %34)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %38)
br label %42
40: ; preds = %9
%41 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %42
42: ; preds = %40, %36
%43 = phi %struct.addrinfo* [ null, %36 ], [ %41, %40 ]
br label %44
44: ; preds = %42, %6
%45 = phi %struct.addrinfo* [ %43, %42 ], [ null, %6 ]
ret %struct.addrinfo* %45
}