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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
int uname(struct utsname *);
int gethostname(char *name, size_t len) {
  struct utsname uts;
  if (name == NULL) {
    __set_errno(EINVAL);
    return -1;
  }
  if (uname(&uts) == -1)
    return -1;
  if (strlen(uts.nodename) + 1 > len) {
    __set_errno(EINVAL);
    return -1;
  }
  strcpy(name, uts.nodename);
  return 0;
}


x86 assembly (clang, -O0):
<gethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <gethostname+0x35>
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <gethostname+0x97>
lea    -0x20(%rbp),%rdi
call   1060 <uname@plt>
cmp    $0xffffffff,%eax
jne    1193 <gethostname+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <gethostname+0x97>
mov    -0x20(%rbp),%edi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jbe    11c4 <gethostname+0x84>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <gethostname+0x97>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1040 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     118a <gethostname+0x4a>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1060 <uname@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11ac <gethostname+0x6c>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r14,%rax
jbe    119f <gethostname+0x5f>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    11ac <gethostname+0x6c>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%ebp
jmp    11ac <gethostname+0x6c>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <strcpy@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.utsname = type { i32 }
@EINVAL = 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 %9
6: ; preds = %2
%7 = load i32, i32* @EINVAL
%8 = tail call i32 @__set_errno(i32 %7)
br label %24
9: ; preds = %2
%10 = call i32 @uname(%struct.utsname* %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %24, label %12
12: ; preds = %9
%13 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i32 @strlen(i32 %14)
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = icmp ugt i64 %17, %1
br i1 %18, label %19, label %22
19: ; preds = %12
%20 = load i32, i32* @EINVAL
%21 = tail call i32 @__set_errno(i32 %20)
br label %24
22: ; preds = %12
%23 = tail call i32 @strcpy(i8* %0, i32 %14)
br label %24
24: ; preds = %9, %22, %19, %6
%25 = phi i32 [ -1, %6 ], [ -1, %19 ], [ 0, %22 ], [ -1, %9 ]
ret i32 %25
}