#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 fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int strlen(char *);
void copyWithInject(FILE *from, FILE *to, char *match, char *inject) {
  int c;
  int n = strlen(match), i = 0;
  while ((c = fgetc(from)) != EOF) {
    if (c == match[i]) {
      i++;
    } else {
      i = 0;
    }
    fputc(c, to);
    if (i == n) {
      char *injectPointer = inject;
      while (c = *(injectPointer++)) {
        fputc(c, to);
      }
      i = 0;
    }
  }
}


x86 assembly (clang, -O0):
<copyWithInject>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%eax
je     11fd <copyWithInject+0xcd>
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    119b <copyWithInject+0x6b>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a2 <copyWithInject+0x72>
movl   $0x0,-0x2c(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <fputc@plt>
mov    -0x2c(%rbp),%eax
cmp    -0x28(%rbp),%eax
jne    11f8 <copyWithInject+0xc8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11f1 <copyWithInject+0xc1>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <fputc@plt>
jmp    11c2 <copyWithInject+0x92>
movl   $0x0,-0x2c(%rbp)
jmp    115b <copyWithInject+0x2b>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyWithInject>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,(%rsp)
mov    %rdx,%r13
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1040 <fgetc@plt>
mov    0x2e83(%rip),%rbp
cmp    0x0(%rbp),%eax
jne    1179 <copyWithInject+0x49>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rcx
add    $0x1,%rcx
mov    %rcx,0x8(%rsp)
xor    %r14d,%r14d
mov    %r12,0x10(%rsp)
jmp    119d <copyWithInject+0x6d>
mov    %r12,%rdi
call   1040 <fgetc@plt>
cmp    0x0(%rbp),%eax
je     116a <copyWithInject+0x3a>
movslq %r14d,%r14
movsbl 0x0(%r13,%r14,1),%ecx
add    $0x1,%r14d
cmp    %ecx,%eax
mov    $0x0,%ecx
cmovne %ecx,%r14d
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
cmp    %r15d,%r14d
jne    1190 <copyWithInject+0x60>
mov    (%rsp),%rax
mov    (%rax),%al
xor    %r14d,%r14d
test   %al,%al
je     1190 <copyWithInject+0x60>
mov    %r15d,%r12d
mov    %r13,%r15
mov    0x8(%rsp),%r13
nopl   0x0(%rax)
movsbl %al,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
movzbl 0x0(%r13),%eax
add    $0x1,%r13
test   %al,%al
jne    11e0 <copyWithInject+0xb0>
mov    %r15,%r13
mov    %r12d,%r15d
mov    0x10(%rsp),%r12
jmp    1190 <copyWithInject+0x60>

LLVM IR (-O0):
@EOF = global i32 0
define void @copyWithInject(i32* %0, i32* %1, i8* %2, i8* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%13 = load i8*, i8** %7
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %10
store i32 0, i32* %11
br label %15
15: ; preds = %53, %4
%16 = load i32*, i32** %5
%17 = call i32 @fgetc(i32* %16)
store i32 %17, i32* %9
%18 = load i32, i32* @EOF
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %54
20: ; preds = %15
%21 = load i32, i32* %9
%22 = load i8*, i8** %7
%23 = load i32, i32* %11
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %21, %27
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i32, i32* %11
%31 = add nsw i32 %30, 1
store i32 %31, i32* %11
br label %33
32: ; preds = %20
store i32 0, i32* %11
br label %33
33: ; preds = %32, %29
%34 = load i32, i32* %9
%35 = load i32*, i32** %6
%36 = call i32 @fputc(i32 %34, i32* %35)
%37 = load i32, i32* %11
%38 = load i32, i32* %10
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %53
40: ; preds = %33
%41 = load i8*, i8** %8
store i8* %41, i8** %12
br label %42
42: ; preds = %48, %40
%43 = load i8*, i8** %12
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %12
%45 = load i8, i8* %43
%46 = sext i8 %45 to i32
store i32 %46, i32* %9
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = load i32, i32* %9
%50 = load i32*, i32** %6
%51 = call i32 @fputc(i32 %49, i32* %50)
br label %42
52: ; preds = %42
store i32 0, i32* %11
br label %53
53: ; preds = %52, %33
br label %15
54: ; preds = %15
ret void
}

LLVM IR (-O2):
@EOF = global i32 0
define void @copyWithInject(i32* nocapture %0, i32* nocapture %1, i8* %2, i8* nocapture %3) {
%5 = tail call i32 @strlen(i8* %2)
%6 = tail call i32 @fgetc(i32* %0)
%7 = load i32, i32* @EOF
%8 = icmp eq i32 %6, %7
br i1 %8, label %37, label %9
9: ; preds = %4, %32
%10 = phi i32 [ %34, %32 ], [ %6, %4 ]
%11 = phi i32 [ %33, %32 ], [ 0, %4 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %2, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %10, %15
%17 = add nsw i32 %11, 1
%18 = select i1 %16, i32 %17, i32 0
%19 = tail call i32 @fputc(i32 %10, i32* %1)
%20 = icmp eq i32 %18, %5
br i1 %20, label %21, label %32
21: ; preds = %9
%22 = load i8, i8* %3
%23 = icmp eq i8 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %21, %24
%25 = phi i8 [ %30, %24 ], [ %22, %21 ]
%26 = phi i8* [ %28, %24 ], [ %3, %21 ]
%27 = sext i8 %25 to i32
%28 = getelementptr inbounds i8, i8* %26, i64 1
%29 = tail call i32 @fputc(i32 %27, i32* %1)
%30 = load i8, i8* %28
%31 = icmp eq i8 %30, 0
br i1 %31, label %32, label %24
32: ; preds = %24, %21, %9
%33 = phi i32 [ %18, %9 ], [ 0, %21 ], [ 0, %24 ]
%34 = tail call i32 @fgetc(i32* %0)
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %9
37: ; preds = %32, %4
ret void
}