#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 {
  unsigned int s_addr;
};
struct hostent {
  int /*<<< orphan*/ h_name;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ ENOMEM;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int gethostname(char *, size_t);
int /*<<< orphan*/ memchr(char *, char, size_t);
char *realloc(char *, size_t);
char *strdup(int /*<<< orphan*/);
char *localhost(void) {
  char *buf = NULL;
  size_t buf_len = 0;
  int status = 0;
  do {
    char *tmp;
    errno = 0;
    buf_len += 256;
    tmp = realloc(buf, buf_len);
    if (tmp == NULL) {
      errno = ENOMEM;
      free(buf);
      return 0;
    } else
      buf = tmp;
  } while (((status = gethostname(buf, buf_len)) == 0 &&
            !memchr(buf, '\0', buf_len)));
  if (status != 0 && errno != 0) {
    free(buf);
    buf = 0;
  } else {
    struct hostent *hp = gethostbyname(buf);
    if (hp) {
      struct in_addr addr;
      addr.s_addr = *(unsigned int *)hp->h_addr;
      hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
      if (hp) {
        free(buf);
        buf = strdup(hp->h_name);
      }
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<localhost>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e3a(%rip),%rax
movq   $0x0,(%rax)
mov    -0x18(%rbp),%rax
add    $0x100,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f1 <localhost+0x81>
mov    0x2e0a(%rip),%rax
mov    (%rax),%rcx
mov    0x2df8(%rip),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e9 <localhost+0x179>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    11fe <localhost+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <gethostname@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x39(%rbp)
jne    1238 <localhost+0xc8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <memchr@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
test   $0x1,%al
jne    118f <localhost+0x1f>
cmpl   $0x0,-0x1c(%rbp)
je     1274 <localhost+0x104>
mov    0x2d7c(%rip),%rax
cmpq   $0x0,(%rax)
je     1274 <localhost+0x104>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    12e1 <localhost+0x171>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12dc <localhost+0x16c>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
lea    -0x38(%rbp),%rdi
mov    0x2d44(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1080 <gethostbyaddr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12d7 <localhost+0x167>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%edi
call   1090 <strdup@plt>
mov    %rax,-0x10(%rbp)
jmp    12dc <localhost+0x16c>
jmp    12e1 <localhost+0x171>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<localhost>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x100,%ebx
xor    %r15d,%r15d
mov    0x2e49(%rip),%r12
nopw   0x0(%rax,%rax,1)
movq   $0x0,(%r12)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     11d7 <localhost+0x67>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <gethostname@plt>
test   %eax,%eax
jne    11ea <localhost+0x7a>
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memchr@plt>
add    $0x100,%rbx
mov    %r14,%r15
test   %eax,%eax
je     1190 <localhost+0x20>
jmp    11fe <localhost+0x8e>
mov    0x2dfa(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
mov    %r15,%rdi
jmp    11f4 <localhost+0x84>
cmpq   $0x0,(%r12)
je     11fe <localhost+0x8e>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
jmp    1244 <localhost+0xd4>
mov    %r14,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1244 <localhost+0xd4>
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x4,%esi
call   1080 <gethostbyaddr@plt>
test   %rax,%rax
je     1244 <localhost+0xd4>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    (%rbx),%edi
call   1090 <strdup@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i64 }
%struct.in_addr = type { i32 }
@errno = global i64 0
@ENOMEM = global i64 0
@AF_INET = global i32 0
define i8* @localhost() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.hostent*
%7 = alloca %struct.in_addr
store i8* null, i8** %2
store i64 0, i64* %3
store i32 0, i32* %4
br label %8
8: ; preds = %34, %0
store i64 0, i64* @errno
%9 = load i64, i64* %3
%10 = add i64 %9, 256
store i64 %10, i64* %3
%11 = load i8*, i8** %2
%12 = load i64, i64* %3
%13 = call i8* @realloc(i8* %11, i64 %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %8
%17 = load i64, i64* @ENOMEM
store i64 %17, i64* @errno
%18 = load i8*, i8** %2
%19 = call i32 @free(i8* %18)
store i8* null, i8** %1
br label %73
20: ; preds = %8
%21 = load i8*, i8** %5
store i8* %21, i8** %2
br label %22
22: ; preds = %20
br label %23
23: ; preds = %22
%24 = load i8*, i8** %2
%25 = load i64, i64* %3
%26 = call i32 @gethostname(i8* %24, i64 %25)
store i32 %26, i32* %4
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i8*, i8** %2
%30 = load i64, i64* %3
%31 = call i32 @memchr(i8* %29, i8 signext 0, i64 %30)
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br label %34
34: ; preds = %28, %23
%35 = phi i1 [ false, %23 ], [ %33, %28 ]
br i1 %35, label %8, label %36
36: ; preds = %34
%37 = load i32, i32* %4
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %36
%40 = load i64, i64* @errno
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i8*, i8** %2
%44 = call i32 @free(i8* %43)
store i8* null, i8** %2
br label %71
45: ; preds = %39, %36
%46 = load i8*, i8** %2
%47 = call %struct.hostent* @gethostbyname(i8* %46)
store %struct.hostent* %47, %struct.hostent** %6
%48 = load %struct.hostent*, %struct.hostent** %6
%49 = icmp ne %struct.hostent* %48, null
br i1 %49, label %50, label %70
50: ; preds = %45
%51 = load %struct.hostent*, %struct.hostent** %6
%52 = getelementptr inbounds %struct.hostent, %struct.hostent* %51, i32 0, i32 1
%53 = load i64, i64* %52
%54 = inttoptr i64 %53 to i32*
%55 = load i32, i32* %54
%56 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %7, i32 0, i32 0
store i32 %55, i32* %56
%57 = bitcast %struct.in_addr* %7 to i8*
%58 = load i32, i32* @AF_INET
%59 = call %struct.hostent* @gethostbyaddr(i8* %57, i32 4, i32 %58)
store %struct.hostent* %59, %struct.hostent** %6
%60 = load %struct.hostent*, %struct.hostent** %6
%61 = icmp ne %struct.hostent* %60, null
br i1 %61, label %62, label %69
62: ; preds = %50
%63 = load i8*, i8** %2
%64 = call i32 @free(i8* %63)
%65 = load %struct.hostent*, %struct.hostent** %6
%66 = getelementptr inbounds %struct.hostent, %struct.hostent* %65, i32 0, i32 0
%67 = load i32, i32* %66
%68 = call i8* @strdup(i32 %67)
store i8* %68, i8** %2
br label %69
69: ; preds = %62, %50
br label %70
70: ; preds = %69, %45
br label %71
71: ; preds = %70, %42
%72 = load i8*, i8** %2
store i8* %72, i8** %1
br label %73
73: ; preds = %71, %16
%74 = load i8*, i8** %1
ret i8* %74
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32, i64 }
@errno = global i64 0
@ENOMEM = global i64 0
@AF_INET = global i32 0
define i8* @localhost() {
%1 = alloca %struct.in_addr
br label %2
2: ; preds = %14, %0
%3 = phi i8* [ null, %0 ], [ %6, %14 ]
%4 = phi i64 [ 0, %0 ], [ %5, %14 ]
store i64 0, i64* @errno
%5 = add i64 %4, 256
%6 = tail call i8* @realloc(i8* %3, i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i64, i64* @ENOMEM
store i64 %9, i64* @errno
%10 = tail call i32 @free(i8* %3)
br label %42
11: ; preds = %2
%12 = tail call i32 @gethostname(i8* %6, i64 %5)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i32 @memchr(i8* %6, i8 signext 0, i64 %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %2, label %22
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @free(i8* %6)
br label %42
22: ; preds = %14, %17
%23 = tail call %struct.hostent* @gethostbyname(i8* %6)
%24 = icmp eq %struct.hostent* %23, null
br i1 %24, label %42, label %25
25: ; preds = %22
%26 = bitcast %struct.in_addr* %1 to i8*
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i64 0, i32 1
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to i32*
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* @AF_INET
%33 = call %struct.hostent* @gethostbyaddr(i8* %26, i32 4, i32 %32)
%34 = icmp eq %struct.hostent* %33, null
br i1 %34, label %40, label %35
35: ; preds = %25
%36 = call i32 @free(i8* %6)
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i8* @strdup(i32 %38)
br label %40
40: ; preds = %35, %25
%41 = phi i8* [ %39, %35 ], [ %6, %25 ]
br label %42
42: ; preds = %8, %20, %40, %22
%43 = phi i8* [ null, %20 ], [ %41, %40 ], [ %6, %22 ], [ null, %8 ]
ret i8* %43
}