#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*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int findandreplace(char *, char *, char *, unsigned long);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, unsigned long, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
unsigned long ftell(int /*<<< orphan*/ *);
size_t fwrite(char *, int, unsigned long, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int replacStrAtNew(char **strList1, char **strList2, char *path, int count) {
  int ret = 0;
  unsigned long filesize;
  char *content;
  FILE *org_fileh = fopen(path, "r");
  if (org_fileh == NULL)
    return -1;
  fseek(org_fileh, 0L, SEEK_END);
  filesize = ftell(org_fileh);
  rewind(org_fileh);
  content = malloc(filesize);
  fread(content, 1, filesize, org_fileh);
  fclose(org_fileh);
  int i;
  for (i = 0; i < count; i++) {
    char *str_org = strList1[i];
    char *str_new = strList2[i];
    if (strlen(str_org) == 0 || strlen(str_new) == 0) {
      ret = -2;
      break;
    }
    if (strlen(str_org) != strlen(str_new)) {
      ret = -3;
      break;
    }
    int times = findandreplace(str_org, str_new, content, filesize);
    if (times < 0) {
      ret = -4;
      break;
    }
    printf("> replace '%s' %d times.\n", str_org, times);
    ret += times;
  }
  if (ret > 0) {
    char *newpath = malloc(strlen(path) + 10);
    sprintf(newpath, "%s_new", path);
    FILE *new_fileh = fopen(newpath, "w+");
    if (new_fileh == NULL) {
      free(content);
      return -5;
    }
    size_t nbytes = fwrite(content, 1, filesize, new_fileh);
    if (nbytes != filesize) {
      free(content);
      return -6;
    }
    fclose(new_fileh);
    free(newpath);
  }
  free(content);
  return ret;
}


x86 assembly (clang, -O0):
<replacStrAtNew>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    121c <replacStrAtNew+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1426 <replacStrAtNew+0x256>
mov    -0x40(%rbp),%rdi
mov    0x2dc1(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <fseek@plt>
mov    -0x40(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
call   1090 <rewind@plt>
mov    -0x30(%rbp),%rax
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    -0x40(%rbp),%rdi
call   1060 <fclose@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    135f <replacStrAtNew+0x18f>
mov    -0x10(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x0,%eax
je     12cd <replacStrAtNew+0xfd>
mov    -0x58(%rbp),%rdi
call   1070 <strlen@plt>
cmp    $0x0,%eax
jne    12d9 <replacStrAtNew+0x109>
movl   $0xfffffffe,-0x28(%rbp)
jmp    135f <replacStrAtNew+0x18f>
mov    -0x50(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x7c(%rbp)
mov    -0x58(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%ecx
mov    -0x7c(%rbp),%eax
cmp    %ecx,%eax
je     1307 <replacStrAtNew+0x137>
movl   $0xfffffffd,-0x28(%rbp)
jmp    135f <replacStrAtNew+0x18f>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1040 <findandreplace@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    1335 <replacStrAtNew+0x165>
movl   $0xfffffffc,-0x28(%rbp)
jmp    135f <replacStrAtNew+0x18f>
mov    -0x50(%rbp),%rsi
mov    -0x5c(%rbp),%edx
lea    0xcbf(%rip),%rdi
call   1080 <printf@plt>
mov    -0x5c(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    127d <replacStrAtNew+0xad>
cmpl   $0x0,-0x28(%rbp)
jle    1417 <replacStrAtNew+0x247>
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,%edi
add    $0xa,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xc8d(%rip),%rsi
call   10e0 <sprintf@plt>
mov    -0x68(%rbp),%rdi
lea    0xc84(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmpq   $0x0,-0x70(%rbp)
jne    13c8 <replacStrAtNew+0x1f8>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1426 <replacStrAtNew+0x256>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x70(%rbp),%rcx
mov    $0x1,%esi
call   10f0 <fwrite@plt>
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     1405 <replacStrAtNew+0x235>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffa,-0x4(%rbp)
jmp    1426 <replacStrAtNew+0x256>
mov    -0x70(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x68(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replacStrAtNew>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r14d
mov    %rdx,%rbx
mov    %rsi,0x20(%rsp)
mov    %rdi,0x18(%rsp)
lea    0xe0b(%rip),%rsi
mov    %rdx,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     138e <replacStrAtNew+0x1be>
mov    %rax,%r15
mov    %rbx,0x10(%rsp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    %r15,%rdi
call   10a0 <ftell@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1090 <rewind@plt>
mov    %ebx,%edi
call   10b0 <malloc@plt>
mov    %rax,(%rsp)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbx,0x8(%rsp)
mov    %rbx,%rdx
mov    %r15,%rcx
call   1050 <fread@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
test   %r14d,%r14d
jle    13a1 <replacStrAtNew+0x1d1>
mov    %r14d,%r13d
xor    %r12d,%r12d
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x18(%rsp),%rax
mov    (%rax,%r12,8),%r14
mov    0x20(%rsp),%rax
mov    (%rax,%r12,8),%rbx
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    $0xfffffffe,%ebp
test   %eax,%eax
je     13a1 <replacStrAtNew+0x1d1>
mov    %rbx,%rdi
call   1070 <strlen@plt>
test   %eax,%eax
je     13a1 <replacStrAtNew+0x1d1>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <strlen@plt>
cmp    %eax,%ebp
jne    1395 <replacStrAtNew+0x1c5>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    (%rsp),%rdx
mov    0x8(%rsp),%rcx
call   1040 <findandreplace@plt>
test   %eax,%eax
js     139c <replacStrAtNew+0x1cc>
mov    %eax,%ebx
lea    0xd0c(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1080 <printf@plt>
add    %ebx,%r15d
add    $0x1,%r12
cmp    %r12,%r13
jne    1280 <replacStrAtNew+0xb0>
test   %r15d,%r15d
jle    1389 <replacStrAtNew+0x1b9>
mov    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <strlen@plt>
lea    0xa(%rax),%edi
call   10b0 <malloc@plt>
mov    %rax,%r14
lea    0xce8(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   10e0 <sprintf@plt>
lea    0xcdd(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     13bb <replacStrAtNew+0x1eb>
mov    %rax,%rbx
mov    (%rsp),%rdi
mov    $0x1,%esi
mov    0x8(%rsp),%r12
mov    %r12,%rdx
mov    %rax,%rcx
call   10f0 <fwrite@plt>
mov    $0xfffffffa,%ebp
cmp    %r12,%rax
jne    13a1 <replacStrAtNew+0x1d1>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%ebp
jmp    13a1 <replacStrAtNew+0x1d1>
mov    $0xffffffff,%ebp
jmp    13aa <replacStrAtNew+0x1da>
mov    $0xfffffffd,%ebp
jmp    13a1 <replacStrAtNew+0x1d1>
mov    $0xfffffffc,%ebp
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xfffffffb,%ebp
jmp    13a1 <replacStrAtNew+0x1d1>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.1 = [26 x i8] c"> replace '%s' %d times.\0A\00"
@.str.2 = [7 x i8] c"%s_new\00"
@.str.3 = [3 x i8] c"w+\00"
define i32 @replacStrAtNew(i8** %0, i8** %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i32*
%20 = alloca i64
store i8** %0, i8*** %6
store i8** %1, i8*** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
store i32 0, i32* %10
%21 = load i8*, i8** %8
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %13
%23 = load i32*, i32** %13
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %4
store i32 -1, i32* %5
br label %130
26: ; preds = %4
%27 = load i32*, i32** %13
%28 = load i32, i32* @SEEK_END
%29 = call i32 @fseek(i32* %27, i64 0, i32 %28)
%30 = load i32*, i32** %13
%31 = call i64 @ftell(i32* %30)
store i64 %31, i64* %11
%32 = load i32*, i32** %13
%33 = call i32 @rewind(i32* %32)
%34 = load i64, i64* %11
%35 = trunc i64 %34 to i32
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %12
%37 = load i8*, i8** %12
%38 = load i64, i64* %11
%39 = load i32*, i32** %13
%40 = call i32 @fread(i8* %37, i32 1, i64 %38, i32* %39)
%41 = load i32*, i32** %13
%42 = call i32 @fclose(i32* %41)
store i32 0, i32* %14
br label %43
43: ; preds = %89, %26
%44 = load i32, i32* %14
%45 = load i32, i32* %9
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %92
47: ; preds = %43
%48 = load i8**, i8*** %6
%49 = load i32, i32* %14
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %48, i64 %50
%52 = load i8*, i8** %51
store i8* %52, i8** %15
%53 = load i8**, i8*** %7
%54 = load i32, i32* %14
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
store i8* %57, i8** %16
%58 = load i8*, i8** %15
%59 = call i32 @strlen(i8* %58)
%60 = icmp eq i32 %59, 0
br i1 %60, label %65, label %61
61: ; preds = %47
%62 = load i8*, i8** %16
%63 = call i32 @strlen(i8* %62)
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %61, %47
store i32 -2, i32* %10
br label %92
66: ; preds = %61
%67 = load i8*, i8** %15
%68 = call i32 @strlen(i8* %67)
%69 = load i8*, i8** %16
%70 = call i32 @strlen(i8* %69)
%71 = icmp ne i32 %68, %70
br i1 %71, label %72, label %73
72: ; preds = %66
store i32 -3, i32* %10
br label %92
73: ; preds = %66
%74 = load i8*, i8** %15
%75 = load i8*, i8** %16
%76 = load i8*, i8** %12
%77 = load i64, i64* %11
%78 = call i32 @findandreplace(i8* %74, i8* %75, i8* %76, i64 %77)
store i32 %78, i32* %17
%79 = load i32, i32* %17
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %73
store i32 -4, i32* %10
br label %92
82: ; preds = %73
%83 = load i8*, i8** %15
%84 = load i32, i32* %17
%85 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %83, i32 %84)
%86 = load i32, i32* %17
%87 = load i32, i32* %10
%88 = add nsw i32 %87, %86
store i32 %88, i32* %10
br label %89
89: ; preds = %82
%90 = load i32, i32* %14
%91 = add nsw i32 %90, 1
store i32 %91, i32* %14
br label %43
92: ; preds = %81, %72, %65, %43
%93 = load i32, i32* %10
%94 = icmp sgt i32 %93, 0
br i1 %94, label %95, label %126
95: ; preds = %92
%96 = load i8*, i8** %8
%97 = call i32 @strlen(i8* %96)
%98 = add nsw i32 %97, 10
%99 = call i8* @malloc(i32 %98)
store i8* %99, i8** %18
%100 = load i8*, i8** %18
%101 = load i8*, i8** %8
%102 = call i32 @sprintf(i8* %100, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %101)
%103 = load i8*, i8** %18
%104 = call i32* @fopen(i8* %103, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %104, i32** %19
%105 = load i32*, i32** %19
%106 = icmp eq i32* %105, null
br i1 %106, label %107, label %110
107: ; preds = %95
%108 = load i8*, i8** %12
%109 = call i32 @free(i8* %108)
store i32 -5, i32* %5
br label %130
110: ; preds = %95
%111 = load i8*, i8** %12
%112 = load i64, i64* %11
%113 = load i32*, i32** %19
%114 = call i64 @fwrite(i8* %111, i32 1, i64 %112, i32* %113)
store i64 %114, i64* %20
%115 = load i64, i64* %20
%116 = load i64, i64* %11
%117 = icmp ne i64 %115, %116
br i1 %117, label %118, label %121
118: ; preds = %110
%119 = load i8*, i8** %12
%120 = call i32 @free(i8* %119)
store i32 -6, i32* %5
br label %130
121: ; preds = %110
%122 = load i32*, i32** %19
%123 = call i32 @fclose(i32* %122)
%124 = load i8*, i8** %18
%125 = call i32 @free(i8* %124)
br label %126
126: ; preds = %121, %92
%127 = load i8*, i8** %12
%128 = call i32 @free(i8* %127)
%129 = load i32, i32* %10
store i32 %129, i32* %5
br label %130
130: ; preds = %126, %118, %107, %25
%131 = load i32, i32* %5
ret i32 %131
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.1 = [26 x i8] c"> replace '%s' %d times.\0A\00"
@.str.2 = [7 x i8] c"%s_new\00"
@.str.3 = [3 x i8] c"w+\00"
define i32 @replacStrAtNew(i8** nocapture %0, i8** nocapture %1, i8* %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %61, label %7
7: ; preds = %4
%8 = load i32, i32* @SEEK_END
%9 = tail call i32 @fseek(i32* %5, i64 0, i32 %8)
%10 = tail call i64 @ftell(i32* %5)
%11 = tail call i32 @rewind(i32* %5)
%12 = trunc i64 %10 to i32
%13 = tail call i8* @malloc(i32 %12)
%14 = tail call i32 @fread(i8* %13, i32 1, i64 %10, i32* %5)
%15 = tail call i32 @fclose(i32* %5)
%16 = icmp sgt i32 %3, 0
br i1 %16, label %17, label %58
17: ; preds = %7
%18 = zext i32 %3 to i64
br label %19
19: ; preds = %17, %38
%20 = phi i64 [ 0, %17 ], [ %41, %38 ]
%21 = phi i32 [ 0, %17 ], [ %40, %38 ]
%22 = getelementptr inbounds i8*, i8** %0, i64 %20
%23 = load i8*, i8** %22
%24 = getelementptr inbounds i8*, i8** %1, i64 %20
%25 = load i8*, i8** %24
%26 = tail call i32 @strlen(i8* %23)
%27 = icmp eq i32 %26, 0
br i1 %27, label %58, label %28
28: ; preds = %19
%29 = tail call i32 @strlen(i8* %25)
%30 = icmp eq i32 %29, 0
br i1 %30, label %58, label %31
31: ; preds = %28
%32 = tail call i32 @strlen(i8* %23)
%33 = tail call i32 @strlen(i8* %25)
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %58
35: ; preds = %31
%36 = tail call i32 @findandreplace(i8* %23, i8* %25, i8* %13, i64 %10)
%37 = icmp slt i32 %36, 0
br i1 %37, label %58, label %38
38: ; preds = %35
%39 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %23, i32 %36)
%40 = add nsw i32 %36, %21
%41 = add nuw nsw i64 %20, 1
%42 = icmp eq i64 %41, %18
br i1 %42, label %43, label %19
43: ; preds = %38
%44 = icmp sgt i32 %40, 0
br i1 %44, label %45, label %58
45: ; preds = %43
%46 = tail call i32 @strlen(i8* %2)
%47 = add nsw i32 %46, 10
%48 = tail call i8* @malloc(i32 %47)
%49 = tail call i32 @sprintf(i8* dereferenceable(1) %48, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%50 = tail call i32* @fopen(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp eq i32* %50, null
br i1 %51, label %58, label %52
52: ; preds = %45
%53 = tail call i64 @fwrite(i8* %13, i32 1, i64 %10, i32* %50)
%54 = icmp eq i64 %53, %10
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = tail call i32 @fclose(i32* %50)
%57 = tail call i32 @free(i8* %48)
br label %58
58: ; preds = %35, %31, %19, %28, %43, %55, %7, %52, %45
%59 = phi i32 [ -5, %45 ], [ -6, %52 ], [ %40, %55 ], [ %40, %43 ], [ 0, %7 ], [ -2, %28 ], [ -2, %19 ], [ -3, %31 ], [ -4, %35 ]
%60 = tail call i32 @free(i8* %13)
br label %61
61: ; preds = %58, %4
%62 = phi i32 [ -1, %4 ], [ %59, %58 ]
ret i32 %62
}