#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*/ ENAMETOOLONG;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, size_t);
int strlen(int /*<<< orphan*/);
scalar_t__ uname(struct utsname *);
int __gethostname(char *name, size_t len) {
  struct utsname buf;
  size_t node_len;
  if (uname(&buf))
    return -1;
  node_len = strlen(buf.nodename) + 1;
  memcpy(name, buf.nodename, len < node_len ? len : node_len);
  if (node_len > len) {
    __set_errno(ENAMETOOLONG);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<__gethostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <uname@plt>
cmp    $0x0,%rax
je     116f <__gethostname+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <__gethostname+0xb0>
mov    -0x20(%rbp),%edi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11a9 <__gethostname+0x69>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11b1 <__gethostname+0x71>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x2c(%rbp),%esi
mov    -0x38(%rbp),%rdi
mov    -0x40(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    11e9 <__gethostname+0xa9>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f0 <__gethostname+0xb0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__gethostname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdi
call   1050 <uname@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    11a3 <__gethostname+0x63>
mov    (%rsp),%r15d
mov    %r15d,%edi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbp
cmp    %rbx,%rbp
mov    %rbp,%rdx
cmova  %rbx,%rdx
mov    %r14,%rdi
mov    %r15d,%esi
call   1060 <memcpy@plt>
xor    %eax,%eax
cmp    %rbx,%rbp
jbe    11a3 <__gethostname+0x63>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utsname = type { i32 }
@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 i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%8 = call i64 @uname(%struct.utsname* %6)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %37
11: ; preds = %2
%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
store i64 %16, i64* %7
%17 = load i8*, i8** %4
%18 = getelementptr inbounds %struct.utsname, %struct.utsname* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i64, i64* %5
%21 = load i64, i64* %7
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %11
%24 = load i64, i64* %5
br label %27
25: ; preds = %11
%26 = load i64, i64* %7
br label %27
27: ; preds = %25, %23
%28 = phi i64 [ %24, %23 ], [ %26, %25 ]
%29 = call i32 @memcpy(i8* %17, i32 %19, i64 %28)
%30 = load i64, i64* %7
%31 = load i64, i64* %5
%32 = icmp ugt i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %27
%34 = load i32, i32* @ENAMETOOLONG
%35 = call i32 @__set_errno(i32 %34)
store i32 -1, i32* %3
br label %37
36: ; preds = %27
store i32 0, i32* %3
br label %37
37: ; preds = %36, %33, %10
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.utsname = type { i32 }
@ENAMETOOLONG = global i32 0
define i32 @__gethostname(i8* %0, i64 %1) {
%3 = alloca %struct.utsname
%4 = bitcast %struct.utsname* %3 to i8*
%5 = call i64 @uname(%struct.utsname* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %19
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
%14 = select i1 %13, i64 %1, i64 %12
%15 = tail call i32 @memcpy(i8* %0, i32 %9, i64 %14)
br i1 %13, label %16, label %19
16: ; preds = %7
%17 = load i32, i32* @ENAMETOOLONG
%18 = tail call i32 @__set_errno(i32 %17)
br label %19
19: ; preds = %7, %2, %16
%20 = phi i32 [ -1, %16 ], [ -1, %2 ], [ 0, %7 ]
ret i32 %20
}