#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *getenv(char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ tc_gtdata(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ tc_tname(int /*<<< orphan*/ *, char *, char *);
void get_term_info() {
  FILE *fp;
  char *term, *tcf;
  char buf[256];
  char *tc_file = "/etc/termcap";
  tcf = getenv("TERMCAP");
  if (tcf) {
    if (strlen(tcf) > 40) {
      fprintf(stderr, "TERMCAP file name too long\n");
      exit(1);
    }
    tc_file = tcf;
  }
  term = getenv("TERM");
  if (!term) {
    fprintf(stderr, "Cannot find TERM variable in environ\n");
    exit(1);
  }
  fp = fopen(tc_file, "r");
  if (!fp) {
    fprintf(stderr, "Cannot open TERMCAP file: %s\n", tc_file);
    exit(1);
  }
  if (!tc_tname(fp, term, buf)) {
    fprintf(stderr, "Cannot find TERM type: %s in TERMCAP file: %s", term,
            tc_file);
    exit(1);
  }
  tc_gtdata(fp, buf);
  fclose(fp);
}


x86 assembly (clang, -O0):
<get_term_info>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
lea    0xe6e(%rip),%rax
mov    %rax,-0x128(%rbp)
lea    0xe6d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f2 <get_term_info+0x72>
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x28,%eax
jle    11e7 <get_term_info+0x67>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x128(%rbp)
lea    0xe38(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    122e <get_term_info+0xae>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x128(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1278 <get_term_info+0xf8>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    -0x128(%rbp),%rdx
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x120(%rbp),%rdx
call   1070 <tc_tname@plt>
cmp    $0x0,%eax
jne    12c1 <get_term_info+0x141>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x128(%rbp),%rcx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    -0x120(%rbp),%rsi
call   1040 <tc_gtdata@plt>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_term_info>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
lea    0xe7a(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11bf <get_term_info+0x3f>
mov    %rax,%r14
mov    %rax,%rdi
call   1060 <strlen@plt>
cmp    $0x29,%eax
jl     11c6 <get_term_info+0x46>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
jmp    1233 <get_term_info+0xb3>
lea    0xe3a(%rip),%r14
lea    0xe64(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1223 <get_term_info+0xa3>
mov    %rax,%r15
lea    0xe7b(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1244 <get_term_info+0xc4>
mov    %rax,%rbx
mov    %rsp,%rdx
mov    %rax,%rdi
mov    %r15,%rsi
call   1070 <tc_tname@plt>
test   %eax,%eax
je     1268 <get_term_info+0xe8>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <tc_gtdata@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"/etc/termcap\00"
@.str.1 = [8 x i8] c"TERMCAP\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"TERMCAP file name too long\0A\00"
@.str.3 = [5 x i8] c"TERM\00"
@.str.4 = [38 x i8] c"Cannot find TERM variable in environ\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [30 x i8] c"Cannot open TERMCAP file: %s\0A\00"
@.str.7 = [46 x i8] c"Cannot find TERM type: %s in TERMCAP file: %s\00"
define void @get_term_info() {
%1 = alloca i32*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [256 x i8]
%5 = alloca i8*
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %6, i8** %3
%7 = load i8*, i8** %3
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %19
9: ; preds = %0
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = icmp sgt i32 %11, 40
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %9
%18 = load i8*, i8** %3
store i8* %18, i8** %5
br label %19
19: ; preds = %17, %0
%20 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i8* %20, i8** %2
%21 = load i8*, i8** %2
%22 = icmp ne i8* %21, null
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = load i8*, i8** %5
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %29, i32** %1
%30 = load i32*, i32** %1
%31 = icmp ne i32* %30, null
br i1 %31, label %37, label %32
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %5
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
%38 = load i32*, i32** %1
%39 = load i8*, i8** %2
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%41 = call i32 @tc_tname(i32* %38, i8* %39, i8* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %49, label %43
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %2
%46 = load i8*, i8** %5
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.7, i64 0, i64 0), i8* %45, i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %37
%50 = load i32*, i32** %1
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%52 = call i32 @tc_gtdata(i32* %50, i8* %51)
%53 = load i32*, i32** %1
%54 = call i32 @fclose(i32* %53)
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/etc/termcap\00"
@.str.1 = [8 x i8] c"TERMCAP\00"
@stderr = global i32 0
@.str.2 = [28 x i8] c"TERMCAP file name too long\0A\00"
@.str.3 = [5 x i8] c"TERM\00"
@.str.4 = [38 x i8] c"Cannot find TERM variable in environ\0A\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [30 x i8] c"Cannot open TERMCAP file: %s\0A\00"
@.str.7 = [46 x i8] c"Cannot find TERM type: %s in TERMCAP file: %s\00"
define void @get_term_info() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %12, label %5
5: ; preds = %0
%6 = tail call i32 @strlen(i8* %3)
%7 = icmp sgt i32 %6, 40
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %5, %0
%13 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), %0 ], [ %3, %5 ]
%14 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %12
%21 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %13)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = call i32 @tc_tname(i32* %21, i8* %14, i8* %2)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.7, i64 0, i64 0), i8* %14, i8* %13)
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = call i32 @tc_gtdata(i32* %21, i8* %2)
%36 = call i32 @fclose(i32* %21)
ret void
}