#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 ai_socktype;
  scalar_t__ ai_protocol;
  scalar_t__ ai_flags;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ free(char *);
scalar_t__ getaddrinfo(char const *, char *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
char *mp_malloc(int);
int /*<<< orphan*/ mp_snprintf(char *, int, char *, int);
int /*<<< orphan*/ unknown(char *, char const *);
struct addrinfo *mp_getaddrinfo(const char *hostname, int port, int family,
                                int type) {
  struct addrinfo hints;
  struct addrinfo *result;
  char *buffer;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = type;
  hints.ai_flags = 0;
  hints.ai_protocol = 0;
  buffer = mp_malloc(6);
  mp_snprintf(buffer, 6, "%d", port);
  if (getaddrinfo(hostname, buffer, &hints, &result) != 0) {
    free(buffer);
    unknown("Can't resolv %s", hostname);
  }
  free(buffer);
  return result;
}


x86 assembly (clang, -O0):
<mp_getaddrinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    $0x6,%edi
call   1070 <mp_malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0x6,%esi
lea    0xe38(%rip),%rdx
call   1060 <mp_snprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x48(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1205 <mp_getaddrinfo+0xa5>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rsi
lea    0xe03(%rip),%rdi
call   1050 <unknown@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mp_getaddrinfo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %ebx,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
mov    $0x6,%edi
call   1070 <mp_malloc@plt>
mov    %rax,%rbx
lea    0xe4e(%rip),%rdx
mov    %rax,%rdi
mov    $0x6,%esi
mov    %ebp,%ecx
call   1060 <mp_snprintf@plt>
mov    %rsp,%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1080 <getaddrinfo@plt>
test   %rax,%rax
je     11ee <mp_getaddrinfo+0x8e>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe1d(%rip),%rdi
mov    %r14,%rsi
call   1050 <unknown@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64, i64, i32 }
@AF_INET = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [16 x i8] c"Can't resolv %s\00"
define %struct.addrinfo* @mp_getaddrinfo(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.addrinfo
%10 = alloca %struct.addrinfo*
%11 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = call i32 @memset(%struct.addrinfo* %9, i32 0, i32 32)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* %8
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 0
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 2
store i64 0, i64* %17
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 1
store i64 0, i64* %18
%19 = call i8* @mp_malloc(i32 6)
store i8* %19, i8** %11
%20 = load i8*, i8** %11
%21 = load i32, i32* %6
%22 = call i32 @mp_snprintf(i8* %20, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i8*, i8** %5
%24 = load i8*, i8** %11
%25 = call i64 @getaddrinfo(i8* %23, i8* %24, %struct.addrinfo* %9, %struct.addrinfo** %10)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %4
%28 = load i8*, i8** %11
%29 = call i32 @free(i8* %28)
%30 = load i8*, i8** %5
%31 = call i32 @unknown(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %27, %4
%33 = load i8*, i8** %11
%34 = call i32 @free(i8* %33)
%35 = load %struct.addrinfo*, %struct.addrinfo** %10
ret %struct.addrinfo* %35
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64, i64, i32 }
@AF_INET = global i32 0
@.str = [3 x i8] c"%d\00"
@.str.1 = [16 x i8] c"Can't resolv %s\00"
define %struct.addrinfo* @mp_getaddrinfo(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.addrinfo
%6 = alloca %struct.addrinfo*
%7 = bitcast %struct.addrinfo* %5 to i8*
%8 = bitcast %struct.addrinfo** %6 to i8*
%9 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 32)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 3
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 0
store i32 %3, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i64 0, i32 1
%14 = bitcast i64* %13 to i8*
%15 = call i8* @mp_malloc(i32 6)
%16 = call i32 @mp_snprintf(i8* %15, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%17 = call i64 @getaddrinfo(i8* %0, i8* %15, %struct.addrinfo* %5, %struct.addrinfo** %6)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %4
%20 = call i32 @free(i8* %15)
%21 = call i32 @unknown(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %19, %4
%23 = call i32 @free(i8* %15)
%24 = load %struct.addrinfo*, %struct.addrinfo** %6
ret %struct.addrinfo* %24
}