#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 EINVAL;
int ENAMETOOLONG;
int errno;
size_t os_strlcpy(char *, int /*<<< orphan*/, size_t);
scalar_t__ uname(struct utsname *);
int os_local_host_name(char *hostname, size_t size) {
  static struct utsname host;
  if (hostname == NULL || size == 0)
    return -EINVAL;
  if (uname(&host) != 0)
    return -errno;
  if (os_strlcpy((hostname), (host.nodename), (size)) >= size)
    return -ENAMETOOLONG;
  return 0;
}


x86 assembly (clang, -O0):
<os_local_host_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <os_local_host_name+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1159 <os_local_host_name+0x39>
xor    %eax,%eax
mov    0x2e89(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <os_local_host_name+0x99>
lea    0x2ed4(%rip),%rdi
call   1030 <uname@plt>
cmp    $0x0,%rax
je     1182 <os_local_host_name+0x62>
xor    %eax,%eax
mov    0x2e68(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <os_local_host_name+0x99>
mov    -0x10(%rbp),%rdi
mov    0x2ea8(%rip),%esi
mov    -0x18(%rbp),%rdx
call   1040 <os_strlcpy@plt>
cmp    -0x18(%rbp),%rax
jb     11b2 <os_local_host_name+0x92>
xor    %eax,%eax
mov    0x2e28(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11b9 <os_local_host_name+0x99>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_local_host_name>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     114e <os_local_host_name+0x2e>
mov    %rsi,%r14
test   %rsi,%rsi
je     114e <os_local_host_name+0x2e>
mov    %rdi,%rbx
lea    0x2ef9(%rip),%rdi
call   1030 <uname@plt>
test   %rax,%rax
je     1161 <os_local_host_name+0x41>
mov    0x2e94(%rip),%rcx
jmp    1155 <os_local_host_name+0x35>
mov    0x2e83(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2ecd(%rip),%esi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <os_strlcpy@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %r14,%rcx
jb     1159 <os_local_host_name+0x39>
mov    0x2e4d(%rip),%rcx
jmp    1155 <os_local_host_name+0x35>

LLVM IR (-O0):
%struct.utsname = type { i32 }
@os_local_host_name.host = internal global %struct.utsname zeroinitializer
@EINVAL = global i32 0
@errno = global i32 0
@ENAMETOOLONG = global i32 0
define i32 @os_local_host_name(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%6 = load i8*, i8** %4
%7 = icmp eq i8* %6, null
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i64, i64* %5
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8, %2
%12 = load i32, i32* @EINVAL
%13 = sub nsw i32 0, %12
store i32 %13, i32* %3
br label %31
14: ; preds = %8
%15 = call i64 @uname(%struct.utsname* @os_local_host_name.host)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
store i32 %19, i32* %3
br label %31
20: ; preds = %14
%21 = load i8*, i8** %4
%22 = load i32, i32* getelementptr inbounds (%struct.utsname, %struct.utsname* @os_local_host_name.host, i32 0, i32 0)
%23 = load i64, i64* %5
%24 = call i64 @os_strlcpy(i8* %21, i32 %22, i64 %23)
%25 = load i64, i64* %5
%26 = icmp uge i64 %24, %25
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* @ENAMETOOLONG
%29 = sub nsw i32 0, %28
store i32 %29, i32* %3
br label %31
30: ; preds = %20
store i32 0, i32* %3
br label %31
31: ; preds = %30, %27, %17, %11
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
%struct.utsname = type { i32 }
@os_local_host_name.host = internal global %struct.utsname zeroinitializer
@EINVAL = global i32 0
@errno = global i32 0
@ENAMETOOLONG = global i32 0
define i32 @os_local_host_name(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i64 %1, 0
%5 = or i1 %3, %4
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EINVAL
%8 = sub nsw i32 0, %7
br label %22
9: ; preds = %2
%10 = tail call i64 @uname(%struct.utsname* @os_local_host_name.host)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
br label %22
15: ; preds = %9
%16 = load i32, i32* getelementptr inbounds (%struct.utsname, %struct.utsname* @os_local_host_name.host, i64 0, i32 0)
%17 = tail call i64 @os_strlcpy(i8* %0, i32 %16, i64 %1)
%18 = icmp ult i64 %17, %1
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = load i32, i32* @ENAMETOOLONG
%21 = sub nsw i32 0, %20
br label %22
22: ; preds = %15, %19, %12, %6
%23 = phi i32 [ %8, %6 ], [ %14, %12 ], [ %21, %19 ], [ 0, %15 ]
ret i32 %23
}