#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 size_t uint32_t;
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ NI_NUMERICHOST;
size_t *boxString(size_t, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *, int,
                int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ unboxString(size_t *, char *, int, size_t *);
uint32_t *socketGetHostByName(uint32_t context_len, uint32_t *hPtr) {
  char hostname[256];
  uint32_t hostname_len;
  struct addrinfo hints, *res;
  char buf[256] = "";
  int error;
  unboxString(hPtr, hostname, 256, &hostname_len);
  hostname[hostname_len] = (char)0;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  error = getaddrinfo(hostname, NULL, &hints, &res);
  if (error != 0) {
    fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(error));
    exit(1);
  }
  error = getnameinfo(res->ai_addr, res->ai_addrlen, buf, sizeof(buf), NULL, 0,
                      NI_NUMERICHOST);
  if (error != 0) {
    fprintf(stderr, "getnameinfo error: %s\n", gai_strerror(error));
    exit(1);
  }
  freeaddrinfo(res);
  return boxString(context_len, buf, strlen(buf));
}


x86 assembly (clang, -O0):
<socketGetHostByName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
lea    -0x118(%rbp),%rcx
call   1070 <unboxString@plt>
mov    -0x118(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x128(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x120(%rbp)
lea    -0x110(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x128(%rbp),%rdx
lea    -0x130(%rbp),%rcx
call   10b0 <getaddrinfo@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
je     127d <socketGetHostByName+0xdd>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x238(%rbp)
mov    -0x234(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x238(%rbp),%edi
mov    %rax,%rdx
lea    0xd92(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x130(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x130(%rbp),%rax
mov    (%rax),%esi
lea    -0x230(%rbp),%rdx
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
mov    $0x100,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1080 <getnameinfo@plt>
mov    %eax,-0x234(%rbp)
cmpl   $0x0,-0x234(%rbp)
je     12ff <socketGetHostByName+0x15f>
mov    0x2d0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x23c(%rbp)
mov    -0x234(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    -0x23c(%rbp),%edi
mov    %rax,%rdx
lea    0xd27(%rip),%rsi
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x130(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x250(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x248(%rbp)
lea    -0x230(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x250(%rbp),%rdi
mov    -0x248(%rbp),%rsi
mov    %eax,%edx
call   1040 <boxString@plt>
add    $0x260,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socketGetHostByName>:
push   %r15
push   %r14
push   %rbx
sub    $0x230,%rsp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
lea    0x130(%rsp),%r15
lea    0x18(%rsp),%rcx
mov    %rsi,%rdi
mov    %r15,%rsi
mov    $0x100,%edx
call   1070 <unboxString@plt>
mov    0x18(%rsp),%rax
movb   $0x0,0x130(%rsp,%rax,1)
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
lea    0x10(%rsp),%rcx
mov    %r15,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   10b0 <getaddrinfo@plt>
test   %eax,%eax
jne    12e4 <socketGetHostByName+0x144>
mov    0x10(%rsp),%rax
mov    (%rax),%esi
mov    0x4(%rax),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
lea    0x30(%rsp),%rdx
mov    $0x100,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1080 <getnameinfo@plt>
test   %eax,%eax
jne    12fd <socketGetHostByName+0x15d>
mov    0x10(%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <boxString@plt>
add    $0x230,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2ced(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xd05(%rip),%rsi
jmp    1314 <socketGetHostByName+0x174>
mov    0x2cd4(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xd03(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1090 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"getaddrinfo error: %s\0A\00"
@NI_NUMERICHOST = global i32 0
@.str.1 = [23 x i8] c"getnameinfo error: %s\0A\00"
define i64* @socketGetHostByName(i64 %0, i64* %1) {
%3 = alloca i64
%4 = alloca i64*
%5 = alloca [256 x i8]
%6 = alloca i64
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca [256 x i8]
%10 = alloca i32
store i64 %0, i64* %3
store i64* %1, i64** %4
%11 = bitcast [256 x i8]* %9 to i8*
%12 = load i64*, i64** %4
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%14 = call i32 @unboxString(i64* %12, i8* %13, i32 256, i64* %6)
%15 = load i64, i64* %6
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %15
store i8 0, i8* %16
%17 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 %18, i32* %19
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%21 = call i32 @getaddrinfo(i8* %20, i32* null, %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = load i32, i32* %10
%27 = call i8* @gai_strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %2
%31 = load %struct.addrinfo*, %struct.addrinfo** %8
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %8
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 0
%36 = load i32, i32* %35
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%38 = load i32, i32* @NI_NUMERICHOST
%39 = call i32 @getnameinfo(i32 %33, i32 %36, i8* %37, i32 256, i32* null, i32 0, i32 %38)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %30
%43 = load i32, i32* @stderr
%44 = load i32, i32* %10
%45 = call i8* @gai_strerror(i32 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %30
%49 = load %struct.addrinfo*, %struct.addrinfo** %8
%50 = call i32 @freeaddrinfo(%struct.addrinfo* %49)
%51 = load i64, i64* %3
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%54 = call i32 @strlen(i8* %53)
%55 = call i64* @boxString(i64 %51, i8* %52, i32 %54)
ret i64* %55
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"getaddrinfo error: %s\0A\00"
@NI_NUMERICHOST = global i32 0
@.str.1 = [23 x i8] c"getnameinfo error: %s\0A\00"
define i64* @socketGetHostByName(i64 %0, i64* %1) {
%3 = alloca [256 x i8]
%4 = alloca i64
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = alloca [256 x i8]
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = bitcast i64* %4 to i8*
%10 = bitcast %struct.addrinfo* %5 to i8*
%11 = bitcast %struct.addrinfo** %6 to i8*
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%13 = call i32 @unboxString(i64* %1, i8* %8, i32 256, i64* %4)
%14 = load i64, i64* %4
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @getaddrinfo(i8* %8, i32* null, %struct.addrinfo* %5, %struct.addrinfo** %6)
%20 = icmp eq i32 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = call i8* @gai_strerror(i32 %19)
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load %struct.addrinfo*, %struct.addrinfo** %6
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%31 = load i32, i32* %30
%32 = load i32, i32* @NI_NUMERICHOST
%33 = call i32 @getnameinfo(i32 %29, i32 %31, i8* %12, i32 256, i32* null, i32 0, i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %26
%36 = load i32, i32* @stderr
%37 = call i8* @gai_strerror(i32 %33)
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %26
%41 = load %struct.addrinfo*, %struct.addrinfo** %6
%42 = call i32 @freeaddrinfo(%struct.addrinfo* %41)
%43 = call i32 @strlen(i8* %12)
%44 = call i64* @boxString(i64 %0, i8* %12, i32 %43)
ret i64* %44
}