#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 /*<<< orphan*/ fclose(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*/ fputs(char const *, int /*<<< orphan*/ *);
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 *text,
                     const char *start_line, const char *end_line) {
  static char line[1024 * 4];
  const char *tmpname = "tmp.h";
  FILE *source_fp;
  FILE *dest_fp;
  if (file_copy(filename, tmpname) == 0)
    return 0;
  source_fp = fopen(tmpname, "r");
  dest_fp = fopen(filename, "w");
  if (source_fp == NULL || dest_fp == NULL)
    return 0;
  for (;;) {
    if (fgets(line, 1024 * 4, source_fp) == NULL)
      break;
    if (strncmp(line, start_line, strlen(start_line)) == 0) {
      fputs(line, dest_fp);
      fputs(text, 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(source_fp);
  fclose(dest_fp);
  unlink(tmpname);
  return 1;
}


x86 assembly (clang, -O0):
<insert_into_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    0xe61(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <file_copy@plt>
cmp    $0x0,%rax
jne    11c6 <insert_into_file+0x46>
movl   $0x0,-0x4(%rbp)
jmp    133a <insert_into_file+0x1ba>
mov    -0x30(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe23(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1204 <insert_into_file+0x84>
cmpq   $0x0,-0x40(%rbp)
jne    1210 <insert_into_file+0x90>
movl   $0x0,-0x4(%rbp)
jmp    133a <insert_into_file+0x1ba>
jmp    1215 <insert_into_file+0x95>
mov    -0x38(%rbp),%rdx
lea    0x2e50(%rip),%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    1239 <insert_into_file+0xb9>
jmp    1318 <insert_into_file+0x198>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0x2e19(%rip),%rdi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1303 <insert_into_file+0x183>
mov    -0x40(%rbp),%rsi
lea    0x2dff(%rip),%rdi
call   1070 <fputs@plt>
mov    -0x18(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1070 <fputs@plt>
mov    -0x40(%rbp),%rsi
lea    0xd7c(%rip),%rdi
call   1070 <fputs@plt>
mov    -0x38(%rbp),%rdx
lea    0x2dd2(%rip),%rdi
mov    $0x1000,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    12b7 <insert_into_file+0x137>
jmp    12fe <insert_into_file+0x17e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    0x2d9b(%rip),%rdi
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    12f9 <insert_into_file+0x179>
mov    -0x40(%rbp),%rsi
lea    0x2d81(%rip),%rdi
call   1070 <fputs@plt>
jmp    12fe <insert_into_file+0x17e>
jmp    1293 <insert_into_file+0x113>
jmp    1313 <insert_into_file+0x193>
mov    -0x40(%rbp),%rsi
lea    0x2d62(%rip),%rdi
call   1070 <fputs@plt>
jmp    1215 <insert_into_file+0x95>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert_into_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rsi,(%rsp)
mov    %rdi,%rbp
lea    0xe51(%rip),%rsi
call   10a0 <file_copy@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     12e1 <insert_into_file+0x151>
lea    0xe39(%rip),%rdi
lea    0xe38(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%r12
lea    0xe2b(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %r12,%r12
je     12e1 <insert_into_file+0x151>
mov    %rax,%r13
test   %rax,%rax
je     12e1 <insert_into_file+0x151>
lea    0x2e7f(%rip),%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     12bf <insert_into_file+0x12f>
lea    0x2e62(%rip),%rbp
jmp    1239 <insert_into_file+0xa9>
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     12bf <insert_into_file+0x12f>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <fputs@plt>
test   %r15,%r15
jne    1220 <insert_into_file+0x90>
mov    (%rsp),%rdi
mov    %r13,%rsi
call   1070 <fputs@plt>
mov    $0xa,%edi
mov    %r13,%rsi
call   1080 <fputc@plt>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r12,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1220 <insert_into_file+0x90>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    1280 <insert_into_file+0xf0>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <fputs@plt>
jmp    1220 <insert_into_file+0x90>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
lea    0xd2a(%rip),%rdi
call   1030 <unlink@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x8,%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"
@.str.3 = [2 x i8] c"\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 i8*
%11 = alloca i32*
%12 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8** %10
%13 = load i8*, i8** %6
%14 = load i8*, i8** %10
%15 = call i64 @file_copy(i8* %13, i8* %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 0, i32* %5
br label %76
18: ; preds = %4
%19 = load i8*, i8** %10
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %11
%21 = load i8*, i8** %6
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %22, i32** %12
%23 = load i32*, i32** %11
%24 = icmp eq i32* %23, null
br i1 %24, label %28, label %25
25: ; preds = %18
%26 = load i32*, i32** %12
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %25, %18
store i32 0, i32* %5
br label %76
29: ; preds = %25
br label %30
30: ; preds = %68, %29
%31 = load i32*, i32** %11
%32 = call i32* @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32 4096, i32* %31)
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %35
34: ; preds = %30
br label %69
35: ; preds = %30
%36 = load i8*, i8** %8
%37 = load i8*, i8** %8
%38 = call i32 @strlen(i8* %37)
%39 = call i64 @strncmp(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i8* %36, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %65
41: ; preds = %35
%42 = load i32*, i32** %12
%43 = call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32* %42)
%44 = load i8*, i8** %7
%45 = load i32*, i32** %12
%46 = call i32 @fputs(i8* %44, i32* %45)
%47 = load i32*, i32** %12
%48 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32* %47)
br label %49
49: ; preds = %63, %41
%50 = load i32*, i32** %11
%51 = call i32* @fgets(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32 4096, i32* %50)
%52 = icmp eq i32* %51, null
br i1 %52, label %53, label %54
53: ; preds = %49
br label %64
54: ; preds = %49
%55 = load i8*, i8** %9
%56 = load i8*, i8** %9
%57 = call i32 @strlen(i8* %56)
%58 = call i64 @strncmp(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i8* %55, i32 %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %54
%61 = load i32*, i32** %12
%62 = call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32* %61)
br label %64
63: ; preds = %54
br label %49
64: ; preds = %60, %53
br label %68
65: ; preds = %35
%66 = load i32*, i32** %12
%67 = call i32 @fputs(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i32* %66)
br label %68
68: ; preds = %65, %64
br label %30
69: ; preds = %34
%70 = load i32*, i32** %11
%71 = call i32 @fclose(i32* %70)
%72 = load i32*, i32** %12
%73 = call i32 @fclose(i32* %72)
%74 = load i8*, i8** %10
%75 = call i32 @unlink(i8* %74)
store i32 1, i32* %5
br label %76
76: ; preds = %69, %28, %17
%77 = load i32, i32* %5
ret i32 %77
}

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"
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 %40, 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 = icmp eq i32* %8, null
%11 = icmp eq i32* %9, null
%12 = or i1 %10, %11
br i1 %12, label %40, label %13
13: ; preds = %7
%14 = 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)
%15 = icmp eq i32* %14, null
br i1 %15, label %36, label %16
16: ; preds = %13, %33
%17 = tail call i32 @strlen(i8* %2)
%18 = tail call i64 @strncmp(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i8* %2, i32 %17)
%19 = icmp eq i64 %18, 0
%20 = 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 %19, label %21, label %33
21: ; preds = %16
%22 = tail call i32 @fputs(i8* %1, i32* %9)
%23 = tail call i32 @fputc(i32 10, i32* %9)
br label %24
24: ; preds = %27, %21
%25 = 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)
%26 = icmp eq i32* %25, null
br i1 %26, label %33, label %27
27: ; preds = %24
%28 = tail call i32 @strlen(i8* %3)
%29 = tail call i64 @strncmp(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @insert_into_file.line, i64 0, i64 0), i8* %3, i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %24
31: ; preds = %27
%32 = 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 %33
33: ; preds = %24, %16, %31
%34 = 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)
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %16
36: ; preds = %33, %13
%37 = tail call i32 @fclose(i32* %8)
%38 = tail call i32 @fclose(i32* %9)
%39 = tail call i32 @unlink(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %7, %4, %36
%41 = phi i32 [ 1, %36 ], [ 0, %4 ], [ 0, %7 ]
ret i32 %41
}