#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ free(char *);
size_t get_host_name_max();
int gethostname(char *, size_t);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main() {
  int result;
  size_t hostname_size = get_host_name_max();
  char *hostname = malloc(hostname_size);
  ((void)0);
  result = gethostname(hostname, hostname_size);
  if (result == 0) {
    printf("%s\n", hostname);
    free(hostname);
    return EXIT_SUCCESS;
  } else {
    fprintf(stderr, "gethostname returned %d: %s\n", result, strerror(errno));
    free(hostname);
    return EXIT_FAILURE;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <get_host_name_max@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <gethostname@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11db <main+0x6b>
mov    -0x18(%rbp),%rsi
lea    0xe5d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1225 <main+0xb5>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%edx
mov    %rax,%rcx
lea    0xdf5(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1030 <get_host_name_max@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1080 <gethostname@plt>
test   %eax,%eax
je     11cf <main+0x5f>
mov    %eax,%ebp
mov    0x2e3f(%rip),%rax
mov    (%rax),%r14d
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe47(%rip),%rsi
mov    %r14d,%edi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    0x2e03(%rip),%rbp
jmp    11e5 <main+0x75>
lea    0xe43(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x2de3(%rip),%rbp
mov    %rbx,%rdi
call   1040 <free@plt>
mov    0x0(%rbp),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"gethostname returned %d: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
store i32 0, i32* %1
%5 = call i64 (...) @get_host_name_max()
store i64 %5, i64* %3
%6 = load i64, i64* %3
%7 = call i8* @malloc(i64 %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = load i64, i64* %3
%10 = call i32 @gethostname(i8* %8, i64 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %0
%14 = load i8*, i8** %4
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i8*, i8** %4
%17 = call i32 @free(i8* %16)
%18 = load i32, i32* @EXIT_SUCCESS
store i32 %18, i32* %1
br label %28
19: ; preds = %0
%20 = load i32, i32* @stderr
%21 = load i32, i32* %2
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %23)
%25 = load i8*, i8** %4
%26 = call i32 @free(i8* %25)
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %1
br label %28
28: ; preds = %19, %13
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
@EXIT_SUCCESS = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"gethostname returned %d: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
define i32 @main() {
%1 = tail call i64 (...) @get_host_name_max()
%2 = tail call i8* @malloc(i64 %1)
%3 = tail call i32 @gethostname(i8* %2, i64 %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %3, i8* %10)
br label %12
12: ; preds = %7, %5
%13 = phi i32* [ @EXIT_SUCCESS, %5 ], [ @EXIT_FAILURE, %7 ]
%14 = tail call i32 @free(i8* %2)
%15 = load i32, i32* %13
ret i32 %15
}