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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchmod(int, int);
int openhostname(int, int);
size_t write(int, char const *, size_t);
int sethostname(const char *name, size_t len) {
  int fd = openhostname(O_TRUNC | O_CREAT, 0644);
  if (fd == -1)
    return -1;
  if (write(fd, name, len) != len)
    return -1;
  fchmod(fd, 0644);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<sethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e98(%rip),%rax
or     (%rax),%edi
mov    $0x1a4,%esi
call   1060 <openhostname@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1185 <sethostname+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <sethostname+0x87>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x18(%rbp),%rax
je     11ab <sethostname+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c7 <sethostname+0x87>
mov    -0x1c(%rbp),%edi
mov    $0x1a4,%esi
call   1050 <fchmod@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sethostname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e9c(%rip),%rax
mov    0x2e9d(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
mov    $0x1a4,%esi
call   1060 <openhostname@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     119c <sethostname+0x5c>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
cmp    %r14,%rax
jne    119c <sethostname+0x5c>
mov    %ebp,%edi
mov    $0x1a4,%esi
call   1050 <fchmod@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @sethostname(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i32, i32* @O_TRUNC
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = call i32 @openhostname(i32 %9, i32 420)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %27
14: ; preds = %2
%15 = load i32, i32* %6
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = call i64 @write(i32 %15, i8* %16, i64 %17)
%19 = load i64, i64* %5
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 -1, i32* %3
br label %27
22: ; preds = %14
%23 = load i32, i32* %6
%24 = call i32 @fchmod(i32 %23, i32 420)
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
store i32 0, i32* %3
br label %27
27: ; preds = %22, %21, %13
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @sethostname(i8* nocapture %0, i64 %1) {
%3 = load i32, i32* @O_TRUNC
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = tail call i32 @openhostname(i32 %5, i32 420)
%7 = icmp eq i32 %6, -1
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = tail call i64 @write(i32 %6, i8* %0, i64 %1)
%10 = icmp eq i64 %9, %1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @fchmod(i32 %6, i32 420)
%13 = tail call i32 @close(i32 %6)
br label %14
14: ; preds = %8, %2, %11
%15 = phi i32 [ 0, %11 ], [ -1, %2 ], [ -1, %8 ]
ret i32 %15
}