#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 {
  char *nodename;
};
typedef int /*<<< orphan*/ s;

/* Variables and functions */
char *getenv(char *);
scalar_t__ gethostname(char *, unsigned int);
scalar_t__ realloc(char *, unsigned int);
int /*<<< orphan*/ strncpy(char *, char *, int);
scalar_t__ uname(struct utsname *);
char *__get_sys_hostname(void) {
  static char s[512];
  unsigned int j = 1024;
  struct utsname me;
  char *q, *p;
  p = getenv("HOSTNAME");
  if (p)
    return p;
  if (uname(&me) == 0) {
    for (q = me.nodename; q < s + (sizeof(s) - 2); q++) {
      if (*q == '.')
        *q = '\0';
      if (*q == '\0') {
        strncpy(s, me.nodename, sizeof(s) - 2);
        return s;
      }
    }
  }
  if (gethostname(s, sizeof(s) - 1) == 0) {
    for (q = s; q < s + (sizeof(s) - 2); q++) {
      if (*q == '.')
        *q = '\0';
      if (*q == '\0')
        return s;
    }
  }
  for (p = NULL;;) {
    p = (char *)realloc(p, j);
    if (!p)
      return 0;
    if (gethostname(p, j - 1) == 0) {
      for (q = p; q < p + (j - 2); q++) {
        if (*q == '.')
          *q = '\0';
        if (*q == '\0')
          return p;
      }
    }
    j += 16;
  }
}


x86 assembly (clang, -O0):
<__get_sys_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x400,-0xc(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1187 <__get_sys_hostname+0x37>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1379 <__get_sys_hostname+0x229>
lea    -0x18(%rbp),%rdi
call   1050 <uname@plt>
cmp    $0x0,%rax
jne    1220 <__get_sys_hostname+0xd0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    0x2eb7(%rip),%rax
add    $0x1fe,%rax
cmp    %rax,-0x20(%rbp)
jae    121b <__get_sys_hostname+0xcb>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    11d0 <__get_sys_hostname+0x80>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1205 <__get_sys_hostname+0xb5>
mov    -0x18(%rbp),%rsi
lea    0x2e75(%rip),%rdi
mov    $0x1fe,%edx
call   1040 <strncpy@plt>
lea    0x2e64(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1379 <__get_sys_hostname+0x229>
jmp    120a <__get_sys_hostname+0xba>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11a2 <__get_sys_hostname+0x52>
jmp    1220 <__get_sys_hostname+0xd0>
lea    0x2e39(%rip),%rdi
mov    $0x1ff,%esi
call   1070 <gethostname@plt>
cmp    $0x0,%rax
jne    12af <__get_sys_hostname+0x15f>
lea    0x2e1e(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0x2e13(%rip),%rax
add    $0x1fe,%rax
cmp    %rax,-0x20(%rbp)
jae    12aa <__get_sys_hostname+0x15a>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1274 <__get_sys_hostname+0x124>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1294 <__get_sys_hostname+0x144>
lea    0x2dd5(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1379 <__get_sys_hostname+0x229>
jmp    1299 <__get_sys_hostname+0x149>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1246 <__get_sys_hostname+0xf6>
jmp    12af <__get_sys_hostname+0x15f>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12df <__get_sys_hostname+0x18f>
movq   $0x0,-0x8(%rbp)
jmp    1379 <__get_sys_hostname+0x229>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1070 <gethostname@plt>
cmp    $0x0,%rax
jne    136b <__get_sys_hostname+0x21b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0xc(%rbp),%edx
sub    $0x2,%edx
mov    %edx,%edx
add    %rdx,%rcx
cmp    %rcx,%rax
jae    1366 <__get_sys_hostname+0x216>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1333 <__get_sys_hostname+0x1e3>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1350 <__get_sys_hostname+0x200>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1379 <__get_sys_hostname+0x229>
jmp    1355 <__get_sys_hostname+0x205>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1300 <__get_sys_hostname+0x1b0>
jmp    136b <__get_sys_hostname+0x21b>
mov    -0xc(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0xc(%rbp)
jmp    12b7 <__get_sys_hostname+0x167>
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__get_sys_hostname>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xea0(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r14
test   %rax,%rax
je     117a <__get_sys_hostname+0x2a>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0x8(%rsp),%rdi
call   1050 <uname@plt>
test   %rax,%rax
jne    11ce <__get_sys_hostname+0x7e>
mov    0x8(%rsp),%rsi
lea    0x30c9(%rip),%rax
cmp    %rax,%rsi
jae    11ce <__get_sys_hostname+0x7e>
lea    0x2ebf(%rip),%r14
mov    %r14,%rax
sub    %rsi,%rax
add    $0x1fe,%rax
xor    %ecx,%ecx
nop
movzbl (%rsi,%rcx,1),%edx
test   %dl,%dl
je     12a8 <__get_sys_hostname+0x158>
cmp    $0x2e,%dl
je     12a4 <__get_sys_hostname+0x154>
add    $0x1,%rcx
cmp    %rcx,%rax
jne    11b0 <__get_sys_hostname+0x60>
lea    0x2e8b(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1070 <gethostname@plt>
test   %rax,%rax
je     1267 <__get_sys_hostname+0x117>
xor    %r14d,%r14d
mov    $0x400,%ebx
xor    %edi,%edi
mov    $0x400,%esi
call   1060 <realloc@plt>
test   %rax,%rax
je     116d <__get_sys_hostname+0x1d>
mov    %rax,%r15
xor    %r14d,%r14d
jmp    1229 <__get_sys_hostname+0xd9>
add    $0x10,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   1060 <realloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     116d <__get_sys_hostname+0x1d>
lea    -0x1(%rbx),%esi
mov    %r15,%rdi
call   1070 <gethostname@plt>
test   %rax,%rax
jne    1210 <__get_sys_hostname+0xc0>
lea    -0x2(%rbx),%eax
add    %r15,%rax
mov    %r15,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movzbl (%rcx),%edx
test   %dl,%dl
je     128d <__get_sys_hostname+0x13d>
cmp    $0x2e,%dl
je     128a <__get_sys_hostname+0x13a>
add    $0x1,%rcx
cmp    %rax,%rcx
jb     1250 <__get_sys_hostname+0x100>
jmp    1210 <__get_sys_hostname+0xc0>
lea    0x2ff0(%rip),%rax
xchg   %ax,%ax
movzbl (%rbx),%ecx
test   %cl,%cl
je     1298 <__get_sys_hostname+0x148>
cmp    $0x2e,%cl
je     1295 <__get_sys_hostname+0x145>
add    $0x1,%rbx
cmp    %rax,%rbx
jb     1270 <__get_sys_hostname+0x120>
jmp    11eb <__get_sys_hostname+0x9b>
movb   $0x0,(%rcx)
mov    %r15,%r14
jmp    116d <__get_sys_hostname+0x1d>
movb   $0x0,(%rbx)
lea    0x2dc1(%rip),%r14
jmp    116d <__get_sys_hostname+0x1d>
movb   $0x0,(%rsi,%rcx,1)
mov    %r14,%rdi
mov    $0x1fe,%edx
call   1040 <strncpy@plt>
jmp    116d <__get_sys_hostname+0x1d>

LLVM IR (-O0):
%struct.utsname = type { i8* }
@__get_sys_hostname.s = internal global [512 x i8] zeroinitializer
@.str = [9 x i8] c"HOSTNAME\00"
define i8* @__get_sys_hostname() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca %struct.utsname
%4 = alloca i8*
%5 = alloca i8*
store i32 1024, i32* %2
%6 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** %5
%7 = load i8*, i8** %5
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = load i8*, i8** %5
store i8* %10, i8** %1
br label %113
11: ; preds = %0
%12 = call i64 @uname(%struct.utsname* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %41
14: ; preds = %11
%15 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i32 0, i32 0
%16 = load i8*, i8** %15
store i8* %16, i8** %4
br label %17
17: ; preds = %37, %14
%18 = load i8*, i8** %4
%19 = icmp ult i8* %18, getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 510)
br i1 %19, label %20, label %40
20: ; preds = %17
%21 = load i8*, i8** %4
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 46
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = load i8*, i8** %4
store i8 0, i8* %26
br label %27
27: ; preds = %25, %20
%28 = load i8*, i8** %4
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = getelementptr inbounds %struct.utsname, %struct.utsname* %3, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = call i32 @strncpy(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), i8* %34, i32 510)
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), i8** %1
br label %113
36: ; preds = %27
br label %37
37: ; preds = %36
%38 = load i8*, i8** %4
%39 = getelementptr inbounds i8, i8* %38, i32 1
store i8* %39, i8** %4
br label %17
40: ; preds = %17
br label %41
41: ; preds = %40, %11
%42 = call i64 @gethostname(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), i32 511)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %66
44: ; preds = %41
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), i8** %4
br label %45
45: ; preds = %62, %44
%46 = load i8*, i8** %4
%47 = icmp ult i8* %46, getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 510)
br i1 %47, label %48, label %65
48: ; preds = %45
%49 = load i8*, i8** %4
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp eq i32 %51, 46
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = load i8*, i8** %4
store i8 0, i8* %54
br label %55
55: ; preds = %53, %48
%56 = load i8*, i8** %4
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), i8** %1
br label %113
61: ; preds = %55
br label %62
62: ; preds = %61
%63 = load i8*, i8** %4
%64 = getelementptr inbounds i8, i8* %63, i32 1
store i8* %64, i8** %4
br label %45
65: ; preds = %45
br label %66
66: ; preds = %65, %41
store i8* null, i8** %5
br label %67
67: ; preds = %110, %66
%68 = load i8*, i8** %5
%69 = load i32, i32* %2
%70 = call i64 @realloc(i8* %68, i32 %69)
%71 = inttoptr i64 %70 to i8*
store i8* %71, i8** %5
%72 = load i8*, i8** %5
%73 = icmp ne i8* %72, null
br i1 %73, label %75, label %74
74: ; preds = %67
store i8* null, i8** %1
br label %113
75: ; preds = %67
%76 = load i8*, i8** %5
%77 = load i32, i32* %2
%78 = sub i32 %77, 1
%79 = call i64 @gethostname(i8* %76, i32 %78)
%80 = icmp eq i64 %79, 0
br i1 %80, label %81, label %110
81: ; preds = %75
%82 = load i8*, i8** %5
store i8* %82, i8** %4
br label %83
83: ; preds = %106, %81
%84 = load i8*, i8** %4
%85 = load i8*, i8** %5
%86 = load i32, i32* %2
%87 = sub i32 %86, 2
%88 = zext i32 %87 to i64
%89 = getelementptr inbounds i8, i8* %85, i64 %88
%90 = icmp ult i8* %84, %89
br i1 %90, label %91, label %109
91: ; preds = %83
%92 = load i8*, i8** %4
%93 = load i8, i8* %92
%94 = sext i8 %93 to i32
%95 = icmp eq i32 %94, 46
br i1 %95, label %96, label %98
96: ; preds = %91
%97 = load i8*, i8** %4
store i8 0, i8* %97
br label %98
98: ; preds = %96, %91
%99 = load i8*, i8** %4
%100 = load i8, i8* %99
%101 = sext i8 %100 to i32
%102 = icmp eq i32 %101, 0
br i1 %102, label %103, label %105
103: ; preds = %98
%104 = load i8*, i8** %5
store i8* %104, i8** %1
br label %113
105: ; preds = %98
br label %106
106: ; preds = %105
%107 = load i8*, i8** %4
%108 = getelementptr inbounds i8, i8* %107, i32 1
store i8* %108, i8** %4
br label %83
109: ; preds = %83
br label %110
110: ; preds = %109, %75
%111 = load i32, i32* %2
%112 = add i32 %111, 16
store i32 %112, i32* %2
br label %67
113: ; preds = %103, %74, %60, %32, %9
%114 = load i8*, i8** %1
ret i8* %114
}

LLVM IR (-O2):
%struct.utsname = type { i8* }
@__get_sys_hostname.s = internal global [512 x i8] zeroinitializer
@.str = [9 x i8] c"HOSTNAME\00"
define i8* @__get_sys_hostname() {
%1 = alloca %struct.utsname
%2 = bitcast %struct.utsname* %1 to i8*
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %63
5: ; preds = %0
%6 = call i64 @uname(%struct.utsname* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %25
8: ; preds = %5
%9 = getelementptr inbounds %struct.utsname, %struct.utsname* %1, i64 0, i32 0
%10 = load i8*, i8** %9
%11 = icmp ult i8* %10, getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 510)
br i1 %11, label %12, label %25
12: ; preds = %8
%13 = ptrtoint i8* %10 to i64
%14 = sub i64 add (i64 ptrtoint ([512 x i8]* @__get_sys_hostname.s to i64), i64 510), %13
%15 = getelementptr i8, i8* %10, i64 %14
br label %16
16: ; preds = %12, %22
%17 = phi i8* [ %23, %22 ], [ %10, %12 ]
%18 = load i8, i8* %17
switch i8 %18, label %22 [
i8 46, label %19
i8 0, label %20
]
19: ; preds = %16
store i8 0, i8* %17
br label %20
20: ; preds = %16, %19
%21 = tail call i32 @strncpy(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), i8* %10, i32 510)
br label %63
22: ; preds = %16
%23 = getelementptr inbounds i8, i8* %17, i64 1
%24 = icmp eq i8* %23, %15
br i1 %24, label %25, label %16
25: ; preds = %22, %8, %5
%26 = tail call i64 @gethostname(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), i32 511)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %25, %32
%29 = phi i8* [ %33, %32 ], [ getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), %25 ]
%30 = load i8, i8* %29
switch i8 %30, label %32 [
i8 46, label %31
i8 0, label %63
]
31: ; preds = %28
store i8 0, i8* %29
br label %63
32: ; preds = %28
%33 = getelementptr inbounds i8, i8* %29, i64 1
%34 = icmp ult i8* %33, getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 510)
br i1 %34, label %28, label %35
35: ; preds = %32, %25
%36 = tail call i64 @realloc(i8* null, i32 1024)
%37 = icmp eq i64 %36, 0
br i1 %37, label %63, label %38
38: ; preds = %35, %57
%39 = phi i64 [ %59, %57 ], [ %36, %35 ]
%40 = phi i32 [ %58, %57 ], [ 1024, %35 ]
%41 = inttoptr i64 %39 to i8*
%42 = add i32 %40, -1
%43 = tail call i64 @gethostname(i8* %41, i32 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %57
45: ; preds = %38
%46 = add i32 %40, -2
%47 = zext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %41, i64 %47
br label %49
49: ; preds = %45, %54
%50 = phi i8* [ %55, %54 ], [ %41, %45 ]
%51 = load i8, i8* %50
switch i8 %51, label %54 [
i8 46, label %52
i8 0, label %61
]
52: ; preds = %49
%53 = inttoptr i64 %39 to i8*
store i8 0, i8* %50
br label %63
54: ; preds = %49
%55 = getelementptr inbounds i8, i8* %50, i64 1
%56 = icmp ult i8* %55, %48
br i1 %56, label %49, label %57
57: ; preds = %54, %38
%58 = add i32 %40, 16
%59 = tail call i64 @realloc(i8* %41, i32 %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %63, label %38
61: ; preds = %49
%62 = inttoptr i64 %39 to i8*
br label %63
63: ; preds = %28, %57, %61, %35, %52, %31, %0, %20
%64 = phi i8* [ getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), %20 ], [ %3, %0 ], [ getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), %31 ], [ %53, %52 ], [ null, %35 ], [ %62, %61 ], [ null, %57 ], [ getelementptr inbounds ([512 x i8], [512 x i8]* @__get_sys_hostname.s, i64 0, i64 0), %28 ]
ret i8* %64
}