#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*/ errno;
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
int /*<<< orphan*/ uname(struct utsname *);
int gethostname(char *name, size_t len) {
  struct utsname un;
  if (!uname(&un))
    return -1;
  if (len < strlen(un.nodename) + 1) {
    errno = EINVAL;
    return -1;
  }
  strcpy(name, un.nodename);
  return 0;
}


x86 assembly (clang, -O0):
<gethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <uname@plt>
cmp    $0x0,%eax
jne    115e <gethostname+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <gethostname+0x84>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    11a1 <gethostname+0x71>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11b4 <gethostname+0x84>
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
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rdi
call   1050 <uname@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
je     1182 <gethostname+0x52>
mov    (%rsp),%ebp
mov    %ebp,%edi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r15,%rax
jbe    1176 <gethostname+0x46>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    0x2e6e(%rip),%rcx
mov    %eax,(%rcx)
jmp    1182 <gethostname+0x52>
mov    %r14,%rdi
mov    %ebp,%esi
call   1030 <strcpy@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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