#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 */
typedef int /*<<< orphan*/ default_id;

/* Variables and functions */
scalar_t__ gethostname(char *, size_t);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int get_server_id(char *buf, size_t len) {
  const char default_id[] = "gg-unknown";
  if (!len)
    return -1;
  if (gethostname(buf, len)) {
    ({});
    if (len >= sizeof(default_id)) {
      memcpy(buf, default_id, sizeof(default_id));
      return 0;
    } else {
      return -1;
    }
  } else {
    buf[len - 1] = 0;
    return 0;
  }
}


x86 assembly (clang, -O0):
<get_server_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xec9(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xec6(%rip),%ax
mov    %ax,-0x1b(%rbp)
mov    0xebe(%rip),%al
mov    %al,-0x19(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1166 <get_server_id+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <get_server_id+0xa9>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <gethostname@plt>
cmp    $0x0,%rax
je     11b2 <get_server_id+0x92>
cmpq   $0xb,-0x18(%rbp)
jb     11a6 <get_server_id+0x86>
mov    -0x10(%rbp),%rdi
lea    -0x23(%rbp),%rsi
mov    $0xb,%edx
call   1030 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c9 <get_server_id+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <get_server_id+0xa9>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_server_id>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0x6f6e6b6e752d6767,%rax
6b 6e 6f
mov    %rax,(%rsp)
movl   $0x6e776f,0x7(%rsp)
mov    $0xffffffff,%ebp
test   %rsi,%rsi
je     1178 <get_server_id+0x58>
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <gethostname@plt>
test   %rax,%rax
je     1170 <get_server_id+0x50>
cmp    $0xb,%rbx
jb     1178 <get_server_id+0x58>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0xb,%edx
call   1030 <memcpy@plt>
jmp    1176 <get_server_id+0x56>
movb   $0x0,-0x1(%rbx,%r14,1)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.get_server_id.default_id = [11 x i8] c"gg-unknown\00"
define i32 @get_server_id(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca [11 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = bitcast [11 x i8]* %6 to i8*
%8 = load i64, i64* %5
%9 = icmp ne i64 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %2
store i32 -1, i32* %3
br label %29
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i64, i64* %5
%14 = call i64 @gethostname(i8* %12, i64 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %11
%17 = load i64, i64* %5
%18 = icmp uge i64 %17, 11
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8*, i8** %4
%21 = getelementptr inbounds [11 x i8], [11 x i8]* %6, i64 0, i64 0
%22 = call i32 @memcpy(i8* %20, i8* %21, i32 11)
store i32 0, i32* %3
br label %29
23: ; preds = %16
store i32 -1, i32* %3
br label %29
24: ; preds = %11
%25 = load i8*, i8** %4
%26 = load i64, i64* %5
%27 = sub i64 %26, 1
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
store i32 0, i32* %3
br label %29
29: ; preds = %24, %23, %19, %10
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@__const.get_server_id.default_id = [11 x i8] c"gg-unknown\00"
define i32 @get_server_id(i8* %0, i64 %1) {
%3 = alloca [11 x i8]
%4 = getelementptr inbounds [11 x i8], [11 x i8]* %3, i64 0, i64 0
%5 = icmp eq i64 %1, 0
br i1 %5, label %16, label %6
6: ; preds = %2
%7 = tail call i64 @gethostname(i8* %0, i64 %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = icmp ugt i64 %1, 10
br i1 %10, label %11, label %16
11: ; preds = %9
%12 = call i32 @memcpy(i8* %0, i8* %4, i32 11)
br label %16
13: ; preds = %6
%14 = add i64 %1, -1
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %9, %2, %13, %11
%17 = phi i32 [ 0, %11 ], [ 0, %13 ], [ -1, %2 ], [ -1, %9 ]
ret i32 %17
}