#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 openhostname(int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, size_t);
int gethostname(char *name, size_t len) {
  int got;
  int fd = openhostname(O_RDONLY, 0);
  if (fd == -1) {
    *name = 0;
    return 0;
  }
  got = read(fd, name, len);
  if (got >= 0)
    name[got] = 0;
  else
    *name = 0;
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<gethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <openhostname@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1170 <gethostname+0x40>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11b4 <gethostname+0x84>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     119e <gethostname+0x6e>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11a5 <gethostname+0x75>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2ea4(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
xor    %esi,%esi
call   1050 <openhostname@plt>
cmp    $0xffffffff,%eax
je     1176 <gethostname+0x46>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
test   %eax,%eax
cmovns %eax,%ebx
movb   $0x0,(%r15,%rbx,1)
mov    %ebp,%edi
call   1030 <close@plt>
jmp    117a <gethostname+0x4a>
movb   $0x0,(%r15)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@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
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @openhostname(i32 %8, i32 0)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = load i8*, i8** %4
store i8 0, i8* %13
store i32 0, i32* %3
br label %31
14: ; preds = %2
%15 = load i32, i32* %7
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = call i32 @read(i32 %15, i8* %16, i64 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp sge i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %14
%22 = load i8*, i8** %4
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
br label %28
26: ; preds = %14
%27 = load i8*, i8** %4
store i8 0, i8* %27
br label %28
28: ; preds = %26, %21
%29 = load i32, i32* %7
%30 = call i32 @close(i32 %29)
store i32 0, i32* %3
br label %31
31: ; preds = %28, %12
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
define i32 @gethostname(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @openhostname(i32 %3, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %7
6: ; preds = %2
store i8 0, i8* %0
br label %14
7: ; preds = %2
%8 = tail call i32 @read(i32 %4, i8* %0, i64 %1)
%9 = icmp sgt i32 %8, 0
%10 = select i1 %9, i32 %8, i32 0
%11 = zext i32 %10 to i64
%12 = getelementptr i8, i8* %0, i64 %11
store i8 0, i8* %12
%13 = tail call i32 @close(i32 %4)
br label %14
14: ; preds = %7, %6
ret i32 0
}