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

/* Variables and functions */
int /*<<< orphan*/ R_OK;
int access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int const, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ getenv(char *);
char *str_replace(char *, int /*<<< orphan*/, int /*<<< orphan*/);
size_t strlen(char const *);
scalar_t__ strncmp(char *, char const *, size_t);
char *strtok(char *, char *);
char *resolveAlias(const char *name, char *buf, const int buflen) {
  const char *eHostAliases =
      str_replace("~", getenv("HOME"), getenv("HOSTALIASES"));
  if (eHostAliases != NULL && access(eHostAliases, R_OK) != -1) {
    FILE *f = fopen(eHostAliases, "r");
    buf[buflen - 1] = '\0';
    size_t len = strlen(name);
    while (fgets(buf, buflen - 1, f)) {
      if (strncmp(buf, name, len) == 0) {
        char *token = strtok(buf, " ");
        while (token != NULL) {
          token = strtok(NULL, " ");
          if (token != NULL) {
            fclose(f);
            return token;
          }
        }
      }
    }
    fclose(f);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<resolveAlias>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    0xe58(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,-0x44(%rbp)
lea    0xe4e(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x44(%rbp),%esi
mov    %eax,%edx
lea    0xe36(%rip),%rdi
call   1080 <str_replace@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12cf <resolveAlias+0x13f>
mov    -0x28(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     12cf <resolveAlias+0x13f>
mov    -0x28(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x30(%rbp),%rdx
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12c6 <resolveAlias+0x136>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12c1 <resolveAlias+0x131>
mov    -0x18(%rbp),%rdi
lea    0xda7(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12bc <resolveAlias+0x12c>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd88(%rip),%rsi
call   10b0 <strtok@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     12b7 <resolveAlias+0x127>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d7 <resolveAlias+0x147>
jmp    1277 <resolveAlias+0xe7>
jmp    12c1 <resolveAlias+0x131>
jmp    122b <resolveAlias+0x9b>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolveAlias>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,(%rsp)
lea    0xe56(%rip),%rdi
call   1030 <getenv@plt>
mov    %eax,%ebp
lea    0xe4d(%rip),%rdi
call   1030 <getenv@plt>
lea    0xe3a(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1080 <str_replace@plt>
test   %rax,%rax
je     129e <resolveAlias+0x10e>
mov    %rax,%rbp
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <access@plt>
cmp    $0xffffffff,%eax
je     129e <resolveAlias+0x10e>
lea    0xe17(%rip),%rsi
mov    %rbp,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r15
movslq %r14d,%r12
movb   $0x0,-0x1(%rbx,%r12,1)
add    $0xffffffffffffffff,%r12
mov    (%rsp),%rdi
call   1060 <strlen@plt>
mov    %rax,%r13
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12a3 <resolveAlias+0x113>
lea    0xddb(%rip),%rbp
jmp    1253 <resolveAlias+0xc3>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     12a3 <resolveAlias+0x113>
mov    %rbx,%rdi
mov    (%rsp),%rsi
mov    %r13,%rdx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1240 <resolveAlias+0xb0>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10b0 <strtok@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %rax,%rax
je     1240 <resolveAlias+0xb0>
xor    %edi,%edi
mov    %rbp,%rsi
call   10b0 <strtok@plt>
mov    %rax,%r14
mov    $0x0,%eax
test   %r14,%r14
je     1280 <resolveAlias+0xf0>
jmp    12a6 <resolveAlias+0x116>
xor    %r14d,%r14d
jmp    12ae <resolveAlias+0x11e>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"~\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [12 x i8] c"HOSTALIASES\00"
@R_OK = global i32 0
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c" \00"
define i8* @resolveAlias(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%14 = call i8* @str_replace(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %65
17: ; preds = %3
%18 = load i8*, i8** %8
%19 = load i32, i32* @R_OK
%20 = call i32 @access(i8* %18, i32 %19)
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %65
22: ; preds = %17
%23 = load i8*, i8** %8
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %24, i32** %9
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = sub nsw i32 %26, 1
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %25, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %5
%31 = call i64 @strlen(i8* %30)
store i64 %31, i64* %10
br label %32
32: ; preds = %61, %22
%33 = load i8*, i8** %6
%34 = load i32, i32* %7
%35 = sub nsw i32 %34, 1
%36 = load i32*, i32** %9
%37 = call i64 @fgets(i8* %33, i32 %35, i32* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %62
39: ; preds = %32
%40 = load i8*, i8** %6
%41 = load i8*, i8** %5
%42 = load i64, i64* %10
%43 = call i64 @strncmp(i8* %40, i8* %41, i64 %42)
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %61
45: ; preds = %39
%46 = load i8*, i8** %6
%47 = call i8* @strtok(i8* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %47, i8** %11
br label %48
48: ; preds = %59, %45
%49 = load i8*, i8** %11
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %60
51: ; preds = %48
%52 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %52, i8** %11
%53 = load i8*, i8** %11
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i32*, i32** %9
%57 = call i32 @fclose(i32* %56)
%58 = load i8*, i8** %11
store i8* %58, i8** %4
br label %66
59: ; preds = %51
br label %48
60: ; preds = %48
br label %61
61: ; preds = %60, %39
br label %32
62: ; preds = %32
%63 = load i32*, i32** %9
%64 = call i32 @fclose(i32* %63)
br label %65
65: ; preds = %62, %17, %3
store i8* null, i8** %4
br label %66
66: ; preds = %65, %55
%67 = load i8*, i8** %4
ret i8* %67
}

LLVM IR (-O2):
@.str = [2 x i8] c"~\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [12 x i8] c"HOSTALIASES\00"
@R_OK = global i32 0
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c" \00"
define i8* @resolveAlias(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 @getenv(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%6 = tail call i8* @str_replace(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %37, label %8
8: ; preds = %3
%9 = load i32, i32* @R_OK
%10 = tail call i32 @access(i8* %6, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %37, label %12
12: ; preds = %8
%13 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = add nsw i32 %2, -1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %1, i64 %15
store i8 0, i8* %16
%17 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%18 = tail call i64 @fgets(i8* %1, i32 %14, i32* %13)
%19 = icmp eq i64 %18, 0
br i1 %19, label %34, label %20
20: ; preds = %12, %31
%21 = tail call i64 @strncmp(i8* %1, i8* %0, i64 %17)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %20
%24 = tail call i8* @strtok(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %28, %23
%26 = phi i8* [ %24, %23 ], [ null, %28 ]
%27 = icmp eq i8* %26, null
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %25, label %34
31: ; preds = %25, %20
%32 = tail call i64 @fgets(i8* %1, i32 %14, i32* %13)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %20
34: ; preds = %31, %28, %12
%35 = phi i8* [ null, %12 ], [ %29, %28 ], [ null, %31 ]
%36 = tail call i32 @fclose(i32* %13)
br label %37
37: ; preds = %34, %3, %8
%38 = phi i8* [ null, %8 ], [ null, %3 ], [ %35, %34 ]
ret i8* %38
}