#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 */

/* Variables and functions */
scalar_t__ ENAMETOOLONG;
scalar_t__ ENOMEM;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ gethostname(char *, size_t);
char *malloc(size_t);
int /*<<< orphan*/ memchr(char *, char, size_t);
char *realloc(char *, size_t);
char *localhost() {
  static char *buf = 0;
  static size_t buf_len = 0;
  if (!buf) {
    do {
      errno = 0;
      if (buf) {
        char *new;
        buf_len += buf_len;
        new = realloc(buf, buf_len);
        if (!new) {
          free(buf);
          buf = 0;
          errno = ENOMEM;
          return 0;
        } else
          buf = new;
      } else {
        buf_len = 128;
        buf = malloc(buf_len);
        if (!buf) {
          errno = ENOMEM;
          return 0;
        }
      }
    } while ((gethostname(buf, buf_len) == 0 && !memchr(buf, '\0', buf_len)) ||
             errno == ENAMETOOLONG);
    if (errno) {
      free(buf);
      buf = 0;
    }
  }
  return buf;
}


x86 assembly (clang, -O0):
<localhost>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
cmpq   $0x0,0x2ef0(%rip)
jne    12f1 <localhost+0x1a1>
jmp    116b <localhost+0x1b>
mov    0x2e66(%rip),%rax
movq   $0x0,(%rax)
cmpq   $0x0,0x2ecf(%rip)
je     1206 <localhost+0xb6>
mov    0x2eca(%rip),%rax
add    0x2ec3(%rip),%rax
mov    %rax,0x2ebc(%rip)
mov    0x2ead(%rip),%rdi
mov    0x2eae(%rip),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f6 <localhost+0xa6>
mov    0x2e8b(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2e7b(%rip)
mov    0x2e04(%rip),%rax
mov    (%rax),%rcx
mov    0x2df2(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12fc <localhost+0x1ac>
mov    -0x10(%rbp),%rax
mov    %rax,0x2e4f(%rip)
jmp    1258 <localhost+0x108>
movq   $0x80,0x2e47(%rip)
mov    0x2e40(%rip),%rdi
call   1050 <malloc@plt>
mov    %rax,0x2e2c(%rip)
cmpq   $0x0,0x2e24(%rip)
jne    1253 <localhost+0x103>
mov    0x2da7(%rip),%rax
mov    (%rax),%rcx
mov    0x2d95(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12fc <localhost+0x1ac>
jmp    1258 <localhost+0x108>
jmp    125d <localhost+0x10d>
mov    0x2dec(%rip),%rdi
mov    0x2ded(%rip),%rsi
call   1070 <gethostname@plt>
cmp    $0x0,%rax
jne    129f <localhost+0x14f>
mov    0x2dcf(%rip),%rdi
mov    0x2dd0(%rip),%rdx
xor    %esi,%esi
call   1040 <memchr@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
je     12b9 <localhost+0x169>
mov    0x2d32(%rip),%rax
mov    (%rax),%rax
mov    0x2d20(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    116b <localhost+0x1b>
mov    0x2d0d(%rip),%rax
cmpq   $0x0,(%rax)
je     12ec <localhost+0x19c>
mov    0x2d74(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2d64(%rip)
jmp    12f1 <localhost+0x1a1>
mov    0x2d58(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<localhost>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2ef1(%rip),%rax
test   %rax,%rax
jne    126f <localhost+0x11f>
xor    %r14d,%r14d
mov    0x2e66(%rip),%r15
mov    0x2e57(%rip),%r12
jmp    1194 <localhost+0x44>
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rcx
cmp    (%r12),%rcx
jne    122c <localhost+0xdc>
mov    0x2ebc(%rip),%r14
movq   $0x0,(%r15)
test   %r14,%r14
je     11d0 <localhost+0x80>
mov    0x2eb1(%rip),%rbx
add    %rbx,%rbx
mov    %rbx,0x2ea7(%rip)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
je     124d <localhost+0xfd>
mov    %rax,0x2e84(%rip)
jmp    11f6 <localhost+0xa6>
xchg   %ax,%ax
movq   $0x80,0x2e7d(%rip)
mov    $0x80,%ebx
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,0x2e5f(%rip)
test   %rax,%rax
je     1260 <localhost+0x110>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <gethostname@plt>
test   %rax,%rax
jne    1180 <localhost+0x30>
mov    0x2e3f(%rip),%rdi
mov    0x2e40(%rip),%rdx
xor    %esi,%esi
call   1040 <memchr@plt>
test   %eax,%eax
jne    1180 <localhost+0x30>
jmp    118d <localhost+0x3d>
mov    0x2e1d(%rip),%rax
test   %rcx,%rcx
je     126f <localhost+0x11f>
mov    %rax,%rdi
call   1030 <free@plt>
movq   $0x0,0x2e05(%rip)
jmp    126d <localhost+0x11d>
mov    %r14,%rdi
call   1030 <free@plt>
movq   $0x0,0x2df0(%rip)
mov    0x2d79(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@localhost.buf = internal global i8* null
@localhost.buf_len = internal global i64 0
@errno = global i64 0
@ENOMEM = global i64 0
@ENAMETOOLONG = global i64 0
define i8* @localhost() {
%1 = alloca i8*
%2 = alloca i8*
%3 = load i8*, i8** @localhost.buf
%4 = icmp ne i8* %3, null
br i1 %4, label %57, label %5
5: ; preds = %0
br label %6
6: ; preds = %48, %5
store i64 0, i64* @errno
%7 = load i8*, i8** @localhost.buf
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %25
9: ; preds = %6
%10 = load i64, i64* @localhost.buf_len
%11 = load i64, i64* @localhost.buf_len
%12 = add i64 %11, %10
store i64 %12, i64* @localhost.buf_len
%13 = load i8*, i8** @localhost.buf
%14 = load i64, i64* @localhost.buf_len
%15 = call i8* @realloc(i8* %13, i64 %14)
store i8* %15, i8** %2
%16 = load i8*, i8** %2
%17 = icmp ne i8* %16, null
br i1 %17, label %22, label %18
18: ; preds = %9
%19 = load i8*, i8** @localhost.buf
%20 = call i32 @free(i8* %19)
store i8* null, i8** @localhost.buf
%21 = load i64, i64* @ENOMEM
store i64 %21, i64* @errno
store i8* null, i8** %1
br label %59
22: ; preds = %9
%23 = load i8*, i8** %2
store i8* %23, i8** @localhost.buf
br label %24
24: ; preds = %22
br label %33
25: ; preds = %6
store i64 128, i64* @localhost.buf_len
%26 = load i64, i64* @localhost.buf_len
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** @localhost.buf
%28 = load i8*, i8** @localhost.buf
%29 = icmp ne i8* %28, null
br i1 %29, label %32, label %30
30: ; preds = %25
%31 = load i64, i64* @ENOMEM
store i64 %31, i64* @errno
store i8* null, i8** %1
br label %59
32: ; preds = %25
br label %33
33: ; preds = %32, %24
br label %34
34: ; preds = %33
%35 = load i8*, i8** @localhost.buf
%36 = load i64, i64* @localhost.buf_len
%37 = call i64 @gethostname(i8* %35, i64 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %34
%40 = load i8*, i8** @localhost.buf
%41 = load i64, i64* @localhost.buf_len
%42 = call i32 @memchr(i8* %40, i8 signext 0, i64 %41)
%43 = icmp ne i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %39, %34
%45 = load i64, i64* @errno
%46 = load i64, i64* @ENAMETOOLONG
%47 = icmp eq i64 %45, %46
br label %48
48: ; preds = %44, %39
%49 = phi i1 [ true, %39 ], [ %47, %44 ]
br i1 %49, label %6, label %50
50: ; preds = %48
%51 = load i64, i64* @errno
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = load i8*, i8** @localhost.buf
%55 = call i32 @free(i8* %54)
store i8* null, i8** @localhost.buf
br label %56
56: ; preds = %53, %50
br label %57
57: ; preds = %56, %0
%58 = load i8*, i8** @localhost.buf
store i8* %58, i8** %1
br label %59
59: ; preds = %57, %30, %18
%60 = load i8*, i8** %1
ret i8* %60
}

LLVM IR (-O2):
@localhost.buf = internal unnamed_addr global i8* null
@localhost.buf_len = internal unnamed_addr global i64 0
@errno = global i64 0
@ENOMEM = global i64 0
@ENAMETOOLONG = global i64 0
define i8* @localhost() {
%1 = load i8*, i8** @localhost.buf
%2 = icmp eq i8* %1, null
br i1 %2, label %5, label %41
3: ; preds = %32, %27
%4 = load i8*, i8** @localhost.buf
br label %5
5: ; preds = %0, %3
%6 = phi i8* [ %4, %3 ], [ null, %0 ]
store i64 0, i64* @errno
%7 = icmp eq i8* %6, null
br i1 %7, label %17, label %8
8: ; preds = %5
%9 = load i64, i64* @localhost.buf_len
%10 = shl i64 %9, 1
store i64 %10, i64* @localhost.buf_len
%11 = tail call i8* @realloc(i8* %6, i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %14, label %13
13: ; preds = %8
store i8* %11, i8** @localhost.buf
br label %22
14: ; preds = %8
%15 = tail call i32 @free(i8* %6)
store i8* null, i8** @localhost.buf
%16 = load i64, i64* @ENOMEM
store i64 %16, i64* @errno
br label %41
17: ; preds = %5
store i64 128, i64* @localhost.buf_len
%18 = tail call dereferenceable_or_null(128) i8* @malloc(i64 128)
store i8* %18, i8** @localhost.buf
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = load i64, i64* @ENOMEM
store i64 %21, i64* @errno
br label %41
22: ; preds = %13, %17
%23 = phi i64 [ %10, %13 ], [ 128, %17 ]
%24 = phi i8* [ %11, %13 ], [ %18, %17 ]
%25 = tail call i64 @gethostname(i8* %24, i64 %23)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %22
%28 = load i8*, i8** @localhost.buf
%29 = load i64, i64* @localhost.buf_len
%30 = tail call i32 @memchr(i8* %28, i8 signext 0, i64 %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %3, label %32
32: ; preds = %27, %22
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENAMETOOLONG
%35 = icmp eq i64 %33, %34
br i1 %35, label %3, label %36
36: ; preds = %32
%37 = icmp eq i64 %33, 0
%38 = load i8*, i8** @localhost.buf
br i1 %37, label %41, label %39
39: ; preds = %36
%40 = tail call i32 @free(i8* %38)
store i8* null, i8** @localhost.buf
br label %41
41: ; preds = %36, %0, %39, %14, %20
%42 = phi i8* [ null, %14 ], [ null, %20 ], [ null, %39 ], [ %1, %0 ], [ %38, %36 ]
ret i8* %42
}