#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;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fileno(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int, char *, int);
int cat(char *file) {
  int fd_in;
  int fd_out;
  char buf[512];
  int count;
  fflush(stdout);
  if ((fd_in = open(file, O_RDONLY)) == -1)
    return (0);
  fd_out = fileno(stdout);
  while ((count = read(fd_in, buf, sizeof(buf))) > 0)
    write(fd_out, buf, count);
  close(fd_in);
  return (1);
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <cat+0x47>
movl   $0x0,-0x4(%rbp)
jmp    1204 <cat+0xa4>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %eax,-0x224(%rbp)
cmp    $0x0,%eax
jle    11f5 <cat+0x95>
mov    -0x18(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x224(%rbp),%edx
call   1030 <write@plt>
jmp    11b8 <cat+0x58>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x200,%rsp
mov    %rdi,%rbx
mov    0x2e63(%rip),%rbp
mov    0x0(%rbp),%edi
call   1070 <fflush@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11ee <cat+0x8e>
mov    %eax,%ebx
mov    0x0(%rbp),%edi
call   1060 <fileno@plt>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11e0 <cat+0x80>
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x200,%edx
call   1050 <read@plt>
test   %eax,%eax
jg     11c0 <cat+0x60>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%eax
jmp    11f0 <cat+0x90>
xor    %eax,%eax
add    $0x200,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@O_RDONLY = global i32 0
define i32 @cat(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [512 x i8]
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @stdout
%9 = call i32 @fflush(i32 %8)
%10 = load i8*, i8** %3
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %4
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 0, i32* %2
br label %31
15: ; preds = %1
%16 = load i32, i32* @stdout
%17 = call i32 @fileno(i32 %16)
store i32 %17, i32* %5
br label %18
18: ; preds = %23, %15
%19 = load i32, i32* %4
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 512)
store i32 %21, i32* %7
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* %5
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%26 = load i32, i32* %7
%27 = call i32 @write(i32 %24, i8* %25, i32 %26)
br label %18
28: ; preds = %18
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
store i32 1, i32* %2
br label %31
31: ; preds = %28, %14
%32 = load i32, i32* %2
ret i32 %32
}

LLVM IR (-O2):
@stdout = global i32 0
@O_RDONLY = global i32 0
define i32 @cat(i8* nocapture %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @stdout
%5 = tail call i32 @fflush(i32 %4)
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %21, label %9
9: ; preds = %1
%10 = load i32, i32* @stdout
%11 = tail call i32 @fileno(i32 %10)
%12 = call i32 @read(i32 %7, i8* %3, i32 512)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %9, %14
%15 = phi i32 [ %17, %14 ], [ %12, %9 ]
%16 = call i32 @write(i32 %11, i8* %3, i32 %15)
%17 = call i32 @read(i32 %7, i8* %3, i32 512)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %14, label %19
19: ; preds = %14, %9
%20 = tail call i32 @close(i32 %7)
br label %21
21: ; preds = %1, %19
%22 = phi i32 [ 1, %19 ], [ 0, %1 ]
ret i32 %22
}