#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 */
struct termios {
  int c_lflag;
  int *c_cc;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ECHO;
int EOF;
int ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ *stdin;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: cat filename [filename ...]\n");
    return 1;
  }
  for (int i = 1; i < argc; i++) {
    FILE *fp;
    fp = fopen(argv[i], "r");
    if (fp == NULL) {
      printf("file not readable\n");
      return 1;
    }
    struct termios current_settings, hacker_settings;
    tcgetattr(0, &current_settings);
    hacker_settings = current_settings;
    hacker_settings.c_lflag &= ~(ECHO | ICANON);
    hacker_settings.c_cc[VTIME] = 0;
    hacker_settings.c_cc[VMIN] = 1;
    tcsetattr(0, TCSANOW, &hacker_settings);
    for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) {
      fgetc(stdin);
      putchar(c);
    }
    tcsetattr(0, TCSANOW, &current_settings);
    fclose(fp);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a8 <main+0x38>
lea    0xe69(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e6 <main+0x176>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12df <main+0x16f>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11fa <main+0x8a>
lea    0xe3d(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e6 <main+0x176>
xor    %edi,%edi
lea    -0x30(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%eax
mov    0x2d8b(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x40(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    0x2d7d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x38(%rbp),%rax
mov    0x2d78(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x40(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%eax
je     12b4 <main+0x144>
mov    0x2d55(%rip),%rax
mov    (%rax),%rdi
call   1060 <fgetc@plt>
mov    -0x44(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x44(%rbp)
jmp    127a <main+0x10a>
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11af <main+0x3f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jge    118f <main+0x1f>
lea    0xe8a(%rip),%rdi
jmp    12aa <main+0x13a>
mov    %rsi,%r13
mov    %edi,%eax
mov    %rax,0x10(%rsp)
mov    $0x1,%r12d
mov    0x2e2a(%rip),%r15
mov    0x2e3b(%rip),%r14
jmp    11de <main+0x6e>
nop
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
xor    %edi,%edi
lea    0x18(%rsp),%rdx
call   1080 <tcsetattr@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x1,%r12
cmp    0x10(%rsp),%r12
je     12b4 <main+0x144>
mov    0x0(%r13,%r12,8),%rdi
lea    0xe16(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12a3 <main+0x133>
mov    %rax,%rbx
xor    %edi,%edi
lea    0x18(%rsp),%rsi
call   1070 <tcgetattr@plt>
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    0x2db8(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %eax,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2d85(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d84(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    %rsp,%rdx
call   1080 <tcsetattr@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r15),%eax
je     11b0 <main+0x40>
mov    %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r14),%rdi
call   1060 <fgetc@plt>
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebp
cmp    (%r15),%eax
jne    1280 <main+0x110>
jmp    11b0 <main+0x40>
lea    0xd58(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@.str = [36 x i8] c"Usage: cat filename [filename ...]\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"file not readable\0A\00"
@ECHO = global i32 0
@ICANON = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@EOF = global i32 0
@stdin = global i32* null
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca %struct.termios
%9 = alloca %struct.termios
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %75
15: ; preds = %2
store i32 1, i32* %6
br label %16
16: ; preds = %71, %15
%17 = load i32, i32* %6
%18 = load i32, i32* %4
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %74
20: ; preds = %16
%21 = load i8**, i8*** %5
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
%25 = load i8*, i8** %24
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %7
%27 = load i32*, i32** %7
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %75
31: ; preds = %20
%32 = call i32 @tcgetattr(i32 0, %struct.termios* %8)
%33 = bitcast %struct.termios* %9 to i8*
%34 = bitcast %struct.termios* %8 to i8*
%35 = load i32, i32* @ECHO
%36 = load i32, i32* @ICANON
%37 = or i32 %35, %36
%38 = xor i32 %37, -1
%39 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%40 = load i32, i32* %39
%41 = and i32 %40, %38
store i32 %41, i32* %39
%42 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 1
%43 = load i32*, i32** %42
%44 = load i64, i64* @VTIME
%45 = getelementptr inbounds i32, i32* %43, i64 %44
store i32 0, i32* %45
%46 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 1
%47 = load i32*, i32** %46
%48 = load i64, i64* @VMIN
%49 = getelementptr inbounds i32, i32* %47, i64 %48
store i32 1, i32* %49
%50 = load i32, i32* @TCSANOW
%51 = call i32 @tcsetattr(i32 0, i32 %50, %struct.termios* %9)
%52 = load i32*, i32** %7
%53 = call i32 @fgetc(i32* %52)
store i32 %53, i32* %10
br label %54
54: ; preds = %63, %31
%55 = load i32, i32* %10
%56 = load i32, i32* @EOF
%57 = icmp ne i32 %55, %56
br i1 %57, label %58, label %66
58: ; preds = %54
%59 = load i32*, i32** @stdin
%60 = call i32 @fgetc(i32* %59)
%61 = load i32, i32* %10
%62 = call i32 @putchar(i32 %61)
br label %63
63: ; preds = %58
%64 = load i32*, i32** %7
%65 = call i32 @fgetc(i32* %64)
store i32 %65, i32* %10
br label %54
66: ; preds = %54
%67 = load i32, i32* @TCSANOW
%68 = call i32 @tcsetattr(i32 0, i32 %67, %struct.termios* %8)
%69 = load i32*, i32** %7
%70 = call i32 @fclose(i32* %69)
br label %71
71: ; preds = %66
%72 = load i32, i32* %6
%73 = add nsw i32 %72, 1
store i32 %73, i32* %6
br label %16
74: ; preds = %16
store i32 0, i32* %3
br label %75
75: ; preds = %74, %29, %13
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@.str.1 = [2 x i8] c"r\00"
@ECHO = global i32 0
@ICANON = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@EOF = global i32 0
@stdin = global i32* null
@str = [18 x i8] c"file not readable\00"
@str.3 = [35 x i8] c"Usage: cat filename [filename ...]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = icmp slt i32 %0, 2
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = bitcast %struct.termios* %3 to i8*
%8 = bitcast %struct.termios* %4 to i8*
%9 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%10 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%11 = zext i32 %0 to i64
br label %20
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.3, i64 0, i64 0))
br label %54
14: ; preds = %46, %28
%15 = load i32, i32* @TCSANOW
%16 = call i32 @tcsetattr(i32 0, i32 %15, %struct.termios* %3)
%17 = call i32 @fclose(i32* %24)
%18 = add nuw nsw i64 %21, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %54, label %20
20: ; preds = %6, %14
%21 = phi i64 [ 1, %6 ], [ %18, %14 ]
%22 = getelementptr inbounds i8*, i8** %1, i64 %21
%23 = load i8*, i8** %22
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %54
28: ; preds = %20
%29 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%30 = load i32, i32* @ECHO
%31 = load i32, i32* @ICANON
%32 = or i32 %31, %30
%33 = xor i32 %32, -1
%34 = load i32, i32* %9
%35 = and i32 %34, %33
store i32 %35, i32* %9
%36 = load i32*, i32** %10
%37 = load i64, i64* @VTIME
%38 = getelementptr inbounds i32, i32* %36, i64 %37
store i32 0, i32* %38
%39 = load i64, i64* @VMIN
%40 = getelementptr inbounds i32, i32* %36, i64 %39
store i32 1, i32* %40
%41 = load i32, i32* @TCSANOW
%42 = call i32 @tcsetattr(i32 0, i32 %41, %struct.termios* %4)
%43 = call i32 @fgetc(i32* %24)
%44 = load i32, i32* @EOF
%45 = icmp eq i32 %43, %44
br i1 %45, label %14, label %46
46: ; preds = %28, %46
%47 = phi i32 [ %51, %46 ], [ %43, %28 ]
%48 = load i32*, i32** @stdin
%49 = call i32 @fgetc(i32* %48)
%50 = call i32 @putchar(i32 %47)
%51 = call i32 @fgetc(i32* %24)
%52 = load i32, i32* @EOF
%53 = icmp eq i32 %51, %52
br i1 %53, label %14, label %46
54: ; preds = %14, %26, %12
%55 = phi i32 [ 1, %12 ], [ 1, %26 ], [ 0, %14 ]
ret i32 %55
}