#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, size_t);
char *strchr(char *, char);
int gethostname(char *buf, size_t len) {
  int fd;
  int r;
  char *nl;
  if ((fd = open("/etc/hostname.file", O_RDONLY)) < 0)
    return -1;
  r = read(fd, buf, len);
  close(fd);
  if (r == -1)
    return -1;
  buf[len - 1] = '\0';
  if ((nl = strchr(buf, '\n')) != NULL)
    *nl = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<gethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
lea    0xea0(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    117d <gethostname+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <gethostname+0xa8>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0xffffffff,-0x20(%rbp)
jne    11ae <gethostname+0x6e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e8 <gethostname+0xa8>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11e1 <gethostname+0xa1>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e93(%rip),%rax
mov    (%rax),%esi
lea    0xea2(%rip),%rdi
call   1060 <open@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     11a8 <gethostname+0x68>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0xffffffff,%ebx
je     11a8 <gethostname+0x68>
movb   $0x0,-0x1(%r14,%r12,1)
mov    %r12,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     11a8 <gethostname+0x68>
movb   $0x0,(%rax)
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/etc/hostname.file\00"
@O_RDONLY = global i32 0
define i32 @gethostname(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %34
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i8*, i8** %4
%16 = load i64, i64* %5
%17 = call i32 @read(i32 %14, i8* %15, i64 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %6
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %7
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %13
store i32 -1, i32* %3
br label %34
23: ; preds = %13
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = sub i64 %25, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
%28 = load i8*, i8** %4
%29 = call i8* @strchr(i8* %28, i8 signext 10)
store i8* %29, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = load i8*, i8** %8
store i8 0, i8* %32
br label %33
33: ; preds = %31, %23
store i32 0, i32* %3
br label %34
34: ; preds = %33, %22, %12
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [19 x i8] c"/etc/hostname.file\00"
@O_RDONLY = global i32 0
define i32 @gethostname(i8* %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %16, label %6
6: ; preds = %2
%7 = tail call i32 @read(i32 %4, i8* %0, i64 %1)
%8 = tail call i32 @close(i32 %4)
%9 = icmp eq i32 %7, -1
br i1 %9, label %16, label %10
10: ; preds = %6
%11 = add i64 %1, -1
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
%13 = tail call i8* @strchr(i8* dereferenceable(1) %0, i8 signext 10)
%14 = icmp eq i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %10
store i8 0, i8* %13
br label %16
16: ; preds = %10, %15, %6, %2
%17 = phi i32 [ -1, %2 ], [ -1, %6 ], [ 0, %15 ], [ 0, %10 ]
ret i32 %17
}