#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 hostent {
  scalar_t__ h_name;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
struct hostent *gethostbyname(char *);
scalar_t__ gethostname(char *, int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
char *strchr(char *, char);
char *strdup(char *);
char *myname(void) {
  int fd;
  char *host = NULL;
  char buf[256];
  int count;
  if ((fd = open(".me", O_RDONLY)) > -1) {
    if ((count = read(fd, buf, sizeof(buf) - 1)) > 0) {
      char *cp;
      buf[count] = '\0';
      if ((cp = strchr(buf, '\n')))
        *cp = '\0';
      if ((cp = strchr(buf, '\r')))
        *cp = '\0';
      host = strdup(buf);
    }
    close(fd);
  }
  if (!host) {
    struct hostent *hp;
    if (0 == gethostname(buf, sizeof(buf))) {
      hp = gethostbyname(buf);
      if (hp)
        host = (char *)hp->h_name;
    }
  }
  if (NULL == host)
    host = strdup("localhost");
  return host;
}


x86 assembly (clang, -O0):
<myname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movq   $0x0,-0x10(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jle    1246 <myname+0xd6>
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1050 <read@plt>
mov    %eax,-0x114(%rbp)
cmp    $0x0,%eax
jle    123e <myname+0xce>
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x120(%rbp)
cmp    $0x0,%rax
je     1202 <myname+0x92>
mov    -0x120(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x110(%rbp),%rdi
mov    $0xd,%esi
call   1030 <strchr@plt>
mov    %rax,-0x120(%rbp)
cmp    $0x0,%rax
je     122e <myname+0xbe>
mov    -0x120(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x110(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
cmpq   $0x0,-0x10(%rbp)
jne    12a9 <myname+0x139>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
call   1080 <gethostname@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12a4 <myname+0x134>
lea    -0x110(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
je     129f <myname+0x12f>
mov    -0x128(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
jmp    12a4 <myname+0x134>
jmp    12a9 <myname+0x139>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    12c5 <myname+0x155>
lea    0xd48(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<myname>:
push   %rbp
push   %rbx
sub    $0x108,%rsp
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11f7 <myname+0x87>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xff,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11f0 <myname+0x80>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11c2 <myname+0x52>
movb   $0x0,(%rax)
mov    %rsp,%rdi
mov    $0xd,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     11d7 <myname+0x67>
movb   $0x0,(%rax)
mov    %rsp,%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
test   %rbx,%rbx
jne    1218 <myname+0xa8>
jmp    11f7 <myname+0x87>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rsp,%rdi
mov    $0x100,%esi
call   1080 <gethostname@plt>
test   %rax,%rax
je     1225 <myname+0xb5>
lea    0xdf4(%rip),%rdi
call   1090 <strdup@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x108,%rsp
pop    %rbx
pop    %rbp
ret
mov    %rsp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1209 <myname+0x99>
mov    (%rax),%rbx
test   %rbx,%rbx
jne    1218 <myname+0xa8>
jmp    1209 <myname+0x99>

LLVM IR (-O0):
%struct.hostent = type { i64 }
@.str = [4 x i8] c".me\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"localhost\00"
define i8* @myname() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [256 x i8]
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.hostent*
store i8* null, i8** %2
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %1
%9 = icmp sgt i32 %8, -1
br i1 %9, label %10, label %36
10: ; preds = %0
%11 = load i32, i32* %1
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%13 = call i32 @read(i32 %11, i8* %12, i32 255)
store i32 %13, i32* %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %33
15: ; preds = %10
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %17
store i8 0, i8* %18
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%20 = call i8* @strchr(i8* %19, i8 signext 10)
store i8* %20, i8** %5
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = load i8*, i8** %5
store i8 0, i8* %23
br label %24
24: ; preds = %22, %15
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%26 = call i8* @strchr(i8* %25, i8 signext 13)
store i8* %26, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = load i8*, i8** %5
store i8 0, i8* %29
br label %30
30: ; preds = %28, %24
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%32 = call i8* @strdup(i8* %31)
store i8* %32, i8** %2
br label %33
33: ; preds = %30, %10
%34 = load i32, i32* %1
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %33, %0
%37 = load i8*, i8** %2
%38 = icmp ne i8* %37, null
br i1 %38, label %55, label %39
39: ; preds = %36
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%41 = call i64 @gethostname(i8* %40, i32 256)
%42 = icmp eq i64 0, %41
br i1 %42, label %43, label %54
43: ; preds = %39
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%45 = call %struct.hostent* @gethostbyname(i8* %44)
store %struct.hostent* %45, %struct.hostent** %6
%46 = load %struct.hostent*, %struct.hostent** %6
%47 = icmp ne %struct.hostent* %46, null
br i1 %47, label %48, label %53
48: ; preds = %43
%49 = load %struct.hostent*, %struct.hostent** %6
%50 = getelementptr inbounds %struct.hostent, %struct.hostent* %49, i32 0, i32 0
%51 = load i64, i64* %50
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %2
br label %53
53: ; preds = %48, %43
br label %54
54: ; preds = %53, %39
br label %55
55: ; preds = %54, %36
%56 = load i8*, i8** %2
%57 = icmp eq i8* null, %56
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i8* @strdup(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* %59, i8** %2
br label %60
60: ; preds = %58, %55
%61 = load i8*, i8** %2
ret i8* %61
}

LLVM IR (-O2):
%struct.hostent = type { i64 }
@.str = [4 x i8] c".me\00"
@O_RDONLY = global i32 0
@.str.1 = [10 x i8] c"localhost\00"
define i8* @myname() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %25
6: ; preds = %0
%7 = call i32 @read(i32 %4, i8* %2, i32 255)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @close(i32 %4)
br label %25
11: ; preds = %6
%12 = zext i32 %7 to i64
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %12
store i8 0, i8* %13
%14 = call i8* @strchr(i8* %2, i8 signext 10)
%15 = icmp eq i8* %14, null
br i1 %15, label %17, label %16
16: ; preds = %11
store i8 0, i8* %14
br label %17
17: ; preds = %16, %11
%18 = call i8* @strchr(i8* %2, i8 signext 13)
%19 = icmp eq i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %17
store i8 0, i8* %18
br label %21
21: ; preds = %17, %20
%22 = call i8* @strdup(i8* %2)
%23 = call i32 @close(i32 %4)
%24 = icmp eq i8* %22, null
br i1 %24, label %25, label %38
25: ; preds = %0, %9, %21
%26 = call i64 @gethostname(i8* %2, i32 256)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %25
%29 = call %struct.hostent* @gethostbyname(i8* %2)
%30 = icmp eq %struct.hostent* %29, null
br i1 %30, label %36, label %31
31: ; preds = %28
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i64 0, i32 0
%33 = load i64, i64* %32
%34 = inttoptr i64 %33 to i8*
%35 = icmp eq i64 %33, 0
br i1 %35, label %36, label %38
36: ; preds = %25, %28, %31
%37 = call dereferenceable_or_null(10) i8* @strdup(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %21, %36, %31
%39 = phi i8* [ %37, %36 ], [ %34, %31 ], [ %22, %21 ]
ret i8* %39
}