#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) {
        buf_len += buf_len;
        buf = realloc(buf, buf_len);
      } 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    $0x10,%rsp
cmpq   $0x0,0x2ef0(%rip)
jne    12a1 <localhost+0x151>
jmp    116b <localhost+0x1b>
mov    0x2e66(%rip),%rax
movq   $0x0,(%rax)
cmpq   $0x0,0x2ecf(%rip)
je     11bb <localhost+0x6b>
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,0x2e9a(%rip)
jmp    11d9 <localhost+0x89>
movq   $0x80,0x2e92(%rip)
mov    0x2e8b(%rip),%rdi
call   1050 <malloc@plt>
mov    %rax,0x2e77(%rip)
cmpq   $0x0,0x2e6f(%rip)
jne    1208 <localhost+0xb8>
mov    0x2df2(%rip),%rax
mov    (%rax),%rcx
mov    0x2de0(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    12ac <localhost+0x15c>
jmp    120d <localhost+0xbd>
mov    0x2e3c(%rip),%rdi
mov    0x2e3d(%rip),%rsi
call   1070 <gethostname@plt>
cmp    $0x0,%rax
jne    124f <localhost+0xff>
mov    0x2e1f(%rip),%rdi
mov    0x2e20(%rip),%rdx
xor    %esi,%esi
call   1040 <memchr@plt>
mov    %eax,%ecx
mov    $0x1,%al
cmp    $0x0,%ecx
mov    %al,-0x9(%rbp)
je     1269 <localhost+0x119>
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    0x2d70(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x9(%rbp)
mov    -0x9(%rbp),%al
test   $0x1,%al
jne    116b <localhost+0x1b>
mov    0x2d5d(%rip),%rax
cmpq   $0x0,(%rax)
je     129c <localhost+0x14c>
mov    0x2dc4(%rip),%rdi
call   1030 <free@plt>
movq   $0x0,0x2db4(%rip)
jmp    12a1 <localhost+0x151>
mov    0x2da8(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<localhost>:
push   %r15
push   %r14
push   %rbx
mov    0x2ef4(%rip),%rax
test   %rax,%rax
jne    1248 <localhost+0xf8>
xor    %edi,%edi
mov    0x2e6a(%rip),%r14
mov    0x2e5b(%rip),%r15
jmp    1193 <localhost+0x43>
nopw   0x0(%rax,%rax,1)
mov    (%r14),%rcx
cmp    (%r15),%rcx
jne    1227 <localhost+0xd7>
mov    0x2ebd(%rip),%rdi
movq   $0x0,(%r14)
test   %rdi,%rdi
je     11c0 <localhost+0x70>
mov    0x2eb2(%rip),%rbx
add    %rbx,%rbx
mov    %rbx,0x2ea8(%rip)
mov    %rbx,%rsi
call   1060 <realloc@plt>
jmp    11da <localhost+0x8a>
nopw   0x0(%rax,%rax,1)
movq   $0x80,0x2e8d(%rip)
mov    $0x80,%ebx
mov    $0x80,%edi
call   1050 <malloc@plt>
mov    %rax,0x2e6f(%rip)
test   %rax,%rax
je     1218 <localhost+0xc8>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1070 <gethostname@plt>
test   %rax,%rax
jne    1180 <localhost+0x30>
mov    0x2e53(%rip),%rdi
mov    0x2e54(%rip),%rdx
xor    %esi,%esi
call   1040 <memchr@plt>
test   %eax,%eax
jne    1180 <localhost+0x30>
jmp    118c <localhost+0x3c>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r14)
jmp    1246 <localhost+0xf6>
mov    0x2e22(%rip),%rax
test   %rcx,%rcx
je     1248 <localhost+0xf8>
mov    %rax,%rdi
call   1030 <free@plt>
movq   $0x0,0x2e0a(%rip)
xor    %eax,%eax
pop    %rbx
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 = load i8*, i8** @localhost.buf
%3 = icmp ne i8* %2, null
br i1 %3, label %47, label %4
4: ; preds = %0
br label %5
5: ; preds = %38, %4
store i64 0, i64* @errno
%6 = load i8*, i8** @localhost.buf
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = load i64, i64* @localhost.buf_len
%10 = load i64, i64* @localhost.buf_len
%11 = add i64 %10, %9
store i64 %11, i64* @localhost.buf_len
%12 = load i8*, i8** @localhost.buf
%13 = load i64, i64* @localhost.buf_len
%14 = call i8* @realloc(i8* %12, i64 %13)
store i8* %14, i8** @localhost.buf
br label %18
15: ; preds = %5
store i64 128, i64* @localhost.buf_len
%16 = load i64, i64* @localhost.buf_len
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** @localhost.buf
br label %18
18: ; preds = %15, %8
%19 = load i8*, i8** @localhost.buf
%20 = icmp ne i8* %19, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = load i64, i64* @ENOMEM
store i64 %22, i64* @errno
store i8* null, i8** %1
br label %49
23: ; preds = %18
br label %24
24: ; preds = %23
%25 = load i8*, i8** @localhost.buf
%26 = load i64, i64* @localhost.buf_len
%27 = call i64 @gethostname(i8* %25, i64 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %24
%30 = load i8*, i8** @localhost.buf
%31 = load i64, i64* @localhost.buf_len
%32 = call i32 @memchr(i8* %30, i8 signext 0, i64 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29, %24
%35 = load i64, i64* @errno
%36 = load i64, i64* @ENAMETOOLONG
%37 = icmp eq i64 %35, %36
br label %38
38: ; preds = %34, %29
%39 = phi i1 [ true, %29 ], [ %37, %34 ]
br i1 %39, label %5, label %40
40: ; preds = %38
%41 = load i64, i64* @errno
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i8*, i8** @localhost.buf
%45 = call i32 @free(i8* %44)
store i8* null, i8** @localhost.buf
br label %46
46: ; preds = %43, %40
br label %47
47: ; preds = %46, %0
%48 = load i8*, i8** @localhost.buf
store i8* %48, i8** %1
br label %49
49: ; preds = %47, %21
%50 = load i8*, i8** %1
ret i8* %50
}

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 %37
3: ; preds = %28, %23
%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 %12, 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)
br label %14
12: ; preds = %5
store i64 128, i64* @localhost.buf_len
%13 = tail call dereferenceable_or_null(128) i8* @malloc(i64 128)
br label %14
14: ; preds = %12, %8
%15 = phi i64 [ 128, %12 ], [ %10, %8 ]
%16 = phi i8* [ %13, %12 ], [ %11, %8 ]
store i8* %16, i8** @localhost.buf
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i64, i64* @ENOMEM
store i64 %19, i64* @errno
br label %37
20: ; preds = %14
%21 = tail call i64 @gethostname(i8* %16, i64 %15)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i8*, i8** @localhost.buf
%25 = load i64, i64* @localhost.buf_len
%26 = tail call i32 @memchr(i8* %24, i8 signext 0, i64 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %3, label %28
28: ; preds = %23, %20
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENAMETOOLONG
%31 = icmp eq i64 %29, %30
br i1 %31, label %3, label %32
32: ; preds = %28
%33 = icmp eq i64 %29, 0
%34 = load i8*, i8** @localhost.buf
br i1 %33, label %37, label %35
35: ; preds = %32
%36 = tail call i32 @free(i8* %34)
store i8* null, i8** @localhost.buf
br label %37
37: ; preds = %32, %0, %35, %18
%38 = phi i8* [ null, %18 ], [ null, %35 ], [ %1, %0 ], [ %34, %32 ]
ret i8* %38
}