#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 in_addr {
  int dummy;
};
struct hostent {
  char *h_name;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyaddr(struct in_addr *, int, int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
char *getHostName(char *ip) {
  struct in_addr addr;
  inet_aton(ip, &addr);
  struct hostent *ptr = gethostbyaddr(&addr, sizeof(addr), AF_INET);
  if (!ptr)
    return NULL;
  return ptr->h_name;
}


x86 assembly (clang, -O0):
<getHostName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <inet_aton@plt>
mov    0x2ea8(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rdi
mov    $0x4,%esi
call   1040 <gethostbyaddr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116c <getHostName+0x4c>
movq   $0x0,-0x8(%rbp)
jmp    1177 <getHostName+0x57>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getHostName>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1030 <inet_aton@plt>
mov    0x2eaf(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x4,%esi
call   1040 <gethostbyaddr@plt>
test   %rax,%rax
je     1152 <getHostName+0x32>
mov    (%rax),%rax
jmp    1154 <getHostName+0x34>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i8* }
@AF_INET = global i32 0
define i8* @getHostName(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.in_addr
%5 = alloca %struct.hostent*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @inet_aton(i8* %6, %struct.in_addr* %4)
%8 = load i32, i32* @AF_INET
%9 = call %struct.hostent* @gethostbyaddr(%struct.in_addr* %4, i32 4, i32 %8)
store %struct.hostent* %9, %struct.hostent** %5
%10 = load %struct.hostent*, %struct.hostent** %5
%11 = icmp ne %struct.hostent* %10, null
br i1 %11, label %13, label %12
12: ; preds = %1
store i8* null, i8** %2
br label %17
13: ; preds = %1
%14 = load %struct.hostent*, %struct.hostent** %5
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i32 0, i32 0
%16 = load i8*, i8** %15
store i8* %16, i8** %2
br label %17
17: ; preds = %13, %12
%18 = load i8*, i8** %2
ret i8* %18
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i8* }
@AF_INET = global i32 0
define i8* @getHostName(i8* %0) {
%2 = alloca %struct.in_addr
%3 = bitcast %struct.in_addr* %2 to i8*
%4 = call i32 @inet_aton(i8* %0, %struct.in_addr* %2)
%5 = load i32, i32* @AF_INET
%6 = call %struct.hostent* @gethostbyaddr(%struct.in_addr* %2, i32 4, i32 %5)
%7 = icmp eq %struct.hostent* %6, null
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 0
%10 = load i8*, i8** %9
br label %11
11: ; preds = %1, %8
%12 = phi i8* [ %10, %8 ], [ null, %1 ]
ret i8* %12
}