#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 sockaddr_in6 {
  int dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int NI_MAXHOST;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ getnameinfo(struct sockaddr *, int, char *, int,
                               int /*<<< orphan*/ *, int /*<<< orphan*/,
                               int /*<<< orphan*/);
char *malloc(int);
char *my_getnameinfo(struct sockaddr *addr) {
  char *address;
  address = malloc(NI_MAXHOST * sizeof(char));
  getnameinfo(addr,
              (addr->sa_family == AF_INET) ? sizeof(struct sockaddr_in)
                                           : sizeof(struct sockaddr_in6),
              address, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  return address;
}


x86 assembly (clang, -O0):
<my_getnameinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e9d(%rip),%rax
movslq (%rax),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
mov    0x2e91(%rip),%rax
mov    (%rax),%rdx
mov    $0x4,%eax
cmp    %rdx,%rcx
cmove  %rax,%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
mov    0x2e5d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1030 <getnameinfo@plt>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getnameinfo>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2ea6(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %r14,%rdi
mov    $0x4,%esi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <getnameinfo@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
@NI_MAXHOST = global i32 0
@AF_INET = global i64 0
@NI_NUMERICHOST = global i32 0
define i8* @my_getnameinfo(%struct.sockaddr* %0) {
%2 = alloca %struct.sockaddr*
%3 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %2
%4 = load i32, i32* @NI_MAXHOST
%5 = sext i32 %4 to i64
%6 = mul i64 %5, 1
%7 = trunc i64 %6 to i32
%8 = call i8* @malloc(i32 %7)
store i8* %8, i8** %3
%9 = load %struct.sockaddr*, %struct.sockaddr** %2
%10 = load %struct.sockaddr*, %struct.sockaddr** %2
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @AF_INET
%14 = icmp eq i64 %12, %13
%15 = zext i1 %14 to i64
%16 = select i1 %14, i64 4, i64 4
%17 = trunc i64 %16 to i32
%18 = load i8*, i8** %3
%19 = load i32, i32* @NI_MAXHOST
%20 = load i32, i32* @NI_NUMERICHOST
%21 = call i32 @getnameinfo(%struct.sockaddr* %9, i32 %17, i8* %18, i32 %19, i32* null, i32 0, i32 %20)
%22 = load i8*, i8** %3
ret i8* %22
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@NI_MAXHOST = global i32 0
@AF_INET = global i64 0
@NI_NUMERICHOST = global i32 0
define i8* @my_getnameinfo(%struct.sockaddr* %0) {
%2 = load i32, i32* @NI_MAXHOST
%3 = tail call i8* @malloc(i32 %2)
%4 = load i32, i32* @NI_NUMERICHOST
%5 = tail call i32 @getnameinfo(%struct.sockaddr* %0, i32 4, i8* %3, i32 %2, i32* null, i32 0, i32 %4)
ret i8* %3
}