#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 */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ _POSIX_HOST_NAME_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ gethostname(char *, scalar_t__);
char *malloc(scalar_t__);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
char *get_hostname(void) {
  char *hostname;
  hostname = malloc(_POSIX_HOST_NAME_MAX + 1);
  if (!hostname) {
    fprintf(stderr, "Error: Failedto allocate memory\n");
    exit(EXIT_FAILURE);
  }
  if (gethostname(hostname, _POSIX_HOST_NAME_MAX))
    fprintf(stderr, "Error can't get hostname: %s", strerror(errno));
  return hostname;
}


x86 assembly (clang, -O0):
<get_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119f <get_hostname+0x4f>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%rsi
call   1050 <gethostname@plt>
cmp    $0x0,%rax
je     11ea <get_hostname+0x9a>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hostname>:
push   %r14
push   %rbx
push   %rax
mov    0x2e65(%rip),%rax
mov    (%rax),%rbx
lea    0x1(%rbx),%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     11b4 <get_hostname+0x64>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <gethostname@plt>
test   %rax,%rax
je     11a9 <get_hostname+0x59>
mov    0x2e5a(%rip),%rax
mov    (%rax),%ebx
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe84(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@_POSIX_HOST_NAME_MAX = global i64 0
@stderr = global i32 0
@.str = [33 x i8] c"Error: Failedto allocate memory\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [29 x i8] c"Error can't get hostname: %s\00"
@errno = global i32 0
define i8* @get_hostname() {
%1 = alloca i8*
%2 = load i64, i64* @_POSIX_HOST_NAME_MAX
%3 = add nsw i64 %2, 1
%4 = call i8* @malloc(i64 %3)
store i8* %4, i8** %1
%5 = load i8*, i8** %1
%6 = icmp ne i8* %5, null
br i1 %6, label %12, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i8*, i8** %1
%14 = load i64, i64* @_POSIX_HOST_NAME_MAX
%15 = call i64 @gethostname(i8* %13, i64 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %17, %12
%23 = load i8*, i8** %1
ret i8* %23
}

LLVM IR (-O2):
@_POSIX_HOST_NAME_MAX = global i64 0
@stderr = global i32 0
@.str = [33 x i8] c"Error: Failedto allocate memory\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [29 x i8] c"Error can't get hostname: %s\00"
@errno = global i32 0
define i8* @get_hostname() {
%1 = load i64, i64* @_POSIX_HOST_NAME_MAX
%2 = add nsw i64 %1, 1
%3 = tail call i8* @malloc(i64 %2)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %10
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = tail call i64 @gethostname(i8* %3, i64 %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %13, %10
ret i8* %3
}