#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ inet_aton(char *, struct in_addr *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strlcpy(char *, char *, int);
char *get_hostname(char *addr) {
  static char answer[1024];
  static char lastreq[1024] = {'\0'};
  struct hostent *he;
  struct in_addr naddr;
  static char *last_answerp = NULL;
  printf(" get hostname...");
  fflush(stdout);
  printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
         "               "
         "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
  if (!strcmp(addr, lastreq))
    return last_answerp;
  strlcpy(lastreq, addr, 1024);
  inet_aton(addr, &naddr);
  he = gethostbyaddr((char *)&naddr, 4, AF_INET);
  if (he == NULL) {
    last_answerp = NULL;
    return NULL;
  }
  strlcpy(answer, he->h_name, 1024);
  last_answerp = answer;
  return answer;
}


x86 assembly (clang, -O0):
<get_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe8d(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
lea    0xe84(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0x32c3(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11bb <get_hostname+0x5b>
mov    0x36ae(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    124c <get_hostname+0xec>
mov    -0x10(%rbp),%rsi
lea    0x329a(%rip),%rdi
mov    $0x400,%edx
call   1070 <strlcpy@plt>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <inet_aton@plt>
lea    -0x20(%rbp),%rdi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1080 <gethostbyaddr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    121b <get_hostname+0xbb>
movq   $0x0,0x3652(%rip)
movq   $0x0,-0x8(%rbp)
jmp    124c <get_hostname+0xec>
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
lea    0x2e37(%rip),%rdi
mov    $0x400,%edx
call   1070 <strlcpy@plt>
lea    0x2e26(%rip),%rax
mov    %rax,0x361f(%rip)
lea    0x2e18(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hostname>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xe92(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
lea    0xe89(%rip),%rdi
call   1030 <printf@plt>
lea    0x32cc(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11fd <get_hostname+0x9d>
lea    0x32b9(%rip),%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <strlcpy@plt>
mov    %rsp,%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <inet_aton@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x4,%esi
call   1080 <gethostbyaddr@plt>
test   %rax,%rax
je     1206 <get_hostname+0xa6>
mov    (%rax),%rsi
lea    0x2e79(%rip),%rbx
mov    %rbx,%rdi
mov    $0x400,%edx
call   1070 <strlcpy@plt>
mov    %rbx,0x3665(%rip)
jmp    1213 <get_hostname+0xb3>
mov    0x365c(%rip),%rbx
jmp    1213 <get_hostname+0xb3>
movq   $0x0,0x364f(%rip)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.hostent = type { i8* }
%struct.in_addr = type { i32 }
@get_hostname.answer = internal global [1024 x i8] zeroinitializer
@get_hostname.lastreq = internal global [1024 x i8] zeroinitializer
@get_hostname.last_answerp = internal global i8* null
@.str = [17 x i8] c" get hostname...\00"
@stdout = global i32 0
@.str.1 = [46 x i8] c"\08\08\08\08\08\08\08\08\08\08\08\08\08\08\08 \08\08\08\08\08\08\08\08\08\08\08\08\08\08\08\00"
@AF_INET = global i32 0
define i8* @get_hostname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.hostent*
%5 = alloca %struct.in_addr
store i8* %0, i8** %3
%6 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @stdout
%8 = call i32 @fflush(i32 %7)
%9 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%10 = load i8*, i8** %3
%11 = call i32 @strcmp(i8* %10, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.lastreq, i64 0, i64 0))
%12 = icmp ne i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %1
%14 = load i8*, i8** @get_hostname.last_answerp
store i8* %14, i8** %2
br label %31
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i32 @strlcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.lastreq, i64 0, i64 0), i8* %16, i32 1024)
%18 = load i8*, i8** %3
%19 = call i32 @inet_aton(i8* %18, %struct.in_addr* %5)
%20 = bitcast %struct.in_addr* %5 to i8*
%21 = load i32, i32* @AF_INET
%22 = call %struct.hostent* @gethostbyaddr(i8* %20, i32 4, i32 %21)
store %struct.hostent* %22, %struct.hostent** %4
%23 = load %struct.hostent*, %struct.hostent** %4
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %25, label %26
25: ; preds = %15
store i8* null, i8** @get_hostname.last_answerp
store i8* null, i8** %2
br label %31
26: ; preds = %15
%27 = load %struct.hostent*, %struct.hostent** %4
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @strlcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.answer, i64 0, i64 0), i8* %29, i32 1024)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.answer, i64 0, i64 0), i8** @get_hostname.last_answerp
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.answer, i64 0, i64 0), i8** %2
br label %31
31: ; preds = %26, %25, %13
%32 = load i8*, i8** %2
ret i8* %32
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i8* }
@get_hostname.answer = internal global [1024 x i8] zeroinitializer
@get_hostname.lastreq = internal global [1024 x i8] zeroinitializer
@get_hostname.last_answerp = internal unnamed_addr global i8* null
@.str = [17 x i8] c" get hostname...\00"
@stdout = global i32 0
@.str.1 = [46 x i8] c"\08\08\08\08\08\08\08\08\08\08\08\08\08\08\08 \08\08\08\08\08\08\08\08\08\08\08\08\08\08\08\00"
@AF_INET = global i32 0
define i8* @get_hostname(i8* %0) {
%2 = alloca %struct.in_addr
%3 = bitcast %struct.in_addr* %2 to i8*
%4 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @stdout
%6 = tail call i32 @fflush(i32 %5)
%7 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.lastreq, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i8*, i8** @get_hostname.last_answerp
br label %23
12: ; preds = %1
%13 = tail call i32 @strlcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.lastreq, i64 0, i64 0), i8* %0, i32 1024)
%14 = call i32 @inet_aton(i8* %0, %struct.in_addr* %2)
%15 = load i32, i32* @AF_INET
%16 = call %struct.hostent* @gethostbyaddr(i8* %3, i32 4, i32 %15)
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %18, label %19
18: ; preds = %12
store i8* null, i8** @get_hostname.last_answerp
br label %23
19: ; preds = %12
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @strlcpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.answer, i64 0, i64 0), i8* %21, i32 1024)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.answer, i64 0, i64 0), i8** @get_hostname.last_answerp
br label %23
23: ; preds = %19, %18, %10
%24 = phi i8* [ null, %18 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @get_hostname.answer, i64 0, i64 0), %19 ], [ %11, %10 ]
ret i8* %24
}