#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 */

/* Variables and functions */
scalar_t__ gethostname(char *, size_t);
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlen(char *);
scalar_t__ strncmp(char *, char *, int /*<<< orphan*/);
const char *get_hostname(char *buf, size_t buflen, int short_hostname) {
  char *p;
  if (gethostname(buf, buflen) != 0)
    return NULL;
  buf[buflen - 1] = '\0';
  if (strcmp(buf, "(none)") == 0 || strcmp(buf, "localhost") == 0 ||
      strncmp(buf, "localhost.", strlen("localhost.")) == 0 || buf[0] == '.')
    return NULL;
  if (short_hostname) {
    p = strchr(buf, '.');
    if (p)
      *p = '\0';
  }
  return buf;
}


x86 assembly (clang, -O0):
<get_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <gethostname@plt>
cmp    $0x0,%rax
je     1187 <get_hostname+0x37>
movq   $0x0,-0x8(%rbp)
jmp    1253 <get_hostname+0x103>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     120b <get_hostname+0xbb>
mov    -0x10(%rbp),%rdi
lea    0xe4b(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     120b <get_hostname+0xbb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe37(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
lea    0xe25(%rip),%rsi
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     120b <get_hostname+0xbb>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
jne    1218 <get_hostname+0xc8>
movq   $0x0,-0x8(%rbp)
jmp    1253 <get_hostname+0x103>
cmpl   $0x0,-0x1c(%rbp)
je     124b <get_hostname+0xfb>
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1246 <get_hostname+0xf6>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
jmp    124b <get_hostname+0xfb>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_hostname>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
call   1070 <gethostname@plt>
test   %rax,%rax
je     1171 <get_hostname+0x21>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
movb   $0x0,-0x1(%rbp,%rbx,1)
lea    0xe83(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1167 <get_hostname+0x17>
lea    0xe76(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1167 <get_hostname+0x17>
lea    0xe6c(%rip),%rbp
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1167 <get_hostname+0x17>
cmpb   $0x2e,(%rbx)
je     1167 <get_hostname+0x17>
test   %r14d,%r14d
je     1169 <get_hostname+0x19>
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1169 <get_hostname+0x19>
movb   $0x0,(%rax)
jmp    1169 <get_hostname+0x19>

LLVM IR (-O0):
@.str = [7 x i8] c"(none)\00"
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [11 x i8] c"localhost.\00"
define i8* @get_hostname(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load i8*, i8** %5
%10 = load i64, i64* %6
%11 = call i64 @gethostname(i8* %9, i64 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i8* null, i8** %4
br label %51
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = sub i64 %16, 1
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8 0, i8* %18
%19 = load i8*, i8** %5
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %37, label %22
22: ; preds = %14
%23 = load i8*, i8** %5
%24 = call i64 @strcmp(i8* %23, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %37, label %26
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%29 = call i64 @strncmp(i8* %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %26
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i64 0
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 46
br i1 %36, label %37, label %38
37: ; preds = %31, %26, %22, %14
store i8* null, i8** %4
br label %51
38: ; preds = %31
%39 = load i32, i32* %7
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = load i8*, i8** %5
%43 = call i8* @strchr(i8* %42, i8 signext 46)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = icmp ne i8* %44, null
br i1 %45, label %46, label %48
46: ; preds = %41
%47 = load i8*, i8** %8
store i8 0, i8* %47
br label %48
48: ; preds = %46, %41
br label %49
49: ; preds = %48, %38
%50 = load i8*, i8** %5
store i8* %50, i8** %4
br label %51
51: ; preds = %49, %37, %13
%52 = load i8*, i8** %4
ret i8* %52
}

LLVM IR (-O2):
@.str = [7 x i8] c"(none)\00"
@.str.1 = [10 x i8] c"localhost\00"
@.str.2 = [11 x i8] c"localhost.\00"
define i8* @get_hostname(i8* %0, i64 %1, i32 %2) {
%4 = tail call i64 @gethostname(i8* %0, i64 %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %27
6: ; preds = %3
%7 = add i64 %1, -1
%8 = getelementptr inbounds i8, i8* %0, i64 %7
store i8 0, i8* %8
%9 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i64 %9, 0
br i1 %10, label %27, label %11
11: ; preds = %6
%12 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i64 %12, 0
br i1 %13, label %27, label %14
14: ; preds = %11
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %14
%19 = load i8, i8* %0
%20 = icmp eq i8 %19, 46
br i1 %20, label %27, label %21
21: ; preds = %18
%22 = icmp eq i32 %2, 0
br i1 %22, label %27, label %23
23: ; preds = %21
%24 = tail call i8* @strchr(i8* %0, i8 signext 46)
%25 = icmp eq i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %23
store i8 0, i8* %24
br label %27
27: ; preds = %21, %26, %23, %6, %11, %14, %18, %3
%28 = phi i8* [ null, %3 ], [ null, %18 ], [ null, %14 ], [ null, %11 ], [ null, %6 ], [ %0, %23 ], [ %0, %26 ], [ %0, %21 ]
ret i8* %28
}