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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ appch(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ freopen(char *, char *, int /*<<< orphan*/ *);
int getch();
char *malloc(int const);
int /*<<< orphan*/ printf(char *, ...);
char *realloc(char *, int);
int /*<<< orphan*/ rmch(char *);
size_t strlen(char *);
int main(int argc, char *argv[]) {
  const int bufsize = 128;
  int cursize = 0;
  int curch;
  int incch;
  int index = 0;
  int line = 0;
  int linec = 0;
  int linelen = 0;
  size_t itt;
  char *buffer = malloc(bufsize);
  char escseq[3];
  cursize = bufsize;
  FILE *pfile;
  if (argv[1] == NULL) {
    printf("Error\n");
    exit(1);
  }
  pfile = fopen(argv[1], "r+");
  if (pfile != NULL) {
    while ((incch = fgetc(pfile)) != EOF) {
      buffer[index++] = (char)incch;
      if (index == cursize) {
        cursize = index + bufsize;
        buffer = realloc(buffer, cursize);
      }
      if (buffer[index] == '\n') {
        line++;
        linec = 0;
      } else {
        linec++;
      }
    }
    buffer[index] = '\0';
    printf("\e[1;1H\e[2J");
    printf("%s", buffer);
  } else {
    pfile = fopen(argv[1], "w+");
    printf("\e[1;1H\e[2J");
  }
  while (1) {
    switch (curch = getch()) {
    case 27:
      printf("\n[Wrote %zu bytes]\n", strlen(buffer));
      freopen(argv[1], "w", pfile);
      fprintf(pfile, "%s", buffer);
      buffer = NULL;
      fclose(pfile);
      exit(1);
    case 9:
      for (int i = 0; i <= 4; i++) {
        appch(buffer, ' ');
        linec++;
      }
    case 127:
      printf("\b \b");
      if (buffer[strlen(buffer) - 1] == '\n') {
        buffer[strlen(buffer) - 1] = '\0';
        printf("\033[1A");
        for (itt = strlen(buffer); itt > 0 && buffer[itt] != '\n'; itt--) {
          printf("\033[1C");
        }
      } else {
        rmch(buffer);
        linec--;
      }
    default:
      if (curch == 127 || curch == 9 || curch == '\0') {
        break;
      }
      appch(buffer, curch);
      linec++;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x80,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    $0x80,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x40(%rbp)
movl   $0x80,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    124c <main+0x7c>
lea    0xdc5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdac(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     1339 <main+0x169>
jmp    1274 <main+0xa4>
mov    -0x50(%rbp),%rdi
call   1080 <fgetc@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d61(%rip),%rcx
cmp    (%rcx),%eax
je     1308 <main+0x138>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x40(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    12d0 <main+0x100>
mov    -0x24(%rbp),%eax
add    $0x80,%eax
mov    %eax,-0x18(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   10c0 <realloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    12fa <main+0x12a>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
jmp    1303 <main+0x133>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1274 <main+0xa4>
mov    -0x40(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xcef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rsi
lea    0xce8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    135f <main+0x18f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xcd0(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x50(%rbp)
lea    0xcb2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1364 <main+0x194>
xor    %eax,%eax
call   10d0 <getch@plt>
mov    %eax,-0x1c(%rbp)
mov    %eax,%ecx
mov    %ecx,-0x58(%rbp)
sub    $0x9,%eax
je     1404 <main+0x234>
jmp    1381 <main+0x1b1>
mov    -0x58(%rbp),%eax
sub    $0x1b,%eax
je     13a3 <main+0x1d3>
jmp    1392 <main+0x1c2>
mov    -0x58(%rbp),%eax
sub    $0x7f,%eax
je     143f <main+0x26f>
jmp    1521 <main+0x351>
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rsi
lea    0xc65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x50(%rbp),%rdx
lea    0xc5f(%rip),%rsi
call   1070 <freopen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rdx
lea    0xc31(%rip),%rsi
call   1090 <fprintf@plt>
movq   $0x0,-0x40(%rbp)
mov    -0x50(%rbp),%rdi
call   1030 <fclose@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
movl   $0x0,-0x54(%rbp)
cmpl   $0x4,-0x54(%rbp)
jg     143a <main+0x26a>
mov    -0x40(%rbp),%rdi
mov    $0x20,%esi
call   1060 <appch@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    140b <main+0x23b>
jmp    143f <main+0x26f>
lea    0xbeb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    150a <main+0x33a>
mov    -0x40(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x68(%rbp),%rax
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
lea    0xb98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x38(%rbp)
mov    %al,-0x69(%rbp)
jbe    14d6 <main+0x306>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x69(%rbp)
mov    -0x69(%rbp),%al
test   $0x1,%al
jne    14e6 <main+0x316>
jmp    1505 <main+0x335>
lea    0xb4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    14b1 <main+0x2e1>
jmp    151c <main+0x34c>
mov    -0x40(%rbp),%rdi
call   10a0 <rmch@plt>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    1521 <main+0x351>
cmpl   $0x7f,-0x1c(%rbp)
je     153f <main+0x36f>
cmpl   $0x9,-0x1c(%rbp)
je     153f <main+0x36f>
cmpl   $0x0,-0x1c(%rbp)
jne    1544 <main+0x374>
jmp    1559 <main+0x389>
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1060 <appch@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1364 <main+0x194>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbp
mov    $0x80,%edi
call   10c0 <malloc@plt>
mov    0x8(%rbp),%rdi
test   %rdi,%rdi
jne    121a <main+0x3a>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    %rax,%rbx
lea    0xddc(%rip),%rsi
call   10f0 <fopen@plt>
test   %rax,%rax
mov    %rbp,0x10(%rsp)
jne    125b <main+0x7b>
mov    0x8(%rbp),%rdi
lea    0xdd3(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,0x8(%rsp)
lea    0xdb4(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    12e2 <main+0x102>
mov    %rax,%r13
mov    %rax,%rdi
call   10a0 <fgetc@plt>
mov    0x2d7b(%rip),%r14
cmp    (%r14),%eax
jne    1276 <main+0x96>
xor    %eax,%eax
jmp    12ba <main+0xda>
mov    $0x80,%r12d
xor    %ebp,%ebp
jmp    1290 <main+0xb0>
mov    %r13,%rdi
call   10a0 <fgetc@plt>
mov    %r15,%rbp
cmp    (%r14),%eax
je     12b7 <main+0xd7>
lea    0x1(%rbp),%r15
mov    %al,(%rbx,%rbp,1)
mov    %r12d,%eax
cmp    %rax,%r15
jne    1280 <main+0xa0>
add    $0x81,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   10d0 <realloc@plt>
mov    %rax,%rbx
mov    %ebp,%r12d
jmp    1280 <main+0xa0>
mov    %r15d,%eax
mov    %r13,0x8(%rsp)
movb   $0x0,(%rbx,%rax,1)
lea    0xd39(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd36(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xd41(%rip),%r12
lea    0xd3e(%rip),%r13
lea    0xd3c(%rip),%rbp
jmp    130e <main+0x12e>
cmp    $0x1b,%r14d
je     13fa <main+0x21a>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1080 <appch@plt>
xor    %eax,%eax
call   10e0 <getch@plt>
mov    %eax,%r14d
cmp    $0x1a,%eax
jg     1370 <main+0x190>
test   %r14d,%r14d
je     130e <main+0x12e>
cmp    $0x9,%r14d
jne    1303 <main+0x123>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1080 <appch@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1080 <appch@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1080 <appch@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1080 <appch@plt>
mov    %rbx,%rdi
mov    $0x20,%esi
call   1080 <appch@plt>
jmp    1376 <main+0x196>
nopl   0x0(%rax,%rax,1)
cmp    $0x7f,%r14d
jne    12f9 <main+0x119>
mov    %r12,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    13d0 <main+0x1f0>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %r13,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     13d8 <main+0x1f8>
mov    %rax,%r15
xchg   %ax,%ax
cmpb   $0xa,(%rbx,%r15,1)
je     13d8 <main+0x1f8>
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0xffffffffffffffff,%r15
jne    13b0 <main+0x1d0>
jmp    13d8 <main+0x1f8>
nopl   0x0(%rax)
mov    %rbx,%rdi
call   10b0 <rmch@plt>
test   %r14d,%r14d
je     130e <main+0x12e>
cmp    $0x9,%r14d
je     130e <main+0x12e>
cmp    $0x7f,%r14d
je     130e <main+0x12e>
jmp    1303 <main+0x123>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0xc0b(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%rdi
lea    0xc05(%rip),%rsi
mov    0x8(%rsp),%rbp
mov    %rbp,%rdx
call   1090 <freopen@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1070 <fputs@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"Error\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@EOF = global i32 0
@.str.2 = [11 x i8] c"\1B[1;1H\1B[2J\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [20 x i8] c"\0A[Wrote %zu bytes]\0A\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [4 x i8] c"\08 \08\00"
@.str.8 = [5 x i8] c"\1B[1A\00"
@.str.9 = [5 x i8] c"\1B[1C\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i8*
%16 = alloca [3 x i8]
%17 = alloca i32*
%18 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 128, i32* %6
store i32 0, i32* %7
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
%19 = call i8* @malloc(i32 128)
store i8* %19, i8** %15
store i32 128, i32* %7
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %2
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %17
%32 = load i32*, i32** %17
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %80
34: ; preds = %27
br label %35
35: ; preds = %71, %34
%36 = load i32*, i32** %17
%37 = call i32 @fgetc(i32* %36)
store i32 %37, i32* %9
%38 = load i32, i32* @EOF
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %72
40: ; preds = %35
%41 = load i32, i32* %9
%42 = trunc i32 %41 to i8
%43 = load i8*, i8** %15
%44 = load i32, i32* %10
%45 = add nsw i32 %44, 1
store i32 %45, i32* %10
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %43, i64 %46
store i8 %42, i8* %47
%48 = load i32, i32* %10
%49 = load i32, i32* %7
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %57
51: ; preds = %40
%52 = load i32, i32* %10
%53 = add nsw i32 %52, 128
store i32 %53, i32* %7
%54 = load i8*, i8** %15
%55 = load i32, i32* %7
%56 = call i8* @realloc(i8* %54, i32 %55)
store i8* %56, i8** %15
br label %57
57: ; preds = %51, %40
%58 = load i8*, i8** %15
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 10
br i1 %64, label %65, label %68
65: ; preds = %57
%66 = load i32, i32* %11
%67 = add nsw i32 %66, 1
store i32 %67, i32* %11
store i32 0, i32* %12
br label %71
68: ; preds = %57
%69 = load i32, i32* %12
%70 = add nsw i32 %69, 1
store i32 %70, i32* %12
br label %71
71: ; preds = %68, %65
br label %35
72: ; preds = %35
%73 = load i8*, i8** %15
%74 = load i32, i32* %10
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
store i8 0, i8* %76
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%78 = load i8*, i8** %15
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %78)
br label %86
80: ; preds = %27
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 1
%83 = load i8*, i8** %82
%84 = call i32* @fopen(i8* %83, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %84, i32** %17
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %86
86: ; preds = %80, %72
br label %87
87: ; preds = %86, %176
%88 = call i32 (...) @getch()
store i32 %88, i32* %8
switch i32 %88, label %160 [
i32 27, label %89
i32 9, label %104
i32 127, label %117
]
89: ; preds = %87
%90 = load i8*, i8** %15
%91 = call i64 @strlen(i8* %90)
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i64 %91)
%93 = load i8**, i8*** %5
%94 = getelementptr inbounds i8*, i8** %93, i64 1
%95 = load i8*, i8** %94
%96 = load i32*, i32** %17
%97 = call i32 @freopen(i8* %95, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32* %96)
%98 = load i32*, i32** %17
%99 = load i8*, i8** %15
%100 = call i32 @fprintf(i32* %98, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %99)
store i8* null, i8** %15
%101 = load i32*, i32** %17
%102 = call i32 @fclose(i32* %101)
%103 = call i32 @exit(i32 1)
unreachable
104: ; preds = %87
store i32 0, i32* %18
br label %105
105: ; preds = %113, %104
%106 = load i32, i32* %18
%107 = icmp sle i32 %106, 4
br i1 %107, label %108, label %116
108: ; preds = %105
%109 = load i8*, i8** %15
%110 = call i32 @appch(i8* %109, i32 32)
%111 = load i32, i32* %12
%112 = add nsw i32 %111, 1
store i32 %112, i32* %12
br label %113
113: ; preds = %108
%114 = load i32, i32* %18
%115 = add nsw i32 %114, 1
store i32 %115, i32* %18
br label %105
116: ; preds = %105
br label %117
117: ; preds = %87, %116
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%119 = load i8*, i8** %15
%120 = load i8*, i8** %15
%121 = call i64 @strlen(i8* %120)
%122 = sub i64 %121, 1
%123 = getelementptr inbounds i8, i8* %119, i64 %122
%124 = load i8, i8* %123
%125 = sext i8 %124 to i32
%126 = icmp eq i32 %125, 10
br i1 %126, label %127, label %154
127: ; preds = %117
%128 = load i8*, i8** %15
%129 = load i8*, i8** %15
%130 = call i64 @strlen(i8* %129)
%131 = sub i64 %130, 1
%132 = getelementptr inbounds i8, i8* %128, i64 %131
store i8 0, i8* %132
%133 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%134 = load i8*, i8** %15
%135 = call i64 @strlen(i8* %134)
store i64 %135, i64* %14
br label %136
136: ; preds = %150, %127
%137 = load i64, i64* %14
%138 = icmp ugt i64 %137, 0
br i1 %138, label %139, label %146
139: ; preds = %136
%140 = load i8*, i8** %15
%141 = load i64, i64* %14
%142 = getelementptr inbounds i8, i8* %140, i64 %141
%143 = load i8, i8* %142
%144 = sext i8 %143 to i32
%145 = icmp ne i32 %144, 10
br label %146
146: ; preds = %139, %136
%147 = phi i1 [ false, %136 ], [ %145, %139 ]
br i1 %147, label %148, label %153
148: ; preds = %146
%149 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %150
150: ; preds = %148
%151 = load i64, i64* %14
%152 = add i64 %151, -1
store i64 %152, i64* %14
br label %136
153: ; preds = %146
br label %159
154: ; preds = %117
%155 = load i8*, i8** %15
%156 = call i32 @rmch(i8* %155)
%157 = load i32, i32* %12
%158 = add nsw i32 %157, -1
store i32 %158, i32* %12
br label %159
159: ; preds = %154, %153
br label %160
160: ; preds = %87, %159
%161 = load i32, i32* %8
%162 = icmp eq i32 %161, 127
br i1 %162, label %169, label %163
163: ; preds = %160
%164 = load i32, i32* %8
%165 = icmp eq i32 %164, 9
br i1 %165, label %169, label %166
166: ; preds = %163
%167 = load i32, i32* %8
%168 = icmp eq i32 %167, 0
br i1 %168, label %169, label %170
169: ; preds = %166, %163, %160
br label %176
170: ; preds = %166
%171 = load i8*, i8** %15
%172 = load i32, i32* %8
%173 = call i32 @appch(i8* %171, i32 %172)
%174 = load i32, i32* %12
%175 = add nsw i32 %174, 1
store i32 %175, i32* %12
br label %176
176: ; preds = %170, %169
br label %87
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"r+\00"
@EOF = global i32 0
@.str.2 = [11 x i8] c"\1B[1;1H\1B[2J\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [3 x i8] c"w+\00"
@.str.5 = [20 x i8] c"\0A[Wrote %zu bytes]\0A\00"
@.str.6 = [2 x i8] c"w\00"
@.str.7 = [4 x i8] c"\08 \08\00"
@.str.8 = [5 x i8] c"\1B[1A\00"
@.str.9 = [5 x i8] c"\1B[1C\00"
@str = [6 x i8] c"Error\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call dereferenceable_or_null(128) i8* @malloc(i32 128)
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %45, label %13
13: ; preds = %10
%14 = tail call i32 @fgetc(i32* %11)
%15 = load i32, i32* @EOF
%16 = icmp eq i32 %14, %15
br i1 %16, label %39, label %17
17: ; preds = %13, %31
%18 = phi i64 [ %23, %31 ], [ 0, %13 ]
%19 = phi i32 [ %34, %31 ], [ %14, %13 ]
%20 = phi i32 [ %33, %31 ], [ 128, %13 ]
%21 = phi i8* [ %32, %31 ], [ %3, %13 ]
%22 = trunc i32 %19 to i8
%23 = add nuw i64 %18, 1
%24 = getelementptr inbounds i8, i8* %21, i64 %18
store i8 %22, i8* %24
%25 = zext i32 %20 to i64
%26 = icmp eq i64 %23, %25
br i1 %26, label %27, label %31
27: ; preds = %17
%28 = trunc i64 %18 to i32
%29 = add i32 %28, 129
%30 = tail call i8* @realloc(i8* %21, i32 %29)
br label %31
31: ; preds = %27, %17
%32 = phi i8* [ %30, %27 ], [ %21, %17 ]
%33 = phi i32 [ %29, %27 ], [ %20, %17 ]
%34 = tail call i32 @fgetc(i32* %11)
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %17
37: ; preds = %31
%38 = and i64 %23, 4294967295
br label %39
39: ; preds = %37, %13
%40 = phi i64 [ 0, %13 ], [ %38, %37 ]
%41 = phi i8* [ %3, %13 ], [ %32, %37 ]
%42 = getelementptr inbounds i8, i8* %41, i64 %40
store i8 0, i8* %42
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %41)
br label %49
45: ; preds = %10
%46 = load i8*, i8** %4
%47 = tail call i32* @fopen(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %49
49: ; preds = %45, %39
%50 = phi i8* [ %41, %39 ], [ %3, %45 ]
%51 = phi i32* [ %11, %39 ], [ %47, %45 ]
br label %52
52: ; preds = %54, %49
%53 = tail call i32 (...) @getch()
switch i32 %53, label %92 [
i32 27, label %61
i32 9, label %55
i32 127, label %69
i32 0, label %54
]
54: ; preds = %52, %91, %91, %91, %92
br label %52
55: ; preds = %52
%56 = tail call i32 @appch(i8* %50, i32 32)
%57 = tail call i32 @appch(i8* %50, i32 32)
%58 = tail call i32 @appch(i8* %50, i32 32)
%59 = tail call i32 @appch(i8* %50, i32 32)
%60 = tail call i32 @appch(i8* %50, i32 32)
br label %69
61: ; preds = %52
%62 = tail call i64 @strlen(i8* dereferenceable(1) %50)
%63 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i64 %62)
%64 = load i8*, i8** %4
%65 = tail call i32 @freopen(i8* %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0), i32* %51)
%66 = tail call i32 @fputs(i8* %50, i32* %51)
%67 = tail call i32 @fclose(i32* %51)
%68 = tail call i32 @exit(i32 1)
unreachable
69: ; preds = %52, %55
%70 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%71 = tail call i64 @strlen(i8* dereferenceable(1) %50)
%72 = add i64 %71, -1
%73 = getelementptr inbounds i8, i8* %50, i64 %72
%74 = load i8, i8* %73
%75 = icmp eq i8 %74, 10
br i1 %75, label %76, label %89
76: ; preds = %69
store i8 0, i8* %73
%77 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%78 = tail call i64 @strlen(i8* dereferenceable(1) %50)
%79 = icmp eq i64 %78, 0
br i1 %79, label %91, label %80
80: ; preds = %76, %85
%81 = phi i64 [ %87, %85 ], [ %78, %76 ]
%82 = getelementptr inbounds i8, i8* %50, i64 %81
%83 = load i8, i8* %82
%84 = icmp eq i8 %83, 10
br i1 %84, label %91, label %85
85: ; preds = %80
%86 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
%87 = add i64 %81, -1
%88 = icmp eq i64 %87, 0
br i1 %88, label %91, label %80
89: ; preds = %69
%90 = tail call i32 @rmch(i8* %50)
br label %91
91: ; preds = %80, %85, %76, %89
switch i32 %53, label %92 [
i32 127, label %54
i32 9, label %54
i32 0, label %54
]
92: ; preds = %52, %91
%93 = tail call i32 @appch(i8* %50, i32 %53)
br label %54
}