#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__ errno;
int gethostname(char *, int);
char *malloc(int);
char *realloc(char *, int);
extern char *gethostname_a(void) {
  char *name;
  int r, len = 128;
  name = malloc(len);
  while ((r = gethostname(name, len)) < 0) {
    if (errno == ENAMETOOLONG) {
      len *= 2;
      name = realloc(name, len);
    } else {
      break;
    }
  }
  if (r < 0)
    return NULL;
  else
    return name;
}


x86 assembly (clang, -O0):
<gethostname_a>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x80,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1050 <gethostname@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11a5 <gethostname_a+0x75>
mov    0x2e76(%rip),%rax
mov    (%rax),%rax
mov    0x2e64(%rip),%rcx
cmp    (%rcx),%rax
jne    119b <gethostname_a+0x6b>
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
jmp    11a0 <gethostname_a+0x70>
jmp    11a5 <gethostname_a+0x75>
jmp    114b <gethostname_a+0x1b>
cmpl   $0x0,-0x14(%rbp)
jge    11bc <gethostname_a+0x8c>
movq   $0x0,-0x8(%rbp)
jmp    11c4 <gethostname_a+0x94>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostname_a>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x80,%ebx
mov    $0x80,%edi
call   1030 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x80,%esi
call   1050 <gethostname@plt>
test   %eax,%eax
jns    119b <gethostname_a+0x6b>
mov    0x2e7e(%rip),%r15
mov    0x2e6f(%rip),%r12
nopl   0x0(%rax)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1198 <gethostname_a+0x68>
add    %ebx,%ebx
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   1050 <gethostname@plt>
test   %eax,%eax
js     1170 <gethostname_a+0x40>
jmp    119b <gethostname_a+0x6b>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ENAMETOOLONG = global i64 0
define i8* @gethostname_a() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 128, i32* %4
%5 = load i32, i32* %4
%6 = call i8* @malloc(i32 %5)
store i8* %6, i8** %2
br label %7
7: ; preds = %23, %0
%8 = load i8*, i8** %2
%9 = load i32, i32* %4
%10 = call i32 @gethostname(i8* %8, i32 %9)
store i32 %10, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %7
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENAMETOOLONG
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* %4
%18 = mul nsw i32 %17, 2
store i32 %18, i32* %4
%19 = load i8*, i8** %2
%20 = load i32, i32* %4
%21 = call i8* @realloc(i8* %19, i32 %20)
store i8* %21, i8** %2
br label %23
22: ; preds = %12
br label %24
23: ; preds = %16
br label %7
24: ; preds = %22, %7
%25 = load i32, i32* %3
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
store i8* null, i8** %1
br label %30
28: ; preds = %24
%29 = load i8*, i8** %2
store i8* %29, i8** %1
br label %30
30: ; preds = %28, %27
%31 = load i8*, i8** %1
ret i8* %31
}

LLVM IR (-O2):
@errno = global i64 0
@ENAMETOOLONG = global i64 0
define i8* @gethostname_a() {
%1 = tail call dereferenceable_or_null(128) i8* @malloc(i32 128)
%2 = tail call i32 @gethostname(i8* %1, i32 128)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %15
4: ; preds = %0, %10
%5 = phi i32 [ %11, %10 ], [ 128, %0 ]
%6 = phi i8* [ %12, %10 ], [ %1, %0 ]
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENAMETOOLONG
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %15
10: ; preds = %4
%11 = shl nsw i32 %5, 1
%12 = tail call i8* @realloc(i8* %6, i32 %11)
%13 = tail call i32 @gethostname(i8* %12, i32 %11)
%14 = icmp slt i32 %13, 0
br i1 %14, label %4, label %15
15: ; preds = %10, %4, %0
%16 = phi i8* [ %1, %0 ], [ null, %4 ], [ %12, %10 ]
ret i8* %16
}