#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 */
scalar_t__ EPERM;
int /*<<< orphan*/ chomp(char *);
scalar_t__ errno;
int /*<<< orphan*/ error_msg_and_die(char *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror_msg_and_die(char *);
scalar_t__ sethostname(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *xfopen(char *, char *);
int /*<<< orphan*/ xstrlen(char *);
void do_sethostname(char *s, int isfile) {
  FILE *f;
  char buf[255];
  if (!s)
    return;
  if (!isfile) {
    if (sethostname(s, xstrlen(s)) < 0) {
      if (errno == EPERM)
        error_msg_and_die("you must be root to change the hostname");
      else
        perror_msg_and_die("sethostname");
    }
  } else {
    f = xfopen(s, "r");
    while (fgets(buf, 255, f) != NULL) {
      if (buf[0] == '#') {
        continue;
      }
      chomp(buf);
      do_sethostname(buf, 0);
    }
  }
}


x86 assembly (clang, -O0):
<do_sethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11a2 <do_sethostname+0x22>
jmp    1285 <do_sethostname+0x105>
cmpl   $0x0,-0xc(%rbp)
jne    1219 <do_sethostname+0x99>
mov    -0x8(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <xstrlen@plt>
mov    -0x128(%rbp),%rdi
mov    %eax,%esi
call   1060 <sethostname@plt>
cmp    $0x0,%rax
jge    1214 <do_sethostname+0x94>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    1203 <do_sethostname+0x83>
lea    0xe07(%rip),%rdi
call   1050 <error_msg_and_die@plt>
jmp    120f <do_sethostname+0x8f>
lea    0xe1e(%rip),%rdi
call   1080 <perror_msg_and_die@plt>
jmp    1214 <do_sethostname+0x94>
jmp    1285 <do_sethostname+0x105>
mov    -0x8(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1030 <xfopen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xff,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1280 <do_sethostname+0x100>
movsbl -0x120(%rbp),%eax
cmp    $0x23,%eax
jne    1261 <do_sethostname+0xe1>
jmp    122d <do_sethostname+0xad>
lea    -0x120(%rbp),%rdi
call   10a0 <chomp@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
call   1070 <do_sethostname@plt>
jmp    122d <do_sethostname+0xad>
jmp    1285 <do_sethostname+0x105>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_sethostname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
test   %rdi,%rdi
je     1255 <do_sethostname+0xe5>
mov    %rdi,%rbx
test   %esi,%esi
je     123e <do_sethostname+0xce>
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
call   1030 <xfopen@plt>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0xff,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1255 <do_sethostname+0xe5>
mov    %rsp,%rbx
mov    0x2e16(%rip),%r13
mov    0x2dff(%rip),%rbp
lea    0xe28(%rip),%r15
lea    0xe49(%rip),%r12
jmp    1205 <do_sethostname+0x95>
mov    %r12,%rdi
call   1070 <perror_msg_and_die@plt>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1255 <do_sethostname+0xe5>
cmpb   $0x23,(%rsp)
je     11f0 <do_sethostname+0x80>
mov    %rbx,%rdi
call   1090 <chomp@plt>
mov    %rbx,%rdi
call   1080 <xstrlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <sethostname@plt>
test   %rax,%rax
jns    11f0 <do_sethostname+0x80>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
jne    11e1 <do_sethostname+0x71>
mov    %r15,%rdi
call   1050 <error_msg_and_die@plt>
jmp    11f0 <do_sethostname+0x80>
mov    %rbx,%rdi
call   1080 <xstrlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <sethostname@plt>
test   %rax,%rax
js     1267 <do_sethostname+0xf7>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d72(%rip),%rax
mov    (%rax),%rax
mov    0x2d58(%rip),%rcx
cmp    (%rcx),%rax
jne    129a <do_sethostname+0x12a>
lea    0xd7c(%rip),%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <error_msg_and_die@plt>
lea    0xd87(%rip),%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <perror_msg_and_die@plt>

LLVM IR (-O0):
@errno = global i64 0
@EPERM = global i64 0
@.str = [40 x i8] c"you must be root to change the hostname\00"
@.str.1 = [12 x i8] c"sethostname\00"
@.str.2 = [2 x i8] c"r\00"
define void @do_sethostname(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [255 x i8]
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %2
br label %48
10: ; preds = %2
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 0
br i1 %12, label %29, label %13
13: ; preds = %10
%14 = load i8*, i8** %3
%15 = load i8*, i8** %3
%16 = call i32 @xstrlen(i8* %15)
%17 = call i64 @sethostname(i8* %14, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %13
%20 = load i64, i64* @errno
%21 = load i64, i64* @EPERM
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @error_msg_and_die(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %27
25: ; preds = %19
%26 = call i32 @perror_msg_and_die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %23
br label %28
28: ; preds = %27, %13
br label %48
29: ; preds = %10
%30 = load i8*, i8** %3
%31 = call i32* @xfopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %5
br label %32
32: ; preds = %43, %42, %29
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%34 = load i32*, i32** %5
%35 = call i32* @fgets(i8* %33, i32 255, i32* %34)
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %47
37: ; preds = %32
%38 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 35
br i1 %41, label %42, label %43
42: ; preds = %37
br label %32
43: ; preds = %37
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%45 = call i32 @chomp(i8* %44)
%46 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
br label %32
47: ; preds = %32
br label %48
48: ; preds = %9, %47, %28
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EPERM = global i64 0
@.str = [40 x i8] c"you must be root to change the hostname\00"
@.str.1 = [12 x i8] c"sethostname\00"
@.str.2 = [2 x i8] c"r\00"
define void @do_sethostname(i8* %0, i32 %1) {
%3 = alloca [255 x i8]
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%5 = icmp eq i8* %0, null
br i1 %5, label %43, label %6
6: ; preds = %2
%7 = icmp eq i32 %1, 0
br i1 %7, label %8, label %20
8: ; preds = %6
%9 = tail call i32 @xstrlen(i8* %0)
%10 = tail call i64 @sethostname(i8* %0, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = load i64, i64* @EPERM
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = tail call i32 @error_msg_and_die(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %43
18: ; preds = %12
%19 = tail call i32 @perror_msg_and_die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %43
20: ; preds = %6
%21 = tail call i32* @xfopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32* @fgets(i8* %4, i32 255, i32* %21)
%23 = icmp eq i32* %22, null
br i1 %23, label %43, label %24
24: ; preds = %20, %40
%25 = load i8, i8* %4
%26 = icmp eq i8 %25, 35
br i1 %26, label %40, label %27
27: ; preds = %24
%28 = call i32 @chomp(i8* %4)
%29 = call i32 @xstrlen(i8* %4)
%30 = call i64 @sethostname(i8* %4, i32 %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %40
32: ; preds = %27
%33 = load i64, i64* @errno
%34 = load i64, i64* @EPERM
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @error_msg_and_die(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %40
38: ; preds = %32
%39 = call i32 @perror_msg_and_die(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %36, %27, %24
%41 = call i32* @fgets(i8* %4, i32 255, i32* %21)
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %24
43: ; preds = %40, %20, %16, %18, %8, %2
ret void
}