#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int _hostname_error(char *, int);
scalar_t__ gethostname(char *, int);
int /*<<< orphan*/ puts(char *);
scalar_t__ sethostname(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int _hostname(char const *name) {
  char buf[128];
  if (name != NULL) {
    if (sethostname(name, strlen(name)) != 0)
      return _hostname_error("sethostname", 1);
    return 0;
  }
  if (gethostname(buf, sizeof(buf)) != 0)
    return _hostname_error("gethostname", 1);
  puts(buf);
  return 0;
}


x86 assembly (clang, -O0):
<_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bb <_hostname+0x6b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x98(%rbp),%rdi
mov    %eax,%esi
call   1050 <sethostname@plt>
cmp    $0x0,%rax
je     11af <_hostname+0x5f>
lea    0xe63(%rip),%rdi
mov    $0x1,%esi
call   1060 <_hostname_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1202 <_hostname+0xb2>
movl   $0x0,-0x4(%rbp)
jmp    1202 <_hostname+0xb2>
lea    -0x90(%rbp),%rdi
mov    $0x80,%esi
call   1070 <gethostname@plt>
cmp    $0x0,%rax
je     11ef <_hostname+0x9f>
lea    0xe2f(%rip),%rdi
mov    $0x1,%esi
call   1060 <_hostname_error@plt>
mov    %eax,-0x4(%rbp)
jmp    1202 <_hostname+0xb2>
lea    -0x90(%rbp),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_hostname>:
push   %rbx
sub    $0x80,%rsp
test   %rdi,%rdi
je     118d <_hostname+0x3d>
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <sethostname@plt>
test   %rax,%rax
je     11ba <_hostname+0x6a>
lea    0xe85(%rip),%rdi
mov    $0x1,%esi
add    $0x80,%rsp
pop    %rbx
jmp    1060 <_hostname_error@plt>
mov    %rsp,%rdi
mov    $0x80,%esi
call   1070 <gethostname@plt>
test   %rax,%rax
je     11b2 <_hostname+0x62>
lea    0xe66(%rip),%rdi
mov    $0x1,%esi
call   1060 <_hostname_error@plt>
jmp    11bc <_hostname+0x6c>
mov    %rsp,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"sethostname\00"
@.str.1 = [12 x i8] c"gethostname\00"
define i32 @_hostname(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [128 x i8]
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %16
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = call i64 @sethostname(i8* %8, i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = call i32 @_hostname_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %14, i32* %2
br label %25
15: ; preds = %7
store i32 0, i32* %2
br label %25
16: ; preds = %1
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%18 = call i64 @gethostname(i8* %17, i32 128)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @_hostname_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 %21, i32* %2
br label %25
22: ; preds = %16
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%24 = call i32 @puts(i8* %23)
store i32 0, i32* %2
br label %25
25: ; preds = %22, %20, %15, %13
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.str = [12 x i8] c"sethostname\00"
@.str.1 = [12 x i8] c"gethostname\00"
define i32 @_hostname(i8* %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %11, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
%7 = tail call i64 @sethostname(i8* %0, i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %18, label %9
9: ; preds = %5
%10 = tail call i32 @_hostname_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 1)
br label %18
11: ; preds = %1
%12 = call i64 @gethostname(i8* %3, i32 128)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @_hostname_error(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 1)
br label %18
16: ; preds = %11
%17 = call i32 @puts(i8* %3)
br label %18
18: ; preds = %5, %16, %14, %9
%19 = phi i32 [ %10, %9 ], [ %15, %14 ], [ 0, %16 ], [ 0, %5 ]
ret i32 %19
}