#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*/ STDERR_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, ...);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int write(int /*<<< orphan*/, char *, int);
void cat(char *file) {
  register int fd, nr, nw;
  char buf[1024];
  if ((fd = open(file, O_RDONLY, 0)) < 0)
    err(1, "%s", file);
  while ((nr = read(fd, buf, sizeof(buf))) > 0)
    if ((nw = write(STDERR_FILENO, buf, nr)) == -1)
      err(1, "write to stderr");
  if (nr != 0)
    err(1, "%s", file);
  (void)close(fd);
}


x86 assembly (clang, -O0):
<cat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1196 <cat+0x46>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe71(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
jmp    119b <cat+0x4b>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    11f7 <cat+0xa7>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11f2 <cat+0xa2>
mov    $0x1,%edi
lea    0xe18(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
jmp    119b <cat+0x4b>
cmpl   $0x0,-0x10(%rbp)
je     1218 <cat+0xc8>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cat>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%r14
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    118e <cat+0x3e>
lea    0xe81(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11f7 <cat+0xa7>
mov    0x2e30(%rip),%r12
mov    %rsp,%rbx
lea    0xe51(%rip),%r15
jmp    11d3 <cat+0x83>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    11f7 <cat+0xa7>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11c0 <cat+0x70>
mov    $0x1,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <err@plt>
jmp    11c0 <cat+0x70>
test   %eax,%eax
je     1211 <cat+0xc1>
lea    0xdfe(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    %ebp,%edi
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%s\00"
@STDERR_FILENO = global i32 0
@.str.1 = [16 x i8] c"write to stderr\00"
define void @cat(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8, i32 0)
store i32 %9, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %2
%13 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %1
br label %15
15: ; preds = %28, %14
%16 = load i32, i32* %3
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 1024)
store i32 %18, i32* %4
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %15
%21 = load i32, i32* @STDERR_FILENO
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%23 = load i32, i32* %4
%24 = call i32 @write(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %5
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %20
br label %15
29: ; preds = %15
%30 = load i32, i32* %4
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %2
%34 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %29
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [3 x i8] c"%s\00"
@STDERR_FILENO = global i32 0
@.str.1 = [16 x i8] c"write to stderr\00"
define void @cat(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %9
9: ; preds = %7, %1
%10 = call i32 @read(i32 %5, i8* %3, i32 1024)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9, %19
%13 = phi i32 [ %20, %19 ], [ %10, %9 ]
%14 = load i32, i32* @STDERR_FILENO
%15 = call i32 @write(i32 %14, i8* %3, i32 %13)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = call i32 @read(i32 %5, i8* %3, i32 1024)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %12, label %22
22: ; preds = %19, %9
%23 = phi i32 [ %10, %9 ], [ %20, %19 ]
%24 = icmp eq i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %22
%26 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %27
27: ; preds = %25, %22
%28 = tail call i32 @close(i32 %5)
ret void
}