#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 gethostname(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int sethostname(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
void cmd_hostname(int argc, char *argv[]) {
  char buf[8192];
  if (argc > 1) {
    if (sethostname(argv[1], strlen(argv[1])) == -1)
      perror("sethostname");
  } else {
    if (gethostname(buf, sizeof(buf)) == -1)
      perror("gethostname");
    else
      printf("%s\n", buf);
  }
}


x86 assembly (clang, -O0):
<cmd_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
jle    11b0 <cmd_hostname+0x60>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x2018(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <strlen@plt>
mov    -0x2018(%rbp),%rdi
mov    %eax,%esi
call   1050 <sethostname@plt>
cmp    $0xffffffff,%eax
jne    11ab <cmd_hostname+0x5b>
lea    0xe5a(%rip),%rdi
call   1060 <perror@plt>
jmp    11f3 <cmd_hostname+0xa3>
lea    -0x2010(%rbp),%rdi
mov    $0x2000,%esi
call   1070 <gethostname@plt>
cmp    $0xffffffff,%eax
jne    11db <cmd_hostname+0x8b>
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
jmp    11ee <cmd_hostname+0x9e>
lea    -0x2010(%rbp),%rsi
lea    0xe2f(%rip),%rdi
call   1040 <printf@plt>
jmp    11f3 <cmd_hostname+0xa3>
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_hostname>:
push   %rbx
sub    $0x2000,%rsp
cmp    $0x2,%edi
jl     118c <cmd_hostname+0x3c>
mov    0x8(%rsi),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <sethostname@plt>
cmp    $0xffffffff,%eax
jne    11ad <cmd_hostname+0x5d>
lea    0xe81(%rip),%rdi
add    $0x2000,%rsp
pop    %rbx
jmp    1060 <perror@plt>
mov    %rsp,%rdi
mov    $0x2000,%esi
call   1070 <gethostname@plt>
cmp    $0xffffffff,%eax
je     11b6 <cmd_hostname+0x66>
lea    0xe73(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
add    $0x2000,%rsp
pop    %rbx
ret
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
jmp    11ad <cmd_hostname+0x5d>

LLVM IR (-O0):
@.str = [12 x i8] c"sethostname\00"
@.str.1 = [12 x i8] c"gethostname\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @cmd_hostname(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [8192 x i8]
store i32 %0, i32* %3
store i8** %1, i8*** %4
%6 = load i32, i32* %3
%7 = icmp sgt i32 %6, 1
br i1 %7, label %8, label %21
8: ; preds = %2
%9 = load i8**, i8*** %4
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = load i8**, i8*** %4
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @strlen(i8* %14)
%16 = call i32 @sethostname(i8* %11, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %8
%19 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %8
br label %31
21: ; preds = %2
%22 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%23 = call i32 @gethostname(i8* %22, i32 8192)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %30
27: ; preds = %21
%28 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27, %25
br label %31
31: ; preds = %30, %20
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"sethostname\00"
@.str.1 = [12 x i8] c"gethostname\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @cmd_hostname(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %14
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @strlen(i8* %8)
%10 = tail call i32 @sethostname(i8* %8, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %21
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %21
14: ; preds = %2
%15 = call i32 @gethostname(i8* %4, i32 8192)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %21
19: ; preds = %14
%20 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %21
21: ; preds = %17, %19, %6, %12
ret void
}