#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ gethostname(char *);
int /*<<< orphan*/ printf(char *, char *);
int sethostname(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  if ((argc > 1 && argv[1][0] == '-') || (argc < 2)) {
    char tmp[256];
    gethostname(tmp);
    printf("%s\n", tmp);
    return 0;
  } else {
    if (sethostname(argv[1]) == 2) {
      fprintf(stderr,
              "hostname: you don't have permission to set the host name\n");
      return 1;
    }
    FILE *file = fopen("/etc/hostname", "w");
    if (!file) {
      return 1;
    } else {
      fprintf(file, "%s\n", argv[1]);
      fclose(file);
      return 0;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1197 <main+0x37>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
je     11a1 <main+0x41>
cmpl   $0x2,-0x8(%rbp)
jge    11cc <main+0x6c>
lea    -0x110(%rbp),%rdi
call   1080 <gethostname@plt>
lea    -0x110(%rbp),%rsi
lea    0xe45(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    126a <main+0x10a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <sethostname@plt>
cmp    $0x2,%eax
jne    1206 <main+0xa6>
mov    0x2df7(%rip),%rax
movslq (%rax),%rdi
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    126a <main+0x10a>
lea    0xe31(%rip),%rdi
lea    0xe38(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    123a <main+0xda>
movl   $0x1,-0x4(%rbp)
jmp    126a <main+0x10a>
mov    -0x118(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x118(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x108,%rsp
cmp    $0x2,%edi
jl     118b <main+0x1b>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
cmpb   $0x2d,(%rdi)
jne    11b4 <main+0x44>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1080 <gethostname@plt>
lea    0xe63(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret
call   1060 <sethostname@plt>
cmp    $0x2,%eax
jne    11e5 <main+0x75>
mov    0x2e1b(%rip),%rax
movslq (%rax),%rcx
lea    0xe35(%rip),%rdi
mov    $0x39,%esi
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    $0x1,%ebx
jmp    11a7 <main+0x37>
lea    0xe52(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1221 <main+0xb1>
mov    %rax,%r14
mov    0x8(%rbx),%rdx
lea    0xdf5(%rip),%rsi
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11a7 <main+0x37>
mov    $0x1,%ebx
jmp    11a7 <main+0x37>

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@stderr = global i32 0
@.str.1 = [58 x i8] c"hostname: you don't have permission to set the host name\0A\00"
@.str.2 = [14 x i8] c"/etc/hostname\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 45
br i1 %17, label %21, label %18
18: ; preds = %10, %2
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 2
br i1 %20, label %21, label %26
21: ; preds = %18, %10
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%23 = call i32 @gethostname(i8* %22)
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %24)
store i32 0, i32* %3
br label %50
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @sethostname(i8* %29)
%31 = icmp eq i32 %30, 2
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = sext i32 %33 to i64
%35 = inttoptr i64 %34 to i32*
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %50
37: ; preds = %26
%38 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %38, i32** %7
%39 = load i32*, i32** %7
%40 = icmp ne i32* %39, null
br i1 %40, label %42, label %41
41: ; preds = %37
store i32 1, i32* %3
br label %50
42: ; preds = %37
%43 = load i32*, i32** %7
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %46)
%48 = load i32*, i32** %7
%49 = call i32 @fclose(i32* %48)
store i32 0, i32* %3
br label %50
50: ; preds = %42, %41, %32, %21
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
@stderr = global i32 0
@.str.1 = [58 x i8] c"hostname: you don't have permission to set the host name\0A\00"
@.str.2 = [14 x i8] c"/etc/hostname\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 45
br i1 %9, label %10, label %14
10: ; preds = %2, %5
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = call i32 @gethostname(i8* %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %29
14: ; preds = %5
%15 = tail call i32 @sethostname(i8* %7)
%16 = icmp eq i32 %15, 2
br i1 %16, label %17, label %22
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = sext i32 %18 to i64
%20 = inttoptr i64 %19 to i32*
%21 = tail call i64 @fwrite(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.1, i64 0, i64 0), i64 57, i64 1, i32* %20)
br label %29
22: ; preds = %14
%23 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %26)
%28 = tail call i32 @fclose(i32* %23)
br label %29
29: ; preds = %25, %22, %17, %10
%30 = phi i32 [ 0, %10 ], [ 1, %17 ], [ 0, %25 ], [ 1, %22 ]
ret i32 %30
}