#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*/ uint8_t;
typedef int const uint32_t;
struct stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int /*<<< orphan*/ *mmap(int /*<<< orphan*/, int const, int, int /*<<< orphan*/,
                         int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(int /*<<< orphan*/ *, int const);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char const *);
scalar_t__ strncmp(char *, char const *, int const);
int /*<<< orphan*/ strncpy(char *, char const *, int const);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: %s fileToPath\n", argv[0]);
    printf("This tool disables the 'csr -T' configuration from BlueTool.\n");
    return 10;
  }
  printf("Patching %s...\n", argv[1]);
  struct stat sb;
  int fd = open(argv[1], O_RDWR);
  if (fd < 0)
    return 10;
  if (fstat(fd, &sb) == -1) {
    close(fd);
    return 10;
  }
  uint32_t len = sb.st_size;
  uint8_t *p = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (p == MAP_FAILED) {
    close(fd);
    return 10;
  }
  int count = 0;
  const char *pattern = "csr -T";
  const char *patternNew = "######";
  const int patternLen = strlen(pattern);
  int i;
  for (i = 0; i < len - patternLen; i++) {
    if (strncmp((char *)p + i, pattern, patternLen) == 0) {
      strncpy((char *)p + i, patternNew, patternLen);
      count++;
    }
  }
  munmap(p, len);
  close(fd);
  printf("Done, replaced %s %u times\n", pattern, count);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11df <main+0x4f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xa,-0x4(%rbp)
jmp    1371 <main+0x1e1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1224 <main+0x94>
movl   $0xa,-0x4(%rbp)
jmp    1371 <main+0x1e1>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    124d <main+0xbd>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xa,-0x4(%rbp)
jmp    1371 <main+0x1e1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    0x2d9b(%rip),%rax
mov    (%rax),%edx
mov    0x2d62(%rip),%rax
or     (%rax),%edx
mov    0x2d79(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1060 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d31(%rip),%rcx
cmp    (%rcx),%rax
jne    12ac <main+0x11c>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
movl   $0xa,-0x4(%rbp)
jmp    1371 <main+0x1e1>
movl   $0x0,-0x2c(%rbp)
lea    0xdaa(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0xda6(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    -0x20(%rbp),%ecx
sub    -0x44(%rbp),%ecx
cmp    %ecx,%eax
jge    1341 <main+0x1b1>
mov    -0x28(%rbp),%rdi
movslq -0x48(%rbp),%rax
add    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    132e <main+0x19e>
mov    -0x28(%rbp),%rdi
movslq -0x48(%rbp),%rax
add    %rax,%rdi
mov    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1333 <main+0x1a3>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    12dc <main+0x14c>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1090 <munmap@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11dd <main+0x3d>
mov    (%rbx),%rsi
lea    0xe40(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0xe82(%rip),%rdi
call   1050 <puts@plt>
mov    $0xa,%ebx
jmp    130b <main+0x16b>
mov    0x8(%rbx),%rsi
lea    0xe2e(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    0x8(%rbx),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    $0xa,%ebx
test   %eax,%eax
js     130b <main+0x16b>
mov    %eax,%r15d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
cmp    $0xffffffff,%eax
je     12d2 <main+0x132>
mov    0x8(%rsp),%r14d
mov    0x2dc6(%rip),%rax
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da4(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r14d,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    0x2d61(%rip),%rcx
mov    %rax,0x10(%rsp)
cmp    (%rcx),%rax
je     12d2 <main+0x132>
lea    0xdb6(%rip),%rdi
call   1060 <strlen@plt>
mov    %r14d,0xc(%rsp)
sub    %eax,%r14d
test   %r14d,%r14d
jle    12dc <main+0x13c>
mov    %eax,%ebx
mov    %r14d,%r14d
xor    %ebp,%ebp
lea    0xd96(%rip),%r13
mov    0x10(%rsp),%r12
jmp    12aa <main+0x10a>
nopw   0x0(%rax,%rax,1)
add    $0x1,%r12
add    $0xffffffffffffffff,%r14
je     12dc <main+0x13c>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    12a0 <main+0x100>
mov    %r12,%rdi
lea    0xd67(%rip),%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
add    $0x1,%ebp
jmp    12a0 <main+0x100>
mov    %r15d,%edi
call   1090 <close@plt>
jmp    130b <main+0x16b>
mov    0x10(%rsp),%rdi
mov    0xc(%rsp),%esi
call   10a0 <munmap@plt>
mov    %r15d,%edi
call   1090 <close@plt>
lea    0xd3b(%rip),%rdi
lea    0xd26(%rip),%rsi
xor    %ebx,%ebx
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [22 x i8] c"Usage: %s fileToPath\0A\00"
@.str.1 = [62 x i8] c"This tool disables the 'csr -T' configuration from BlueTool.\0A\00"
@.str.2 = [16 x i8] c"Patching %s...\0A\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [7 x i8] c"csr -T\00"
@.str.4 = [7 x i8] c"######\00"
@.str.5 = [28 x i8] c"Done, replaced %s %u times\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0))
store i32 10, i32* %3
br label %102
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* %30, i32 %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %23
store i32 10, i32* %3
br label %102
36: ; preds = %23
%37 = load i32, i32* %7
%38 = call i32 @fstat(i32 %37, %struct.stat* %6)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
store i32 10, i32* %3
br label %102
43: ; preds = %36
%44 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%45 = load i32, i32* %44
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = load i32, i32* @PROT_READ
%48 = load i32, i32* @PROT_WRITE
%49 = or i32 %47, %48
%50 = load i32, i32* @MAP_SHARED
%51 = load i32, i32* %7
%52 = call i32* @mmap(i32 0, i32 %46, i32 %49, i32 %50, i32 %51, i32 0)
store i32* %52, i32** %9
%53 = load i32*, i32** %9
%54 = load i32*, i32** @MAP_FAILED
%55 = icmp eq i32* %53, %54
br i1 %55, label %56, label %59
56: ; preds = %43
%57 = load i32, i32* %7
%58 = call i32 @close(i32 %57)
store i32 10, i32* %3
br label %102
59: ; preds = %43
store i32 0, i32* %10
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8** %11
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8** %12
%60 = load i8*, i8** %11
%61 = call i32 @strlen(i8* %60)
store i32 %61, i32* %13
store i32 0, i32* %14
br label %62
62: ; preds = %90, %59
%63 = load i32, i32* %14
%64 = load i32, i32* %8
%65 = load i32, i32* %13
%66 = sub nsw i32 %64, %65
%67 = icmp slt i32 %63, %66
br i1 %67, label %68, label %93
68: ; preds = %62
%69 = load i32*, i32** %9
%70 = bitcast i32* %69 to i8*
%71 = load i32, i32* %14
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %70, i64 %72
%74 = load i8*, i8** %11
%75 = load i32, i32* %13
%76 = call i64 @strncmp(i8* %73, i8* %74, i32 %75)
%77 = icmp eq i64 %76, 0
br i1 %77, label %78, label %89
78: ; preds = %68
%79 = load i32*, i32** %9
%80 = bitcast i32* %79 to i8*
%81 = load i32, i32* %14
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = load i8*, i8** %12
%85 = load i32, i32* %13
%86 = call i32 @strncpy(i8* %83, i8* %84, i32 %85)
%87 = load i32, i32* %10
%88 = add nsw i32 %87, 1
store i32 %88, i32* %10
br label %89
89: ; preds = %78, %68
br label %90
90: ; preds = %89
%91 = load i32, i32* %14
%92 = add nsw i32 %91, 1
store i32 %92, i32* %14
br label %62
93: ; preds = %62
%94 = load i32*, i32** %9
%95 = load i32, i32* %8
%96 = call i32 @munmap(i32* %94, i32 %95)
%97 = load i32, i32* %7
%98 = call i32 @close(i32 %97)
%99 = load i8*, i8** %11
%100 = load i32, i32* %10
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %99, i32 %100)
store i32 0, i32* %3
br label %102
102: ; preds = %93, %56, %40, %35, %17
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [22 x i8] c"Usage: %s fileToPath\0A\00"
@.str.2 = [16 x i8] c"Patching %s...\0A\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.3 = [7 x i8] c"csr -T\00"
@.str.4 = [7 x i8] c"######\00"
@.str.5 = [28 x i8] c"Done, replaced %s %u times\0A\00"
@str = [61 x i8] c"This tool disables the 'csr -T' configuration from BlueTool.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str, i64 0, i64 0))
br label %62
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = bitcast %struct.stat* %3 to i8*
%14 = load i8*, i8** %10
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* %14, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %60, label %18
18: ; preds = %9
%19 = call i32 @fstat(i32 %16, %struct.stat* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @close(i32 %16)
br label %60
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %27, %26
%29 = load i32, i32* @MAP_SHARED
%30 = tail call i32* @mmap(i32 0, i32 %25, i32 %28, i32 %29, i32 %16, i32 0)
%31 = load i32*, i32** @MAP_FAILED
%32 = icmp eq i32* %30, %31
br i1 %32, label %33, label %35
33: ; preds = %23
%34 = tail call i32 @close(i32 %16)
br label %60
35: ; preds = %23
%36 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%37 = sub i32 %25, %36
%38 = icmp sgt i32 %37, 0
br i1 %38, label %39, label %55
39: ; preds = %35
%40 = bitcast i32* %30 to i8*
%41 = zext i32 %37 to i64
br label %42
42: ; preds = %39, %51
%43 = phi i64 [ 0, %39 ], [ %53, %51 ]
%44 = phi i32 [ 0, %39 ], [ %52, %51 ]
%45 = getelementptr inbounds i8, i8* %40, i64 %43
%46 = tail call i64 @strncmp(i8* %45, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %42
%49 = tail call i32 @strncpy(i8* %45, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%50 = add nsw i32 %44, 1
br label %51
51: ; preds = %42, %48
%52 = phi i32 [ %50, %48 ], [ %44, %42 ]
%53 = add nuw nsw i64 %43, 1
%54 = icmp eq i64 %53, %41
br i1 %54, label %55, label %42
55: ; preds = %51, %35
%56 = phi i32 [ 0, %35 ], [ %52, %51 ]
%57 = tail call i32 @munmap(i32* %30, i32 %25)
%58 = tail call i32 @close(i32 %16)
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 %56)
br label %60
60: ; preds = %33, %55, %9, %21
%61 = phi i32 [ 10, %21 ], [ 10, %9 ], [ 10, %33 ], [ 0, %55 ]
br label %62
62: ; preds = %60, %5
%63 = phi i32 [ 10, %5 ], [ %61, %60 ]
ret i32 %63
}