#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*/ *);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *strstr(char *, char *);
int modify_script(char *tmpfile, char *modfile, char *addstr) {
  char *filestr;
  FILE *f, *fos, *tf;
  int len = 1024;
  filestr = (char *)malloc(1024);
  f = fopen(tmpfile, "r");
  if (f == NULL) {
    printf("file open error\n");
    free(filestr);
    return (0);
  }
  tf = fopen(modfile, "w");
  if (tf == NULL) {
    printf("file open error\n");
    free(filestr);
    return (0);
  }
  for (;;) {
    if (getline(&filestr, (size_t *)&len, f) < 0)
      break;
    fputs(filestr, tf);
    if (strstr(filestr, "#") != NULL)
      continue;
    else
      break;
  }
  fputs(addstr, tf);
  for (;;) {
    if (getline(&filestr, (size_t *)&len, f) < 0)
      break;
    fputs(filestr, tf);
    if (feof(f) == 0)
      continue;
    else
      break;
  }
  fclose(tf);
  fclose(f);
  free(filestr);
  return 1;
}


x86 assembly (clang, -O0):
<modify_script>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x400,-0x44(%rbp)
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f9 <modify_script+0x69>
lea    0xe23(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1308 <modify_script+0x178>
mov    -0x18(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1239 <modify_script+0xa9>
lea    0xde3(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1308 <modify_script+0x178>
jmp    123e <modify_script+0xae>
lea    -0x44(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   10a0 <getline@plt>
cmp    $0x0,%rax
jge    125e <modify_script+0xce>
jmp    128f <modify_script+0xff>
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x28(%rbp),%rdi
lea    0xd9f(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     128a <modify_script+0xfa>
jmp    123e <modify_script+0xae>
jmp    128f <modify_script+0xff>
mov    -0x20(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <fputs@plt>
lea    -0x44(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    -0x28(%rbp),%rdi
call   10a0 <getline@plt>
cmp    $0x0,%rax
jge    12bc <modify_script+0x12c>
jmp    12e6 <modify_script+0x156>
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x30(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%rax
jne    12e1 <modify_script+0x151>
jmp    129c <modify_script+0x10c>
jmp    12e6 <modify_script+0x156>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<modify_script>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
movl   $0x400,0xc(%rsp)
mov    $0x400,%edi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,(%rsp)
lea    0xe4a(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     129c <modify_script+0x10c>
mov    %rax,%rbx
lea    0xe1f(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     129c <modify_script+0x10c>
mov    %rax,%r14
mov    %rsp,%r12
lea    0xc(%rsp),%r13
nopl   (%rax)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
call   10b0 <getline@plt>
test   %rax,%rax
js     1232 <modify_script+0xa2>
mov    (%rsp),%rdi
mov    %r14,%rsi
call   1070 <fputs@plt>
mov    (%rsp),%rdi
mov    $0x23,%esi
call   1060 <strchr@plt>
test   %rax,%rax
jne    1200 <modify_script+0x70>
mov    %r15,%rdi
mov    %r14,%rsi
call   1070 <fputs@plt>
mov    %rsp,%r15
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10b0 <getline@plt>
test   %rax,%rax
js     127c <modify_script+0xec>
mov    (%rsp),%rdi
mov    %r14,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %rax,%rax
je     1250 <modify_script+0xc0>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    12b2 <modify_script+0x122>
lea    0xd5f(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [17 x i8] c"file open error\0A\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [2 x i8] c"#\00"
define i32 @modify_script(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 1024, i32* %12
%13 = call i64 @malloc(i32 1024)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %8
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %3
%20 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** %8
%22 = call i32 @free(i8* %21)
store i32 0, i32* %4
br label %74
23: ; preds = %3
%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** %11
%26 = load i32*, i32** %11
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%30 = load i8*, i8** %8
%31 = call i32 @free(i8* %30)
store i32 0, i32* %4
br label %74
32: ; preds = %23
br label %33
33: ; preds = %46, %32
%34 = bitcast i32* %12 to i64*
%35 = load i32*, i32** %9
%36 = call i64 @getline(i8** %8, i64* %34, i32* %35)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %48
39: ; preds = %33
%40 = load i8*, i8** %8
%41 = load i32*, i32** %11
%42 = call i32 @fputs(i8* %40, i32* %41)
%43 = load i8*, i8** %8
%44 = call i32* @strstr(i8* %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %47
46: ; preds = %39
br label %33
47: ; preds = %39
br label %48
48: ; preds = %47, %38
%49 = load i8*, i8** %7
%50 = load i32*, i32** %11
%51 = call i32 @fputs(i8* %49, i32* %50)
br label %52
52: ; preds = %65, %48
%53 = bitcast i32* %12 to i64*
%54 = load i32*, i32** %9
%55 = call i64 @getline(i8** %8, i64* %53, i32* %54)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %52
br label %67
58: ; preds = %52
%59 = load i8*, i8** %8
%60 = load i32*, i32** %11
%61 = call i32 @fputs(i8* %59, i32* %60)
%62 = load i32*, i32** %9
%63 = call i64 @feof(i32* %62)
%64 = icmp eq i64 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %58
br label %52
66: ; preds = %58
br label %67
67: ; preds = %66, %57
%68 = load i32*, i32** %11
%69 = call i32 @fclose(i32* %68)
%70 = load i32*, i32** %9
%71 = call i32 @fclose(i32* %70)
%72 = load i8*, i8** %8
%73 = call i32 @free(i8* %72)
store i32 1, i32* %4
br label %74
74: ; preds = %67, %28, %19
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@str.4 = [16 x i8] c"file open error\00"
define i32 @modify_script(i8* nocapture %0, i8* nocapture %1, i8* nocapture %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = bitcast i8** %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 1024, i32* %5
%8 = tail call i64 @malloc(i32 1024)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %4
%10 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
%14 = tail call i32 @free(i8* %9)
br label %47
15: ; preds = %3
%16 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = bitcast i32* %5 to i64*
br label %23
20: ; preds = %15
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
%22 = tail call i32 @free(i8* %9)
br label %47
23: ; preds = %18, %26
%24 = call i64 @getline(i8** %4, i64* %19, i32* %10)
%25 = icmp slt i64 %24, 0
br i1 %25, label %32, label %26
26: ; preds = %23
%27 = load i8*, i8** %4
%28 = call i32 @fputs(i8* %27, i32* %16)
%29 = load i8*, i8** %4
%30 = call i8* @strchr(i8* dereferenceable(1) %29, i32 35)
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %23
32: ; preds = %26, %23
%33 = call i32 @fputs(i8* %2, i32* %16)
br label %34
34: ; preds = %37, %32
%35 = call i64 @getline(i8** %4, i64* %19, i32* %10)
%36 = icmp slt i64 %35, 0
br i1 %36, label %42, label %37
37: ; preds = %34
%38 = load i8*, i8** %4
%39 = call i32 @fputs(i8* %38, i32* %16)
%40 = call i64 @feof(i32* %10)
%41 = icmp eq i64 %40, 0
br i1 %41, label %34, label %42
42: ; preds = %37, %34
%43 = call i32 @fclose(i32* %16)
%44 = call i32 @fclose(i32* %10)
%45 = load i8*, i8** %4
%46 = call i32 @free(i8* %45)
br label %47
47: ; preds = %42, %20, %12
%48 = phi i32 [ 0, %12 ], [ 0, %20 ], [ 1, %42 ]
ret i32 %48
}