#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 */
struct utsname {
  int /*<<< orphan*/ sysname;
};

/* Variables and functions */
int /*<<< orphan*/ EFAULT;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ENAMETOOLONG;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
size_t strlen(int /*<<< orphan*/);
int uname(struct utsname *);
int gethostname(char *name, size_t len) {
  struct utsname buf;
  int rv;
  if (name == NULL) {
    errno = EFAULT;
    return -1;
  }
  if (len <= 0) {
    errno = EINVAL;
    return -1;
  }
  rv = uname(&buf);
  if (rv < 0) {
    return -1;
  }
  if (len < strlen(buf.sysname)) {
    errno = ENAMETOOLONG;
    return -1;
  }
  strcpy(name, buf.sysname);
  return 0;
}


x86 assembly (clang, -O0):
<gethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1169 <gethostname+0x39>
mov    0x2ea6(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <gethostname+0xd5>
cmpq   $0x0,-0x18(%rbp)
ja     1192 <gethostname+0x62>
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
mov    0x2e54(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <gethostname+0xd5>
lea    -0x20(%rbp),%rdi
call   1050 <uname@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b4 <gethostname+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <gethostname+0xd5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jae    11f2 <gethostname+0xc2>
mov    0x2ded(%rip),%rax
mov    (%rax),%ecx
mov    0x2df4(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <gethostname+0xd5>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     117d <gethostname+0x4d>
mov    %rsi,%r14
test   %rsi,%rsi
je     1186 <gethostname+0x56>
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1050 <uname@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     119d <gethostname+0x6d>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1040 <strlen@plt>
cmp    %r14,%rax
jbe    11aa <gethostname+0x7a>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
mov    0x2e5f(%rip),%rcx
mov    %eax,(%rcx)
jmp    119d <gethostname+0x6d>
mov    0x2e74(%rip),%rax
jmp    118d <gethostname+0x5d>
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2e42(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
jmp    119d <gethostname+0x6d>

LLVM IR (-O0):
%struct.utsname = type { i32 }
@EFAULT = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
@ENAMETOOLONG = global i32 0
define i32 @gethostname(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.utsname
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @EFAULT
store i32 %11, i32* @errno
store i32 -1, i32* %3
br label %35
12: ; preds = %2
%13 = load i64, i64* %5
%14 = icmp ule i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
store i32 -1, i32* %3
br label %35
17: ; preds = %12
%18 = call i32 @uname(%struct.utsname* %6)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %3
br label %35
22: ; preds = %17
%23 = load i64, i64* %5
%24 = getelementptr inbounds %struct.utsname, %struct.utsname* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @strlen(i32 %25)
%27 = icmp ult i64 %23, %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i32, i32* @ENAMETOOLONG
store i32 %29, i32* @errno
store i32 -1, i32* %3
br label %35
30: ; preds = %22
%31 = load i8*, i8** %4
%32 = getelementptr inbounds %struct.utsname, %struct.utsname* %6, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @strcpy(i8* %31, i32 %33)
store i32 0, i32* %3
br label %35
35: ; preds = %30, %28, %21, %15, %10
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.utsname = type { i32 }
@EFAULT = global i32 0
@errno = global i32 0
@EINVAL = global i32 0
@ENAMETOOLONG = global i32 0
define i32 @gethostname(i8* %0, i64 %1) {
%3 = alloca %struct.utsname
%4 = bitcast %struct.utsname* %3 to i8*
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = load i32, i32* @EFAULT
store i32 %7, i32* @errno
br label %24
8: ; preds = %2
%9 = icmp eq i64 %1, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %24
12: ; preds = %8
%13 = call i32 @uname(%struct.utsname* %3)
%14 = icmp slt i32 %13, 0
br i1 %14, label %24, label %15
15: ; preds = %12
%16 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i64 @strlen(i32 %17)
%19 = icmp ugt i64 %18, %1
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = load i32, i32* @ENAMETOOLONG
store i32 %21, i32* @errno
br label %24
22: ; preds = %15
%23 = tail call i32 @strcpy(i8* %0, i32 %17)
br label %24
24: ; preds = %12, %22, %20, %10, %6
%25 = phi i32 [ -1, %6 ], [ -1, %10 ], [ -1, %20 ], [ 0, %22 ], [ -1, %12 ]
ret i32 %25
}