#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *cat_file(char *name) {
  int i;
  FILE *fp;
  static char buf[512];
  buf[0] = '\0';
  fp = fopen(name, "r");
  if (fp == NULL)
    return buf;
  fgets(buf, sizeof(buf), fp);
  fclose(fp);
  i = 0;
  while (buf[i] != '\0' && buf[i] != '\r' && buf[i] != '\n')
    i++;
  buf[i] = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<cat_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movb   $0x0,0x2f0d(%rip)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1172 <cat_file+0x42>
lea    0x2ee7(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1221 <cat_file+0xf1>
mov    -0x20(%rbp),%rdx
lea    0x2ed3(%rip),%rdi
mov    $0x200,%esi
call   1040 <fgets@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x14(%rbp)
movslq -0x14(%rbp),%rcx
lea    0x2eae(%rip),%rax
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     11e9 <cat_file+0xb9>
movslq -0x14(%rbp),%rcx
lea    0x2e91(%rip),%rax
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0xd,%ecx
mov    %al,-0x21(%rbp)
je     11e9 <cat_file+0xb9>
movslq -0x14(%rbp),%rcx
lea    0x2e74(%rip),%rax
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11f9 <cat_file+0xc9>
jmp    1207 <cat_file+0xd7>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1197 <cat_file+0x67>
movslq -0x14(%rbp),%rcx
lea    0x2e3e(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e33(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat_file>:
push   %r14
push   %rbx
push   %rax
movb   $0x0,0x2f15(%rip)
lea    0xebe(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     119c <cat_file+0x6c>
mov    %rax,%rbx
lea    0x2efa(%rip),%r14
mov    %r14,%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    $0x2401,%ecx
jmp    1184 <cat_file+0x54>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rax
movzbl (%rax,%r14,1),%edx
cmp    $0xd,%rdx
ja     1180 <cat_file+0x50>
bt     %rdx,%rcx
jae    1180 <cat_file+0x50>
mov    %eax,%eax
movb   $0x0,(%rax,%r14,1)
lea    0x2ead(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@cat_file.buf = internal global [512 x i8] zeroinitializer
@.str = [2 x i8] c"r\00"
define i8* @cat_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
store i8* %0, i8** %3
store i8 0, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 0)
%6 = load i8*, i8** %3
%7 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %7, i32** %5
%8 = load i32*, i32** %5
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 0), i8** %2
br label %46
11: ; preds = %1
%12 = load i32*, i32** %5
%13 = call i32 @fgets(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 0), i32 512, i32* %12)
%14 = load i32*, i32** %5
%15 = call i32 @fclose(i32* %14)
store i32 0, i32* %4
br label %16
16: ; preds = %39, %11
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %16
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 13
br i1 %29, label %30, label %37
30: ; preds = %23
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 %32
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 10
br label %37
37: ; preds = %30, %23, %16
%38 = phi i1 [ false, %23 ], [ false, %16 ], [ %36, %30 ]
br i1 %38, label %39, label %42
39: ; preds = %37
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %16
42: ; preds = %37
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 %44
store i8 0, i8* %45
store i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 0), i8** %2
br label %46
46: ; preds = %42, %10
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@cat_file.buf = internal global [512 x i8] zeroinitializer
@.str = [2 x i8] c"r\00"
define i8* @cat_file(i8* nocapture %0) {
store i8 0, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 0)
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1
%5 = tail call i32 @fgets(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 0), i32 512, i32* %2)
%6 = tail call i32 @fclose(i32* %2)
br label %7
7: ; preds = %11, %4
%8 = phi i64 [ %12, %11 ], [ 0, %4 ]
%9 = getelementptr inbounds [512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 %8
%10 = load i8, i8* %9
switch i8 %10, label %11 [
i8 0, label %13
i8 13, label %13
i8 10, label %13
]
11: ; preds = %7
%12 = add nuw i64 %8, 1
br label %7
13: ; preds = %7, %7, %7
%14 = and i64 %8, 4294967295
%15 = getelementptr inbounds [512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %1, %13
ret i8* getelementptr inbounds ([512 x i8], [512 x i8]* @cat_file.buf, i64 0, i64 0)
}