#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 hostent {
  char *h_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyaddr(unsigned char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, ...);
void get_addr_name(char *namebuf, unsigned char *addr) {
  struct hostent *h = gethostbyaddr(addr, 4, AF_INET);
  if (h == NULL) {
    sprintf(namebuf, "%u.%u.%u.%u", addr[0], addr[1], addr[2], addr[3]);
    return;
  }
  sprintf(namebuf, "%s", h->h_name);
}


x86 assembly (clang, -O0):
<get_addr_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ead(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1030 <gethostbyaddr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118e <get_addr_name+0x6e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movzbl (%rax),%edx
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%ecx
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%r8d
mov    -0x10(%rbp),%rax
movzbl 0x3(%rax),%r9d
lea    0xe7e(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
jmp    11a7 <get_addr_name+0x87>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_addr_name>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2ea7(%rip),%rax
mov    (%rax),%edx
mov    %rsi,%rdi
mov    $0x4,%esi
call   1040 <gethostbyaddr@plt>
test   %rax,%rax
je     1167 <get_addr_name+0x37>
mov    (%rax),%rsi
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <strcpy@plt>
movzbl (%rbx),%edx
movzbl 0x1(%rbx),%ecx
movzbl 0x2(%rbx),%r8d
movzbl 0x3(%rbx),%r9d
lea    0xe81(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <sprintf@plt>

LLVM IR (-O0):
%struct.hostent = type { i8* }
@AF_INET = global i32 0
@.str = [12 x i8] c"%u.%u.%u.%u\00"
@.str.1 = [3 x i8] c"%s\00"
define void @get_addr_name(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.hostent*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = load i32, i32* @AF_INET
%8 = call %struct.hostent* @gethostbyaddr(i8* %6, i32 4, i32 %7)
store %struct.hostent* %8, %struct.hostent** %5
%9 = load %struct.hostent*, %struct.hostent** %5
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %30
11: ; preds = %2
%12 = load i8*, i8** %3
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i8, i8* %18
%20 = zext i8 %19 to i32
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 2
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 3
%27 = load i8, i8* %26
%28 = zext i8 %27 to i32
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %12, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %20, i32 %24, i32 %28)
br label %36
30: ; preds = %2
%31 = load i8*, i8** %3
%32 = load %struct.hostent*, %struct.hostent** %5
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i32 (i8*, i8*, ...) @sprintf(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %30, %11
ret void
}

LLVM IR (-O2):
%struct.hostent = type { i8* }
@AF_INET = global i32 0
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define void @get_addr_name(i8* %0, i8* %1) {
%3 = load i32, i32* @AF_INET
%4 = tail call %struct.hostent* @gethostbyaddr(i8* %1, i32 4, i32 %3)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %6, label %19
6: ; preds = %2
%7 = load i8, i8* %1
%8 = zext i8 %7 to i32
%9 = getelementptr inbounds i8, i8* %1, i64 1
%10 = load i8, i8* %9
%11 = zext i8 %10 to i32
%12 = getelementptr inbounds i8, i8* %1, i64 2
%13 = load i8, i8* %12
%14 = zext i8 %13 to i32
%15 = getelementptr inbounds i8, i8* %1, i64 3
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %11, i32 %14, i32 %17)
br label %23
19: ; preds = %2
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = tail call i8* @strcpy(i8* dereferenceable(1) %0, i8* dereferenceable(1) %21)
br label %23
23: ; preds = %19, %6
ret void
}