#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 */
unsigned int EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
unsigned int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ replace_spec_chars(char *, int);
int replace_esc(char *in_file, char *out_file) {
  FILE *in;
  FILE *out;
  unsigned int c;
  char msg[1001];
  int msg_len;
  in = fopen(in_file, "r");
  out = fopen(out_file, "w+");
  msg[0] = '\0';
  c = fgetc(in);
  msg_len = 0;
  while (c != EOF) {
    while (c != '#') {
      msg[msg_len] = (unsigned char)c;
      msg_len++;
      msg[msg_len] = '\0';
      c = fgetc(in);
    }
    replace_spec_chars(msg, msg_len);
    fputs(msg, out);
    fputs("\n", out);
    fgetc(in);
    c = fgetc(in);
    msg_len = 0;
    msg[msg_len] = '\0';
  }
  fclose(in);
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<replace_esc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
movb   $0x0,-0x410(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x414(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e36(%rip),%rcx
cmp    (%rcx),%eax
je     1270 <replace_esc+0x120>
jmp    11bf <replace_esc+0x6f>
cmpl   $0x23,-0x24(%rbp)
je     120b <replace_esc+0xbb>
mov    -0x24(%rbp),%eax
mov    %al,%cl
movslq -0x414(%rbp),%rax
mov    %cl,-0x410(%rbp,%rax,1)
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
jmp    11bf <replace_esc+0x6f>
lea    -0x410(%rbp),%rdi
mov    -0x414(%rbp),%esi
call   1060 <replace_spec_chars@plt>
lea    -0x410(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x20(%rbp),%rsi
lea    0xdcd(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x414(%rbp)
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
jmp    11a8 <replace_esc+0x58>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_esc>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f0,%rsp
mov    %rsi,%rbx
lea    0xe86(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r12
lea    0xe79(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%r14
movb   $0x0,(%rsp)
mov    %r12,%rdi
call   1050 <fgetc@plt>
mov    0x2e41(%rip),%r13
cmp    0x0(%r13),%eax
jne    11d0 <replace_esc+0x70>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x3f0,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %rsp,%r15
jmp    121c <replace_esc+0xbc>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %ebx,%esi
call   1070 <replace_spec_chars@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <fputs@plt>
mov    $0xa,%edi
mov    %r14,%rsi
call   1060 <fputc@plt>
mov    %r12,%rdi
call   1050 <fgetc@plt>
mov    %r12,%rdi
call   1050 <fgetc@plt>
movb   $0x0,(%rsp)
cmp    0x0(%r13),%eax
je     11ad <replace_esc+0x4d>
mov    $0x0,%ebx
cmp    $0x23,%eax
je     11e0 <replace_esc+0x80>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %al,(%rsp,%rbx,1)
movb   $0x0,0x1(%rsp,%rbx,1)
add    $0x1,%rbx
mov    %r12,%rdi
call   1050 <fgetc@plt>
cmp    $0x23,%eax
jne    1230 <replace_esc+0xd0>
jmp    11e0 <replace_esc+0x80>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"w+\00"
@EOF = global i32 0
@.str.2 = [2 x i8] c"\0A\00"
define i32 @replace_esc(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [1001 x i8]
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = getelementptr inbounds [1001 x i8], [1001 x i8]* %8, i64 0, i64 0
store i8 0, i8* %14
%15 = load i32*, i32** %5
%16 = call i32 @fgetc(i32* %15)
store i32 %16, i32* %7
store i32 0, i32* %9
br label %17
17: ; preds = %38, %2
%18 = load i32, i32* %7
%19 = load i32, i32* @EOF
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %54
21: ; preds = %17
br label %22
22: ; preds = %25, %21
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 35
br i1 %24, label %25, label %38
25: ; preds = %22
%26 = load i32, i32* %7
%27 = trunc i32 %26 to i8
%28 = load i32, i32* %9
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1001 x i8], [1001 x i8]* %8, i64 0, i64 %29
store i8 %27, i8* %30
%31 = load i32, i32* %9
%32 = add nsw i32 %31, 1
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1001 x i8], [1001 x i8]* %8, i64 0, i64 %34
store i8 0, i8* %35
%36 = load i32*, i32** %5
%37 = call i32 @fgetc(i32* %36)
store i32 %37, i32* %7
br label %22
38: ; preds = %22
%39 = getelementptr inbounds [1001 x i8], [1001 x i8]* %8, i64 0, i64 0
%40 = load i32, i32* %9
%41 = call i32 @replace_spec_chars(i8* %39, i32 %40)
%42 = getelementptr inbounds [1001 x i8], [1001 x i8]* %8, i64 0, i64 0
%43 = load i32*, i32** %6
%44 = call i32 @fputs(i8* %42, i32* %43)
%45 = load i32*, i32** %6
%46 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32* %45)
%47 = load i32*, i32** %5
%48 = call i32 @fgetc(i32* %47)
%49 = load i32*, i32** %5
%50 = call i32 @fgetc(i32* %49)
store i32 %50, i32* %7
store i32 0, i32* %9
%51 = load i32, i32* %9
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [1001 x i8], [1001 x i8]* %8, i64 0, i64 %52
store i8 0, i8* %53
br label %17
54: ; preds = %17
%55 = load i32*, i32** %5
%56 = call i32 @fclose(i32* %55)
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"w+\00"
@EOF = global i32 0
define i32 @replace_esc(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca [1001 x i8]
%4 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8 0, i8* %4
%7 = tail call i32 @fgetc(i32* %5)
%8 = load i32, i32* @EOF
%9 = icmp eq i32 %7, %8
br i1 %9, label %33, label %10
10: ; preds = %2, %24
%11 = phi i32 [ %30, %24 ], [ %7, %2 ]
%12 = icmp eq i32 %11, 35
br i1 %12, label %24, label %13
13: ; preds = %10, %13
%14 = phi i64 [ %18, %13 ], [ 0, %10 ]
%15 = phi i32 [ %20, %13 ], [ %11, %10 ]
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 %14
store i8 %16, i8* %17
%18 = add nuw nsw i64 %14, 1
%19 = getelementptr inbounds [1001 x i8], [1001 x i8]* %3, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @fgetc(i32* %5)
%21 = icmp eq i32 %20, 35
br i1 %21, label %22, label %13
22: ; preds = %13
%23 = trunc i64 %18 to i32
br label %24
24: ; preds = %22, %10
%25 = phi i32 [ 0, %10 ], [ %23, %22 ]
%26 = call i32 @replace_spec_chars(i8* %4, i32 %25)
%27 = call i32 @fputs(i8* %4, i32* %6)
%28 = call i32 @fputc(i32 10, i32* %6)
%29 = call i32 @fgetc(i32* %5)
%30 = call i32 @fgetc(i32* %5)
store i8 0, i8* %4
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %33, label %10
33: ; preds = %24, %2
%34 = call i32 @fclose(i32* %5)
%35 = call i32 @fclose(i32* %6)
ret i32 0
}