#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 uname(struct utsname *);
int gethostname(char *name, size_t len) {
  struct utsname uts;
  if (name == NULL) {
    errno = EINVAL;
    return -1;
  }
  if (uname(&uts) == -1)
    return -1;
  if (strlen(uts.nodename) + 1 > len) {
    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    1169 <gethostname+0x39>
mov    0x2e86(%rip),%rax
mov    (%rax),%ecx
mov    0x2e85(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <gethostname+0x9f>
lea    -0x20(%rbp),%rdi
call   1050 <uname@plt>
cmp    $0xffffffff,%eax
jne    1187 <gethostname+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <gethostname+0x9f>
mov    -0x20(%rbp),%edi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jbe    11bc <gethostname+0x8c>
mov    0x2e33(%rip),%rax
mov    (%rax),%ecx
mov    0x2e32(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <gethostname+0x9f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <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     117e <gethostname+0x4e>
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1050 <uname@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11a4 <gethostname+0x74>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r14,%rax
jbe    1197 <gethostname+0x67>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    0x2e66(%rip),%rcx
mov    %eax,(%rcx)
jmp    11a4 <gethostname+0x74>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    0x2e52(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
jmp    11a4 <gethostname+0x74>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <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
@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 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
store i32 -1, i32* %3
br label %30
11: ; preds = %2
%12 = call i32 @uname(%struct.utsname* %6)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 -1, i32* %3
br label %30
15: ; preds = %11
%16 = getelementptr inbounds %struct.utsname, %struct.utsname* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @strlen(i32 %17)
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = load i64, i64* %5
%22 = icmp ugt i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = load i32, i32* @EINVAL
store i32 %24, i32* @errno
store i32 -1, i32* %3
br label %30
25: ; preds = %15
%26 = load i8*, i8** %4
%27 = getelementptr inbounds %struct.utsname, %struct.utsname* %6, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @strcpy(i8* %26, i32 %28)
store i32 0, i32* %3
br label %30
30: ; preds = %25, %23, %14, %9
%31 = load i32, i32* %3
ret i32 %31
}

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