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


x86 assembly (clang, -O0):
<gethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <uname@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11af <gethostname+0x7f>
mov    -0x18(%rbp),%edi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1198 <gethostname+0x68>
mov    0x2e57(%rip),%rax
mov    (%rax),%ecx
mov    0x2e56(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x1c(%rbp)
jmp    11aa <gethostname+0x7a>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%edx
add    $0x1,%edx
call   1050 <memcpy@plt>
jmp    11af <gethostname+0x7f>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gethostname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1040 <uname@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1190 <gethostname+0x60>
mov    %eax,%ebp
mov    0x8(%rsp),%r12d
mov    %r12d,%edi
call   1030 <strlen@plt>
cmp    %r15d,%eax
jge    117e <gethostname+0x4e>
add    $0x1,%eax
mov    %r14,%rdi
mov    %r12d,%esi
mov    %eax,%edx
call   1050 <memcpy@plt>
mov    %ebp,%ebx
jmp    1190 <gethostname+0x60>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    0x2e52(%rip),%rcx
mov    %eax,(%rcx)
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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 i8*
%4 = alloca i64
%5 = alloca %struct.utsname
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
store i32 0, i32* %6
%8 = call i32 @uname(%struct.utsname* %5)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %30
11: ; preds = %2
%12 = getelementptr inbounds %struct.utsname, %struct.utsname* %5, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @strlen(i32 %13)
store i32 %14, i32* %7
%15 = load i64, i64* %4
%16 = trunc i64 %15 to i32
%17 = load i32, i32* %7
%18 = add nsw i32 %17, 1
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = load i32, i32* @EINVAL
store i32 %21, i32* @errno
store i32 -1, i32* %6
br label %29
22: ; preds = %11
%23 = load i8*, i8** %3
%24 = getelementptr inbounds %struct.utsname, %struct.utsname* %5, i32 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* %7
%27 = add nsw i32 %26, 1
%28 = call i32 @memcpy(i8* %23, i32 %25, i32 %27)
br label %29
29: ; preds = %22, %20
br label %30
30: ; preds = %29, %2
%31 = load i32, i32* %6
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 = call i32 @uname(%struct.utsname* %3)
%6 = icmp eq i32 %5, -1
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 = trunc i64 %1 to i32
%12 = icmp slt i32 %10, %11
br i1 %12, label %15, label %13
13: ; preds = %7
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
br label %18
15: ; preds = %7
%16 = add nsw i32 %10, 1
%17 = tail call i32 @memcpy(i8* %0, i32 %9, i32 %16)
br label %18
18: ; preds = %13, %15, %2
%19 = phi i32 [ -1, %2 ], [ -1, %13 ], [ %5, %15 ]
ret i32 %19
}