#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_read_error_msg;
typedef int /*<<< orphan*/ file_open_error_msg;
typedef int /*<<< orphan*/ arg_error_msg;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int n, f1;
  char arg_error_msg[] = "cat: arg error\n";
  char file_open_error_msg[] = "cat: can not open file\n";
  char file_read_error_msg[] = "cat: read file error\n";
  char buf[100];
  if (argc < 1) {
    write(2, arg_error_msg, sizeof(arg_error_msg));
    exit(1);
  }
  if ((f1 = open(argv[1], O_RDONLY, 0)) == -1) {
    write(2, file_open_error_msg, sizeof(file_open_error_msg));
    exit(2);
  }
  while ((n = read(f1, buf, 100)) > 0) {
    if (write(1, buf, n) != n) {
      write(2, file_read_error_msg, sizeof(file_read_error_msg));
      exit(3);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xea0(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe9d(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe9a(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe97(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe94(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe99(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0xe96(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    0xe94(%rip),%eax
mov    %eax,-0x60(%rbp)
mov    0xe8e(%rip),%ax
mov    %ax,-0x5c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jge    11e1 <main+0xa1>
lea    -0x30(%rbp),%rsi
mov    $0x2,%edi
mov    $0x10,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1222 <main+0xe2>
lea    -0x50(%rbp),%rsi
mov    $0x2,%edi
mov    $0x18,%edx
call   1030 <write@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
jmp    1227 <main+0xe7>
mov    -0x18(%rbp),%edi
lea    -0xe0(%rbp),%rsi
mov    $0x64,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1286 <main+0x146>
lea    -0xe0(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x14(%rbp),%eax
je     1281 <main+0x141>
lea    -0x70(%rbp),%rsi
mov    $0x2,%edi
mov    $0x16,%edx
call   1030 <write@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
jmp    1227 <main+0xe7>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xc0,%rsp
movaps 0xeae(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xeb2(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movabs $0xa656c6966206e,%rax
mov    %rax,0x30(%rsp)
movaps 0xeb7(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0xa726f72726520,%rax
mov    %rax,0xe(%rsp)
test   %edi,%edi
jg     11ae <main+0x6e>
lea    0x40(%rsp),%rsi
mov    $0x2,%edi
mov    $0x10,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    0x8(%rsi),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
jne    11e5 <main+0xa5>
lea    0x20(%rsp),%rsi
mov    $0x2,%edi
mov    $0x18,%edx
call   1030 <write@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    %eax,%r14d
lea    0x50(%rsp),%rbx
nopl   (%rax)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1235 <main+0xf5>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
je     11f0 <main+0xb0>
mov    %rsp,%rsi
mov    $0x2,%edi
mov    $0x16,%edx
call   1030 <write@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@__const.main.arg_error_msg = [16 x i8] c"cat: arg error\0A\00"
@__const.main.file_open_error_msg = [24 x i8] c"cat: can not open file\0A\00"
@__const.main.file_read_error_msg = [22 x i8] c"cat: read file error\0A\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [16 x i8]
%9 = alloca [24 x i8]
%10 = alloca [22 x i8]
%11 = alloca [100 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [16 x i8]* %8 to i8*
%13 = bitcast [24 x i8]* %9 to i8*
%14 = bitcast [22 x i8]* %10 to i8*
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%19 = call i32 @write(i32 2, i8* %18, i32 16)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25, i32 0)
store i32 %26, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = getelementptr inbounds [24 x i8], [24 x i8]* %9, i64 0, i64 0
%30 = call i32 @write(i32 2, i8* %29, i32 24)
%31 = call i32 @exit(i32 2)
unreachable
32: ; preds = %21
br label %33
33: ; preds = %48, %32
%34 = load i32, i32* %7
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 100)
store i32 %36, i32* %6
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %33
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %11, i64 0, i64 0
%40 = load i32, i32* %6
%41 = call i32 @write(i32 1, i8* %39, i32 %40)
%42 = load i32, i32* %6
%43 = icmp ne i32 %41, %42
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = getelementptr inbounds [22 x i8], [22 x i8]* %10, i64 0, i64 0
%46 = call i32 @write(i32 2, i8* %45, i32 22)
%47 = call i32 @exit(i32 3)
unreachable
48: ; preds = %38
br label %33
49: ; preds = %33
%50 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@__const.main.arg_error_msg = [16 x i8] c"cat: arg error\0A\00"
@__const.main.file_open_error_msg = [24 x i8] c"cat: can not open file\0A\00"
@__const.main.file_read_error_msg = [22 x i8] c"cat: read file error\0A\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = alloca [24 x i8]
%5 = alloca [22 x i8]
%6 = alloca [100 x i8]
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [24 x i8], [24 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [22 x i8], [22 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%11 = icmp slt i32 %0, 1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @write(i32 2, i8* %7, i32 16)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @write(i32 2, i8* %8, i32 24)
%23 = tail call i32 @exit(i32 2)
unreachable
24: ; preds = %15, %27
%25 = call i32 @read(i32 %19, i8* %10, i32 100)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = call i32 @write(i32 1, i8* %10, i32 %25)
%29 = icmp eq i32 %28, %25
br i1 %29, label %24, label %30
30: ; preds = %27
%31 = call i32 @write(i32 2, i8* %9, i32 22)
%32 = tail call i32 @exit(i32 3)
unreachable
33: ; preds = %24
%34 = tail call i32 @exit(i32 0)
unreachable
}