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

/* Variables and functions */
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
scalar_t__ strcmp(char const *, char *);
char *strtok(char *, char *);
char *terminfo_try_path(char const *, char const *);
char *load_terminfo(void) {
  char tmp[4096];
  const char *term = getenv("TERM");
  if (!term) {
    return 0;
  }
  const char *terminfo = getenv("TERMINFO");
  if (terminfo) {
    return terminfo_try_path(terminfo, term);
  }
  const char *home = getenv("HOME");
  if (home) {
    snprintf(tmp, sizeof(tmp), "%s/.terminfo", home);
    tmp[sizeof(tmp) - 1] = '\0';
    char *data = terminfo_try_path(tmp, term);
    if (data)
      return data;
  }
  const char *dirs = getenv("TERMINFO_DIRS");
  if (dirs) {
    snprintf(tmp, sizeof(tmp), "%s", dirs);
    tmp[sizeof(tmp) - 1] = '\0';
    char *dir = strtok(tmp, ":");
    while (dir) {
      const char *cdir = dir;
      if (strcmp(cdir, "") == 0) {
        cdir = "/usr/share/terminfo";
      }
      char *data = terminfo_try_path(cdir, term);
      if (data)
        return data;
      dir = strtok(0, ":");
    }
  }
  return terminfo_try_path("/usr/share/terminfo", term);
}


x86 assembly (clang, -O0):
<load_terminfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
lea    0xe9e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    1189 <load_terminfo+0x39>
movq   $0x0,-0x8(%rbp)
jmp    135c <load_terminfo+0x20c>
lea    0xe75(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1020(%rbp)
cmpq   $0x0,-0x1020(%rbp)
je     11c6 <load_terminfo+0x76>
mov    -0x1020(%rbp),%rdi
mov    -0x1018(%rbp),%rsi
call   1040 <terminfo_try_path@plt>
mov    %rax,-0x8(%rbp)
jmp    135c <load_terminfo+0x20c>
lea    0xe41(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1028(%rbp)
cmpq   $0x0,-0x1028(%rbp)
je     1247 <load_terminfo+0xf7>
lea    -0x1010(%rbp),%rdi
mov    -0x1028(%rbp),%rcx
mov    $0x1000,%esi
lea    0xe12(%rip),%rdx
call   1050 <snprintf@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x1018(%rbp),%rsi
call   1040 <terminfo_try_path@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
je     1242 <load_terminfo+0xf2>
mov    -0x1030(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    135c <load_terminfo+0x20c>
jmp    1247 <load_terminfo+0xf7>
lea    0xdd2(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
je     1345 <load_terminfo+0x1f5>
lea    -0x1010(%rbp),%rdi
mov    -0x1038(%rbp),%rcx
mov    $0x1000,%esi
lea    0xdac(%rip),%rdx
call   1050 <snprintf@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x1010(%rbp),%rdi
lea    0xd98(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x1040(%rbp)
cmpq   $0x0,-0x1040(%rbp)
je     1340 <load_terminfo+0x1f0>
mov    -0x1040(%rbp),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x1048(%rbp),%rdi
lea    0xd63(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12ec <load_terminfo+0x19c>
lea    0xd4e(%rip),%rax
mov    %rax,-0x1048(%rbp)
mov    -0x1048(%rbp),%rdi
mov    -0x1018(%rbp),%rsi
call   1040 <terminfo_try_path@plt>
mov    %rax,-0x1050(%rbp)
cmpq   $0x0,-0x1050(%rbp)
je     1324 <load_terminfo+0x1d4>
mov    -0x1050(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    135c <load_terminfo+0x20c>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd02(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0x1040(%rbp)
jmp    12a5 <load_terminfo+0x155>
jmp    1345 <load_terminfo+0x1f5>
mov    -0x1018(%rbp),%rsi
lea    0xce0(%rip),%rdi
call   1040 <terminfo_try_path@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_terminfo>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1000,%rsp
lea    0xe99(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a0 <load_terminfo+0x50>
mov    %rax,%r12
lea    0xe8a(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11a7 <load_terminfo+0x57>
mov    %rax,%rdi
mov    %r12,%rsi
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <terminfo_try_path@plt>
xor    %eax,%eax
jmp    12a3 <load_terminfo+0x153>
lea    0xe60(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11ee <load_terminfo+0x9e>
lea    0xe54(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
movb   $0x0,0xfff(%rsp)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <terminfo_try_path@plt>
test   %rax,%rax
jne    12a3 <load_terminfo+0x153>
lea    0xe2b(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1294 <load_terminfo+0x144>
lea    0xe24(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
movb   $0x0,0xfff(%rsp)
lea    0xe05(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strtok@plt>
test   %rax,%rax
je     1294 <load_terminfo+0x144>
mov    %rax,%rbx
lea    0xdef(%rip),%r14
lea    0xde9(%rip),%r13
lea    0xde0(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
cmove  %r13,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1040 <terminfo_try_path@plt>
test   %rax,%rax
jne    12a3 <load_terminfo+0x153>
xor    %edi,%edi
mov    %r15,%rsi
call   1070 <strtok@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1260 <load_terminfo+0x110>
lea    0xd98(%rip),%rdi
mov    %r12,%rsi
call   1040 <terminfo_try_path@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"TERM\00"
@.str.1 = [9 x i8] c"TERMINFO\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [13 x i8] c"%s/.terminfo\00"
@.str.4 = [14 x i8] c"TERMINFO_DIRS\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [2 x i8] c":\00"
@.str.7 = [1 x i8] zeroinitializer
@.str.8 = [20 x i8] c"/usr/share/terminfo\00"
define i8* @load_terminfo() {
%1 = alloca i8*
%2 = alloca [4096 x i8]
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %0
store i8* null, i8** %1
br label %74
15: ; preds = %0
%16 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = load i8*, i8** %3
%22 = call i8* @terminfo_try_path(i8* %20, i8* %21)
store i8* %22, i8** %1
br label %74
23: ; preds = %15
%24 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %24, i8** %5
%25 = load i8*, i8** %5
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %40
27: ; preds = %23
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%29 = load i8*, i8** %5
%30 = call i32 @snprintf(i8* %28, i32 4096, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 4095
store i8 0, i8* %31
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%33 = load i8*, i8** %3
%34 = call i8* @terminfo_try_path(i8* %32, i8* %33)
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %27
%38 = load i8*, i8** %6
store i8* %38, i8** %1
br label %74
39: ; preds = %27
br label %40
40: ; preds = %39, %23
%41 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i8* %41, i8** %7
%42 = load i8*, i8** %7
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %71
44: ; preds = %40
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%46 = load i8*, i8** %7
%47 = call i32 @snprintf(i8* %45, i32 4096, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %46)
%48 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 4095
store i8 0, i8* %48
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%50 = call i8* @strtok(i8* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i8* %50, i8** %8
br label %51
51: ; preds = %68, %44
%52 = load i8*, i8** %8
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %70
54: ; preds = %51
%55 = load i8*, i8** %8
store i8* %55, i8** %9
%56 = load i8*, i8** %9
%57 = call i64 @strcmp(i8* %56, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
%58 = icmp eq i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %54
store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i8** %9
br label %60
60: ; preds = %59, %54
%61 = load i8*, i8** %9
%62 = load i8*, i8** %3
%63 = call i8* @terminfo_try_path(i8* %61, i8* %62)
store i8* %63, i8** %10
%64 = load i8*, i8** %10
%65 = icmp ne i8* %64, null
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = load i8*, i8** %10
store i8* %67, i8** %1
br label %74
68: ; preds = %60
%69 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i8* %69, i8** %8
br label %51
70: ; preds = %51
br label %71
71: ; preds = %70, %40
%72 = load i8*, i8** %3
%73 = call i8* @terminfo_try_path(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i8* %72)
store i8* %73, i8** %1
br label %74
74: ; preds = %71, %66, %37, %19, %14
%75 = load i8*, i8** %1
ret i8* %75
}

LLVM IR (-O2):
@.str = [5 x i8] c"TERM\00"
@.str.1 = [9 x i8] c"TERMINFO\00"
@.str.2 = [5 x i8] c"HOME\00"
@.str.3 = [13 x i8] c"%s/.terminfo\00"
@.str.4 = [14 x i8] c"TERMINFO_DIRS\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [2 x i8] c":\00"
@.str.7 = [1 x i8] zeroinitializer
@.str.8 = [20 x i8] c"/usr/share/terminfo\00"
define i8* @load_terminfo() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %38, label %5
5: ; preds = %0
%6 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i8* %6, null
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i8* @terminfo_try_path(i8* %6, i8* %3)
br label %38
10: ; preds = %5
%11 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %10
%14 = call i32 @snprintf(i8* %2, i32 4096, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 4095
store i8 0, i8* %15
%16 = call i8* @terminfo_try_path(i8* %2, i8* %3)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %38
18: ; preds = %13, %10
%19 = call i8* @getenv(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %36, label %21
21: ; preds = %18
%22 = call i32 @snprintf(i8* %2, i32 4096, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %19)
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 4095
store i8 0, i8* %23
%24 = call i8* @strtok(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %36, label %26
26: ; preds = %21, %33
%27 = phi i8* [ %34, %33 ], [ %24, %21 ]
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
%30 = select i1 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i8* %27
%31 = call i8* @terminfo_try_path(i8* %30, i8* %3)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %26
36: ; preds = %33, %21, %18
%37 = call i8* @terminfo_try_path(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i8* %3)
br label %38
38: ; preds = %26, %8, %36, %13, %0
%39 = phi i8* [ null, %0 ], [ %9, %8 ], [ %16, %13 ], [ %37, %36 ], [ %31, %26 ]
ret i8* %39
}