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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
scalar_t__ write(int /*<<< orphan*/, char *, scalar_t__);
void cat(char *filename) {
  int fd;
  int off;
  char buf[BUFSIZ];
  ssize_t nread;
  ssize_t nwritten;
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "cat: cannot open %s\n", filename);
    return;
  }
  while ((nread = read(fd, buf, BUFSIZ)) > 0) {
    off = 0;
    do {
      nwritten = write(fileno(stdout), &buf[off], nread);
      if (nwritten < 0) {
        fprintf(stderr, "cat: write error\n");
        exit(errno);
      }
      off += nwritten;
    } while ((nread -= nwritten) > 0);
  }
  if (nread < 0)
    fprintf(stderr, "cat: cannot read %s\n", filename);
  close(fd);
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ee <cat+0x7e>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x34(%rbp)
jmp    12d6 <cat+0x166>
jmp    11f3 <cat+0x83>
mov    -0x40(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jle    12a1 <cat+0x131>
movl   $0x0,-0x10(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jge    1275 <cat+0x105>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rcx
movslq -0x10(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jg     121d <cat+0xad>
jmp    11f3 <cat+0x83>
cmpq   $0x0,-0x28(%rbp)
jge    12c7 <cat+0x157>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xd67(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e48(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1262 <cat+0xf2>
mov    %eax,%r15d
mov    %rbx,-0x30(%rbp)
mov    (%r14),%edx
mov    %eax,%edi
mov    %r12,%rsi
call   1050 <read@plt>
mov    %rax,%rbx
test   %rax,%rax
jle    122c <cat+0xbc>
xor    %r13d,%r13d
mov    0x2de8(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edi
call   1070 <fileno@plt>
movslq %r13d,%r13
lea    (%r12,%r13,1),%rsi
mov    %eax,%edi
mov    %rbx,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     128a <cat+0x11a>
add    %rax,%r13
sub    %rax,%rbx
jg     11e0 <cat+0x70>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r12,%rsi
call   1050 <read@plt>
mov    %rax,%rbx
mov    $0x0,%r13d
test   %rax,%rax
jg     11e0 <cat+0x70>
test   %rbx,%rbx
jns    124c <cat+0xdc>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    -0x30(%rbp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %r15d,%edi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <fprintf@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xd7b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"cat: cannot open %s\0A\00"
@stdout = global i32 0
@.str.1 = [18 x i8] c"cat: write error\0A\00"
@errno = global i32 0
@.str.2 = [21 x i8] c"cat: cannot read %s\0A\00"
define void @cat(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i32, i32* @BUFSIZ
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
%14 = load i8*, i8** %2
%15 = load i32, i32* @O_RDONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %2
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %9
br label %67
23: ; preds = %1
br label %24
24: ; preds = %56, %23
%25 = load i32, i32* %3
%26 = load i32, i32* @BUFSIZ
%27 = call i64 @read(i32 %25, i8* %13, i32 %26)
store i64 %27, i64* %7
%28 = icmp sgt i64 %27, 0
br i1 %28, label %29, label %57
29: ; preds = %24
store i32 0, i32* %4
br label %30
30: ; preds = %51, %29
%31 = load i32, i32* @stdout
%32 = call i32 @fileno(i32 %31)
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %13, i64 %34
%36 = load i64, i64* %7
%37 = call i64 @write(i32 %32, i8* %35, i64 %36)
store i64 %37, i64* %8
%38 = load i64, i64* %8
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %30
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* @errno
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %30
%46 = load i64, i64* %8
%47 = load i32, i32* %4
%48 = sext i32 %47 to i64
%49 = add nsw i64 %48, %46
%50 = trunc i64 %49 to i32
store i32 %50, i32* %4
br label %51
51: ; preds = %45
%52 = load i64, i64* %8
%53 = load i64, i64* %7
%54 = sub nsw i64 %53, %52
store i64 %54, i64* %7
%55 = icmp sgt i64 %54, 0
br i1 %55, label %30, label %56
56: ; preds = %51
br label %24
57: ; preds = %24
%58 = load i64, i64* %7
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %2
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %60, %57
%65 = load i32, i32* %3
%66 = call i32 @close(i32 %65)
store i32 0, i32* %9
br label %67
67: ; preds = %64, %19
%68 = load i8*, i8** %5
%69 = load i32, i32* %9
switch i32 %69, label %71 [
i32 0, label %70
i32 1, label %70
]
70: ; preds = %67, %67
ret void
71: ; preds = %67
unreachable
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"cat: cannot open %s\0A\00"
@stdout = global i32 0
@.str.1 = [18 x i8] c"cat: write error\0A\00"
@errno = global i32 0
@.str.2 = [21 x i8] c"cat: cannot read %s\0A\00"
define void @cat(i8* %0) {
%2 = load i32, i32* @BUFSIZ
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %1
%9 = load i32, i32* @BUFSIZ
%10 = call i64 @read(i32 %6, i8* %4, i32 %9)
%11 = icmp sgt i64 %10, 0
br i1 %11, label %19, label %41
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %49
15: ; preds = %34
%16 = load i32, i32* @BUFSIZ
%17 = call i64 @read(i32 %6, i8* %4, i32 %16)
%18 = icmp sgt i64 %17, 0
br i1 %18, label %38, label %41
19: ; preds = %8, %38
%20 = phi i64 [ %39, %38 ], [ %10, %8 ]
%21 = phi i64 [ %40, %38 ], [ 0, %8 ]
%22 = load i32, i32* @stdout
%23 = tail call i32 @fileno(i32 %22)
%24 = shl i64 %21, 32
%25 = ashr exact i64 %24, 32
%26 = getelementptr inbounds i8, i8* %4, i64 %25
%27 = call i64 @write(i32 %23, i8* %26, i64 %20)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @errno
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %19
%35 = add nsw i64 %27, %25
%36 = sub nsw i64 %20, %27
%37 = icmp sgt i64 %36, 0
br i1 %37, label %38, label %15
38: ; preds = %34, %15
%39 = phi i64 [ %36, %34 ], [ %17, %15 ]
%40 = phi i64 [ %35, %34 ], [ 0, %15 ]
br label %19
41: ; preds = %15, %8
%42 = phi i64 [ %10, %8 ], [ %17, %15 ]
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32, i32* @stderr
%46 = tail call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %47
47: ; preds = %44, %41
%48 = tail call i32 @close(i32 %6)
br label %49
49: ; preds = %47, %12
ret void
}