#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;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ memcmp(int /*<<< orphan*/ *, int /*<<< orphan*/ const *, int const);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          int const);
int /*<<< orphan*/ stderr;
void search_and_replace(uint8_t *target, const int target_size,
                        const uint8_t *search, const int search_size,
                        const uint8_t *replace) {
  int i;
  for (i = 0; i < target_size; i++) {
    if (memcmp(&target[i], search, search_size) == 0) {
      memcpy(&target[i], replace, search_size);
      return;
    }
  }
  fprintf(stderr, "Unable to find the default mode in order to replace it.\n");
  fprintf(stderr, "It probably means you replace the mode already.\n");
  exit(1);
}


x86 assembly (clang, -O0):
<search_and_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11c6 <search_and_replace+0x86>
mov    -0x8(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    11b3 <search_and_replace+0x73>
mov    -0x8(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
add    $0x30,%rsp
pop    %rbp
ret
jmp    11b8 <search_and_replace+0x78>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1161 <search_and_replace+0x21>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe4e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<search_and_replace>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %esi,%esi
jle    117d <search_and_replace+0x3d>
mov    %r8,%r14
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rdi,%rbp
mov    %esi,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
je     11aa <search_and_replace+0x6a>
add    $0x4,%rbp
add    $0xffffffffffffffff,%rbx
jne    1160 <search_and_replace+0x20>
mov    0x2e5c(%rip),%rbx
mov    (%rbx),%edi
lea    0xe73(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe9e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <memcpy@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [57 x i8] c"Unable to find the default mode in order to replace it.\0A\00"
@.str.1 = [49 x i8] c"It probably means you replace the mode already.\0A\00"
define void @search_and_replace(i32* %0, i32 %1, i32* %2, i32 %3, i32* %4) {
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32* %4, i32** %10
store i32 0, i32* %11
br label %12
12: ; preds = %34, %5
%13 = load i32, i32* %11
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %37
16: ; preds = %12
%17 = load i32*, i32** %6
%18 = load i32, i32* %11
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %17, i64 %19
%21 = load i32*, i32** %8
%22 = load i32, i32* %9
%23 = call i64 @memcmp(i32* %20, i32* %21, i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %16
%26 = load i32*, i32** %6
%27 = load i32, i32* %11
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32, i32* %26, i64 %28
%30 = load i32*, i32** %10
%31 = load i32, i32* %9
%32 = call i32 @memcpy(i32* %29, i32* %30, i32 %31)
ret void
33: ; preds = %16
br label %34
34: ; preds = %33
%35 = load i32, i32* %11
%36 = add nsw i32 %35, 1
store i32 %36, i32* %11
br label %12
37: ; preds = %12
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [57 x i8] c"Unable to find the default mode in order to replace it.\0A\00"
@.str.1 = [49 x i8] c"It probably means you replace the mode already.\0A\00"
define void @search_and_replace(i32* %0, i32 %1, i32* %2, i32 %3, i32* %4) {
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %19
7: ; preds = %5
%8 = zext i32 %1 to i64
br label %11
9: ; preds = %11
%10 = icmp eq i64 %16, %8
br i1 %10, label %19, label %11
11: ; preds = %7, %9
%12 = phi i64 [ 0, %7 ], [ %16, %9 ]
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = tail call i64 @memcmp(i32* %13, i32* %2, i32 %3)
%15 = icmp eq i64 %14, 0
%16 = add nuw nsw i64 %12, 1
br i1 %15, label %17, label %9
17: ; preds = %11
%18 = tail call i32 @memcpy(i32* %13, i32* %4, i32 %3)
ret void
19: ; preds = %9, %5
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
}