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

/* Variables and functions */
int /*<<< orphan*/ EPERM;
int O_CREAT;
int O_WRONLY;
int __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ geteuid();
scalar_t__ getuid();
int open(char *, int, int);
int write(int, void *, int);
int sethostid(long int new_id) {
  int fd;
  int ret;
  if (geteuid() || getuid())
    return __set_errno(EPERM);
  if ((fd = open("/etc/hostid", O_CREAT | O_WRONLY, 0644)) < 0)
    return -1;
  ret = write(fd, (void *)&new_id, sizeof(new_id)) == sizeof(new_id) ? 0 : -1;
  close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<sethostid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <geteuid@plt>
cmp    $0x0,%rax
jne    118e <sethostid+0x2e>
mov    $0x0,%al
call   1050 <getuid@plt>
cmp    $0x0,%rax
je     11a4 <sethostid+0x44>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    %eax,-0x4(%rbp)
jmp    1212 <sethostid+0xb2>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
lea    0xe43(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11df <sethostid+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1212 <sethostid+0xb2>
mov    -0x14(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
movslq %eax,%rdx
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    $0x8,%rdx
cmove  %ecx,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sethostid>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,(%rsp)
xor    %eax,%eax
call   1060 <geteuid@plt>
test   %rax,%rax
jne    117f <sethostid+0x1f>
xor    %eax,%eax
call   1050 <getuid@plt>
test   %rax,%rax
je     1193 <sethostid+0x33>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <__set_errno@plt>
mov    0x2e5e(%rip),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe54(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
test   %eax,%eax
js     11df <sethostid+0x7f>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x8,%edx
call   1040 <write@plt>
xor    %ebp,%ebp
cmp    $0x8,%eax
setne  %bpl
neg    %ebp
mov    %ebx,%edi
call   1070 <close@plt>
jmp    11e4 <sethostid+0x84>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@EPERM = global i32 0
@.str = [12 x i8] c"/etc/hostid\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @sethostid(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i64 %0, i64* %3
%6 = call i64 (...) @geteuid()
%7 = icmp ne i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = call i64 (...) @getuid()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8, %1
%12 = load i32, i32* @EPERM
%13 = call i32 @__set_errno(i32 %12)
store i32 %13, i32* %2
br label %32
14: ; preds = %8
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_WRONLY
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17, i32 420)
store i32 %18, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %14
store i32 -1, i32* %2
br label %32
21: ; preds = %14
%22 = load i32, i32* %4
%23 = bitcast i64* %3 to i8*
%24 = call i32 @write(i32 %22, i8* %23, i32 8)
%25 = sext i32 %24 to i64
%26 = icmp eq i64 %25, 8
%27 = zext i1 %26 to i64
%28 = select i1 %26, i32 0, i32 -1
store i32 %28, i32* %5
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %5
store i32 %31, i32* %2
br label %32
32: ; preds = %21, %20, %11
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@EPERM = global i32 0
@.str = [12 x i8] c"/etc/hostid\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
define i32 @sethostid(i64 %0) {
%2 = alloca i64
store i64 %0, i64* %2
%3 = tail call i64 (...) @geteuid()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = tail call i64 (...) @getuid()
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %5, %1
%9 = load i32, i32* @EPERM
%10 = tail call i32 @__set_errno(i32 %9)
br label %23
11: ; preds = %5
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_WRONLY
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %14, i32 420)
%16 = icmp slt i32 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = bitcast i64* %2 to i8*
%19 = call i32 @write(i32 %15, i8* %18, i32 8)
%20 = icmp ne i32 %19, 8
%21 = sext i1 %20 to i32
%22 = tail call i32 @close(i32 %15)
br label %23
23: ; preds = %11, %17, %8
%24 = phi i32 [ %10, %8 ], [ %21, %17 ], [ -1, %11 ]
ret i32 %24
}