#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*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ file_copy(char const *, char const *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ strncmp(char *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char const *);
int insert_into_file(const char *filename, const char *insertname,
                     const char *start_line, const char *end_line) {
  int ch;
  int is_found = 0;
  static char line[1024 * 4];
  const char *tmpname = "tmp.h";
  FILE *source_fp;
  FILE *dest_fp;
  FILE *insert_fp;
  if (file_copy(filename, tmpname) == 0)
    return 0;
  source_fp = fopen(tmpname, "r");
  dest_fp = fopen(filename, "w");
  insert_fp = fopen(insertname, "r");
  if (source_fp == NULL || dest_fp == NULL || insert_fp == NULL)
    return 0;
  for (;;) {
    if (fgets(line, 1024 * 4, source_fp) == NULL)
      break;
    if (strncmp(line, start_line, strlen(start_line)) == 0) {
      is_found = 1;
      fputs(line, dest_fp);
      while ((ch = fgetc(insert_fp)) != EOF)
        fputc(ch, dest_fp);
      fputs("\n", dest_fp);
      for (;;) {
        if (fgets(line, 1024 * 4, source_fp) == NULL)
          break;
        if (strncmp(line, end_line, strlen(end_line)) == 0) {
          fputs(line, dest_fp);
          break;
        }
      }
    } else {
      fputs(line, dest_fp);
    }
  }
  fclose(insert_fp);
  fclose(source_fp);
  fclose(dest_fp);
  unlink(tmpname);
  if (is_found)
    printf("patched %s\n", filename);
  return 1;
}


x86 assembly (clang, -O0):
<insert_into_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
lea    0xe2a(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10c0 <file_copy@plt>
cmp    $0x0,%rax
jne    11fd <insert_into_file+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    13d9 <insert_into_file+0x229>
mov    -0x38(%rbp),%rdi
lea    0xdfe(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xdd6(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     125a <insert_into_file+0xaa>
cmpq   $0x0,-0x48(%rbp)
je     125a <insert_into_file+0xaa>
cmpq   $0x0,-0x50(%rbp)
jne    1266 <insert_into_file+0xb6>
movl   $0x0,-0x4(%rbp)
jmp    13d9 <insert_into_file+0x229>
jmp    126b <insert_into_file+0xbb>
mov    -0x40(%rbp),%rdx
lea    0x2e1a(%rip),%rdi
mov    $0x1000,%esi
call   10b0 <fgets@plt>
cmp    $0x0,%rax
jne    128f <insert_into_file+0xdf>
jmp    1394 <insert_into_file+0x1e4>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
lea    0x2de3(%rip),%rdi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    137f <insert_into_file+0x1cf>
movl   $0x1,-0x30(%rbp)
mov    -0x48(%rbp),%rsi
lea    0x2dc2(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x50(%rbp),%rdi
call   1090 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2d02(%rip),%rcx
cmp    (%rcx),%eax
je     12ff <insert_into_file+0x14f>
mov    -0x2c(%rbp),%edi
mov    -0x48(%rbp),%rsi
call   10a0 <fputc@plt>
jmp    12d3 <insert_into_file+0x123>
mov    -0x48(%rbp),%rsi
lea    0xd0a(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x40(%rbp),%rdx
lea    0x2d76(%rip),%rdi
mov    $0x1000,%esi
call   10b0 <fgets@plt>
cmp    $0x0,%rax
jne    1333 <insert_into_file+0x183>
jmp    137a <insert_into_file+0x1ca>
mov    -0x28(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
lea    0x2d3f(%rip),%rdi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1375 <insert_into_file+0x1c5>
mov    -0x48(%rbp),%rsi
lea    0x2d25(%rip),%rdi
call   1080 <fputs@plt>
jmp    137a <insert_into_file+0x1ca>
jmp    130f <insert_into_file+0x15f>
jmp    138f <insert_into_file+0x1df>
mov    -0x48(%rbp),%rsi
lea    0x2d06(%rip),%rdi
call   1080 <fputs@plt>
jmp    126b <insert_into_file+0xbb>
mov    -0x50(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1030 <unlink@plt>
cmpl   $0x0,-0x30(%rbp)
je     13d2 <insert_into_file+0x222>
mov    -0x10(%rbp),%rsi
lea    0xc3d(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert_into_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r12
mov    %rdx,0x20(%rsp)
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xe2d(%rip),%rsi
call   10c0 <file_copy@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     139b <insert_into_file+0x1eb>
lea    0xe15(%rip),%rdi
lea    0xe14(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,%r13
lea    0xe07(%rip),%rsi
mov    %rbp,%rdi
call   10d0 <fopen@plt>
mov    %rax,%rbx
mov    %r15,%rdi
lea    0xdf0(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,0x8(%rsp)
test   %r13,%r13
je     139b <insert_into_file+0x1eb>
test   %rbx,%rbx
je     139b <insert_into_file+0x1eb>
cmpq   $0x0,0x8(%rsp)
je     139b <insert_into_file+0x1eb>
mov    %rbx,0x10(%rsp)
mov    %rbp,0x18(%rsp)
lea    0x2e41(%rip),%rdi
mov    $0x1000,%esi
mov    %r13,%rdx
call   10b0 <fgets@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1358 <insert_into_file+0x1a8>
lea    0x2e22(%rip),%r14
jmp    1289 <insert_into_file+0xd9>
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %r13,%rdx
call   10b0 <fgets@plt>
test   %rax,%rax
je     1358 <insert_into_file+0x1a8>
mov    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    0x10(%rsp),%rsi
call   1080 <fputs@plt>
test   %r15,%r15
mov    0x2d2b(%rip),%rbx
jne    1270 <insert_into_file+0xc0>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1090 <fgetc@plt>
cmp    (%rbx),%eax
mov    0x10(%rsp),%r15
je     12f6 <insert_into_file+0x146>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%edi
mov    %r15,%rsi
call   10a0 <fputc@plt>
mov    %rbp,%rdi
call   1090 <fgetc@plt>
cmp    (%rbx),%eax
jne    12e0 <insert_into_file+0x130>
mov    $0xa,%edi
mov    %r15,%rsi
call   10a0 <fputc@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%rdi
mov    $0x1000,%esi
mov    %r13,%rdx
call   10b0 <fgets@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     1270 <insert_into_file+0xc0>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1310 <insert_into_file+0x160>
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <fputs@plt>
jmp    1270 <insert_into_file+0xc0>
mov    0x8(%rsp),%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    0x10(%rsp),%rdi
call   1050 <fclose@plt>
lea    0xc85(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%r14d
test   %ebp,%ebp
je     139b <insert_into_file+0x1eb>
lea    0xc79(%rip),%rdi
mov    0x18(%rsp),%rsi
call   1070 <printf@plt>
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@insert_into_file.line = internal global [4096 x i8] zeroinitializer
@.str = [6 x i8] c"tmp.h\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [12 x i8] c"patched %s\0A\00"
define i32 @insert_into_file(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 0, i32* %11
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %12
%16 = load i8*, i8** %6
%17 = load i8*, i8** %12
%18 = call i64 @file_copy(i8* %16, i8* %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
store i32 0, i32* %5
br label %99
21: ; preds = %4
%22 = load i8*, i8** %12
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %13
%24 = load i8*, i8** %6
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %25, i32** %14
%26 = load i8*, i8** %7
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %15
%28 = load i32*, i32** %13
%29 = icmp eq i32* %28, null
br i1 %29, label %36, label %30
30: ; preds = %21
%31 = load i32*, i32** %14
%32 = icmp eq i32* %31, null
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = load i32*, i32** %15
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %37
36: ; preds = %33, %30, %21
store i32 0, i32* %5
br label %99
37: ; preds = %33
br label %38
38: ; preds = %83, %37
%39 = load i32*, i32** %13
%40 = call i32* @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32 4096, i32* %39)
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %43
42: ; preds = %38
br label %84
43: ; preds = %38
%44 = load i8*, i8** %8
%45 = load i8*, i8** %8
%46 = call i32 @strlen(i8* %45)
%47 = call i64 @strncmp(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i8* %44, i32 %46)
%48 = icmp eq i64 %47, 0
br i1 %48, label %49, label %80
49: ; preds = %43
store i32 1, i32* %11
%50 = load i32*, i32** %14
%51 = call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32* %50)
br label %52
52: ; preds = %57, %49
%53 = load i32*, i32** %15
%54 = call i32 @fgetc(i32* %53)
store i32 %54, i32* %10
%55 = load i32, i32* @EOF
%56 = icmp ne i32 %54, %55
br i1 %56, label %57, label %61
57: ; preds = %52
%58 = load i32, i32* %10
%59 = load i32*, i32** %14
%60 = call i32 @fputc(i32 %58, i32* %59)
br label %52
61: ; preds = %52
%62 = load i32*, i32** %14
%63 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32* %62)
br label %64
64: ; preds = %78, %61
%65 = load i32*, i32** %13
%66 = call i32* @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32 4096, i32* %65)
%67 = icmp eq i32* %66, null
br i1 %67, label %68, label %69
68: ; preds = %64
br label %79
69: ; preds = %64
%70 = load i8*, i8** %9
%71 = load i8*, i8** %9
%72 = call i32 @strlen(i8* %71)
%73 = call i64 @strncmp(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i8* %70, i32 %72)
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %69
%76 = load i32*, i32** %14
%77 = call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32* %76)
br label %79
78: ; preds = %69
br label %64
79: ; preds = %75, %68
br label %83
80: ; preds = %43
%81 = load i32*, i32** %14
%82 = call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32* %81)
br label %83
83: ; preds = %80, %79
br label %38
84: ; preds = %42
%85 = load i32*, i32** %15
%86 = call i32 @fclose(i32* %85)
%87 = load i32*, i32** %13
%88 = call i32 @fclose(i32* %87)
%89 = load i32*, i32** %14
%90 = call i32 @fclose(i32* %89)
%91 = load i8*, i8** %12
%92 = call i32 @unlink(i8* %91)
%93 = load i32, i32* %11
%94 = icmp ne i32 %93, 0
br i1 %94, label %95, label %98
95: ; preds = %84
%96 = load i8*, i8** %6
%97 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %96)
br label %98
98: ; preds = %95, %84
store i32 1, i32* %5
br label %99
99: ; preds = %98, %36, %20
%100 = load i32, i32* %5
ret i32 %100
}

LLVM IR (-O2):
@insert_into_file.line = internal global [4096 x i8] zeroinitializer
@.str = [6 x i8] c"tmp.h\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@EOF = global i32 0
@.str.4 = [12 x i8] c"patched %s\0A\00"
define i32 @insert_into_file(i8* %0, i8* nocapture %1, i8* %2, i8* %3) {
%5 = tail call i64 @file_copy(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i64 %5, 0
br i1 %6, label %59, label %7
7: ; preds = %4
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %8, null
%12 = icmp eq i32* %9, null
%13 = or i1 %11, %12
%14 = icmp eq i32* %10, null
%15 = or i1 %13, %14
br i1 %15, label %59, label %16
16: ; preds = %7
%17 = tail call i32* @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32 4096, i32* %8)
%18 = icmp eq i32* %17, null
br i1 %18, label %50, label %19
19: ; preds = %16, %46
%20 = phi i32 [ %47, %46 ], [ 0, %16 ]
%21 = tail call i32 @strlen(i8* %2)
%22 = tail call i64 @strncmp(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i8* %2, i32 %21)
%23 = icmp eq i64 %22, 0
%24 = tail call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32* %9)
br i1 %23, label %25, label %46
25: ; preds = %19
%26 = tail call i32 @fgetc(i32* %10)
%27 = load i32, i32* @EOF
%28 = icmp eq i32 %26, %27
br i1 %28, label %35, label %29
29: ; preds = %25, %29
%30 = phi i32 [ %32, %29 ], [ %26, %25 ]
%31 = tail call i32 @fputc(i32 %30, i32* %9)
%32 = tail call i32 @fgetc(i32* %10)
%33 = load i32, i32* @EOF
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %29
35: ; preds = %29, %25
%36 = tail call i32 @fputc(i32 10, i32* %9)
br label %37
37: ; preds = %40, %35
%38 = tail call i32* @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32 4096, i32* %8)
%39 = icmp eq i32* %38, null
br i1 %39, label %46, label %40
40: ; preds = %37
%41 = tail call i32 @strlen(i8* %3)
%42 = tail call i64 @strncmp(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i8* %3, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %37
44: ; preds = %40
%45 = tail call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32* %9)
br label %46
46: ; preds = %37, %19, %44
%47 = phi i32 [ 1, %44 ], [ %20, %19 ], [ 1, %37 ]
%48 = tail call i32* @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32 4096, i32* %8)
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %19
50: ; preds = %46, %16
%51 = phi i32 [ 0, %16 ], [ %47, %46 ]
%52 = tail call i32 @fclose(i32* %10)
%53 = tail call i32 @fclose(i32* %8)
%54 = tail call i32 @fclose(i32* %9)
%55 = tail call i32 @unlink(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%56 = icmp eq i32 %51, 0
br i1 %56, label %59, label %57
57: ; preds = %50
%58 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %59
59: ; preds = %50, %57, %7, %4
%60 = phi i32 [ 0, %4 ], [ 0, %7 ], [ 1, %57 ], [ 1, %50 ]
ret i32 %60
}