#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ StrConcat(char *, char *);
size_t StrGetLength(char const *);
int /*<<< orphan*/ StrNConcat(char *, char const *, size_t);
char *StrSearch(char *, char const *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int DoReplace(const char *pcString1, const char *pcString2) {
  char buf[1023 + 2];
  char *pc_buf = buf;
  char new_string[1023 + 2];
  char *pcnew_string = new_string;
  char *pcFind;
  size_t len, len1, len2;
  int ifFind;
  len1 = StrGetLength(pcString1);
  len2 = StrGetLength(pcString2);
  if (len1 > 1023 || len2 > 1023) {
    fprintf(stderr, "Error: argument is too long");
    return 0;
  }
  if (len1 == 0) {
    fprintf(stderr, "Error: Can't replace an empty substring");
    return 0;
  }
  while (fgets(buf, sizeof(buf), stdin)) {
    if ((len = StrGetLength(buf)) > 1023) {
      fprintf(stderr, "Error: input line is too long\n");
      return 0;
    }
    pc_buf = buf;
    *new_string = '\0';
    pcnew_string = new_string;
    ifFind = 0;
    while (*pc_buf != '\0') {
      if (pcFind = StrSearch(pc_buf, pcString1)) {
        StrNConcat(pcnew_string, pc_buf, pcFind - pc_buf);
        pcnew_string = pcnew_string + (pcFind - pc_buf);
        StrNConcat(pcnew_string, pcString2, len2);
        pcnew_string = pcnew_string + len2;
        pc_buf = pcFind + len1;
        ifFind = 1;
      } else {
        StrConcat(pcnew_string, pc_buf);
        break;
      }
    }
    if (ifFind) {
      fprintf(stdout, "%s", new_string);
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<DoReplace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x830(%rbp),%rax
mov    %rax,-0x838(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <StrGetLength@plt>
mov    %rax,-0x850(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <StrGetLength@plt>
mov    %rax,-0x858(%rbp)
cmpq   $0x3ff,-0x850(%rbp)
ja     11d1 <DoReplace+0x71>
cmpq   $0x3ff,-0x858(%rbp)
jbe    11f4 <DoReplace+0x94>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    0xe1f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13db <DoReplace+0x27b>
cmpq   $0x0,-0x850(%rbp)
jne    1225 <DoReplace+0xc5>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13db <DoReplace+0x27b>
jmp    122a <DoReplace+0xca>
lea    -0x420(%rbp),%rdi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
mov    $0x401,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     13d4 <DoReplace+0x274>
lea    -0x420(%rbp),%rdi
call   1040 <StrGetLength@plt>
mov    %rax,-0x848(%rbp)
cmp    $0x3ff,%rax
jbe    1290 <DoReplace+0x130>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13db <DoReplace+0x27b>
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
movb   $0x0,-0x830(%rbp)
lea    -0x830(%rbp),%rax
mov    %rax,-0x838(%rbp)
movl   $0x0,-0x85c(%rbp)
mov    -0x428(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     13a4 <DoReplace+0x244>
mov    -0x428(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <StrSearch@plt>
mov    %rax,-0x840(%rbp)
cmp    $0x0,%rax
je     1387 <DoReplace+0x227>
mov    -0x838(%rbp),%rdi
mov    -0x428(%rbp),%rsi
mov    -0x840(%rbp),%rdx
mov    -0x428(%rbp),%rax
sub    %rax,%rdx
call   1080 <StrNConcat@plt>
mov    -0x838(%rbp),%rax
mov    -0x840(%rbp),%rcx
mov    -0x428(%rbp),%rdx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x838(%rbp)
mov    -0x838(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x858(%rbp),%rdx
call   1080 <StrNConcat@plt>
mov    -0x838(%rbp),%rax
add    -0x858(%rbp),%rax
mov    %rax,-0x838(%rbp)
mov    -0x840(%rbp),%rax
add    -0x850(%rbp),%rax
mov    %rax,-0x428(%rbp)
movl   $0x1,-0x85c(%rbp)
jmp    139f <DoReplace+0x23f>
mov    -0x838(%rbp),%rdi
mov    -0x428(%rbp),%rsi
call   1070 <StrConcat@plt>
jmp    13a4 <DoReplace+0x244>
jmp    12bd <DoReplace+0x15d>
cmpl   $0x0,-0x85c(%rbp)
je     13cf <DoReplace+0x26f>
mov    0x2c18(%rip),%rax
mov    (%rax),%edi
lea    -0x830(%rbp),%rdx
lea    0xc9b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    122a <DoReplace+0xca>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DoReplace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x838,%rsp
mov    %rsi,%rbx
mov    %rdi,(%rsp)
call   1040 <StrGetLength@plt>
mov    %rax,%rbp
mov    %rbx,0x18(%rsp)
mov    %rbx,%rdi
call   1040 <StrGetLength@plt>
cmp    $0x3ff,%rbp
ja     12e2 <DoReplace+0x182>
mov    %rax,%rbx
cmp    $0x400,%rax
jae    12e2 <DoReplace+0x182>
test   %rbp,%rbp
je     12fb <DoReplace+0x19b>
mov    %rbp,0x8(%rsp)
jmp    11dc <DoReplace+0x7c>
nopl   0x0(%rax)
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
lea    0x20(%rsp),%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
lea    0x430(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x401,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     12f4 <DoReplace+0x194>
mov    %rbp,%rdi
call   1040 <StrGetLength@plt>
cmp    $0x400,%rax
jae    130d <DoReplace+0x1ad>
movb   $0x0,0x20(%rsp)
cmpb   $0x0,0x430(%rsp)
je     11dc <DoReplace+0x7c>
lea    0x430(%rsp),%rbp
mov    %rbp,%rdi
mov    (%rsp),%rsi
call   1030 <StrSearch@plt>
test   %rax,%rax
je     12d0 <DoReplace+0x170>
mov    %rax,0x10(%rsp)
mov    %rax,%r14
lea    0x20(%rsp),%r13
mov    %rbp,%r15
mov    0x8(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r13,%r12
mov    %r14,%r13
sub    %r15,%r13
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1080 <StrNConcat@plt>
add    %r12,%r13
mov    %r13,%rdi
mov    0x18(%rsp),%rsi
mov    %rbx,%rdx
call   1080 <StrNConcat@plt>
cmpb   $0x0,(%r14,%rbp,1)
je     11c0 <DoReplace+0x60>
add    %rbx,%r13
mov    %r14,%r15
add    %rbp,%r15
mov    %r15,%rdi
mov    (%rsp),%rsi
call   1030 <StrSearch@plt>
mov    %rax,%r14
test   %rax,%rax
jne    1260 <DoReplace+0x100>
mov    %r13,%rdi
mov    %r15,%rsi
call   1070 <StrConcat@plt>
cmpq   $0x0,0x10(%rsp)
jne    11c0 <DoReplace+0x60>
jmp    11dc <DoReplace+0x7c>
xchg   %ax,%ax
lea    0x20(%rsp),%rdi
mov    %rbp,%rsi
call   1070 <StrConcat@plt>
jmp    11dc <DoReplace+0x7c>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xd0e(%rip),%rsi
jmp    131d <DoReplace+0x1bd>
mov    $0x1,%ebx
jmp    1326 <DoReplace+0x1c6>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xd11(%rip),%rsi
jmp    131d <DoReplace+0x1bd>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    0xd27(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebx,%eax
add    $0x838,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Error: argument is too long\00"
@.str.1 = [40 x i8] c"Error: Can't replace an empty substring\00"
@stdin = global i32 0
@.str.2 = [31 x i8] c"Error: input line is too long\0A\00"
@stdout = global i32 0
@.str.3 = [3 x i8] c"%s\00"
define i32 @DoReplace(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [1025 x i8]
%7 = alloca i8*
%8 = alloca [1025 x i8]
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%15 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
store i8* %15, i8** %7
%16 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
store i8* %16, i8** %9
%17 = load i8*, i8** %4
%18 = call i64 @StrGetLength(i8* %17)
store i64 %18, i64* %12
%19 = load i8*, i8** %5
%20 = call i64 @StrGetLength(i8* %19)
store i64 %20, i64* %13
%21 = load i64, i64* %12
%22 = icmp ugt i64 %21, 1023
br i1 %22, label %26, label %23
23: ; preds = %2
%24 = load i64, i64* %13
%25 = icmp ugt i64 %24, 1023
br i1 %25, label %26, label %29
26: ; preds = %23, %2
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %102
29: ; preds = %23
%30 = load i64, i64* %12
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %102
35: ; preds = %29
br label %36
36: ; preds = %100, %35
%37 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%38 = load i32, i32* @stdin
%39 = call i64 @fgets(i8* %37, i32 1025, i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %101
41: ; preds = %36
%42 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%43 = call i64 @StrGetLength(i8* %42)
store i64 %43, i64* %11
%44 = icmp ugt i64 %43, 1023
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %3
br label %102
48: ; preds = %41
%49 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
store i8* %49, i8** %7
%50 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
store i8 0, i8* %50
%51 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
store i8* %51, i8** %9
store i32 0, i32* %14
br label %52
52: ; preds = %92, %48
%53 = load i8*, i8** %7
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %93
57: ; preds = %52
%58 = load i8*, i8** %7
%59 = load i8*, i8** %4
%60 = call i8* @StrSearch(i8* %58, i8* %59)
store i8* %60, i8** %10
%61 = icmp ne i8* %60, null
br i1 %61, label %62, label %88
62: ; preds = %57
%63 = load i8*, i8** %9
%64 = load i8*, i8** %7
%65 = load i8*, i8** %10
%66 = load i8*, i8** %7
%67 = ptrtoint i8* %65 to i64
%68 = ptrtoint i8* %66 to i64
%69 = sub i64 %67, %68
%70 = call i32 @StrNConcat(i8* %63, i8* %64, i64 %69)
%71 = load i8*, i8** %9
%72 = load i8*, i8** %10
%73 = load i8*, i8** %7
%74 = ptrtoint i8* %72 to i64
%75 = ptrtoint i8* %73 to i64
%76 = sub i64 %74, %75
%77 = getelementptr inbounds i8, i8* %71, i64 %76
store i8* %77, i8** %9
%78 = load i8*, i8** %9
%79 = load i8*, i8** %5
%80 = load i64, i64* %13
%81 = call i32 @StrNConcat(i8* %78, i8* %79, i64 %80)
%82 = load i8*, i8** %9
%83 = load i64, i64* %13
%84 = getelementptr inbounds i8, i8* %82, i64 %83
store i8* %84, i8** %9
%85 = load i8*, i8** %10
%86 = load i64, i64* %12
%87 = getelementptr inbounds i8, i8* %85, i64 %86
store i8* %87, i8** %7
store i32 1, i32* %14
br label %92
88: ; preds = %57
%89 = load i8*, i8** %9
%90 = load i8*, i8** %7
%91 = call i32 @StrConcat(i8* %89, i8* %90)
br label %93
92: ; preds = %62
br label %52
93: ; preds = %88, %52
%94 = load i32, i32* %14
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %100
96: ; preds = %93
%97 = load i32, i32* @stdout
%98 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%99 = call i32 (i32, i8*, ...) @fprintf(i32 %97, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %98)
br label %100
100: ; preds = %96, %93
br label %36
101: ; preds = %36
store i32 1, i32* %3
br label %102
102: ; preds = %101, %45, %32, %26
%103 = load i32, i32* %3
ret i32 %103
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Error: argument is too long\00"
@.str.1 = [40 x i8] c"Error: Can't replace an empty substring\00"
@stdin = global i32 0
@.str.2 = [31 x i8] c"Error: input line is too long\0A\00"
@stdout = global i32 0
@.str.3 = [3 x i8] c"%s\00"
define i32 @DoReplace(i8* %0, i8* %1) {
%3 = alloca [1025 x i8]
%4 = alloca [1025 x i8]
%5 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%7 = tail call i64 @StrGetLength(i8* %0)
%8 = tail call i64 @StrGetLength(i8* %1)
%9 = icmp ugt i64 %7, 1023
%10 = icmp ugt i64 %8, 1023
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %64
15: ; preds = %2
%16 = icmp eq i64 %7, 0
br i1 %16, label %21, label %17
17: ; preds = %15
%18 = load i32, i32* @stdin
%19 = call i64 @fgets(i8* %5, i32 1025, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %64, label %24
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0))
br label %64
24: ; preds = %17, %60
%25 = call i64 @StrGetLength(i8* %5)
%26 = icmp ugt i64 %25, 1023
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %64
30: ; preds = %24
store i8 0, i8* %6
%31 = load i8, i8* %5
%32 = icmp eq i8 %31, 0
br i1 %32, label %60, label %33
33: ; preds = %30
%34 = call i8* @StrSearch(i8* %5, i8* %0)
%35 = icmp eq i8* %34, null
br i1 %35, label %58, label %39
36: ; preds = %39
%37 = call i8* @StrSearch(i8* %50, i8* %0)
%38 = icmp eq i8* %37, null
br i1 %38, label %53, label %39
39: ; preds = %33, %36
%40 = phi i8* [ %37, %36 ], [ %34, %33 ]
%41 = phi i8* [ %49, %36 ], [ %6, %33 ]
%42 = phi i8* [ %50, %36 ], [ %5, %33 ]
%43 = ptrtoint i8* %40 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = call i32 @StrNConcat(i8* %41, i8* %42, i64 %45)
%47 = getelementptr inbounds i8, i8* %41, i64 %45
%48 = call i32 @StrNConcat(i8* %47, i8* %1, i64 %8)
%49 = getelementptr inbounds i8, i8* %47, i64 %8
%50 = getelementptr inbounds i8, i8* %40, i64 %7
%51 = load i8, i8* %50
%52 = icmp eq i8 %51, 0
br i1 %52, label %55, label %36
53: ; preds = %36
%54 = call i32 @StrConcat(i8* %49, i8* %50)
br i1 %35, label %60, label %55
55: ; preds = %39, %53
%56 = load i32, i32* @stdout
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %6)
br label %60
58: ; preds = %33
%59 = call i32 @StrConcat(i8* %6, i8* %5)
br label %60
60: ; preds = %58, %30, %55, %53
%61 = load i32, i32* @stdin
%62 = call i64 @fgets(i8* %5, i32 1025, i32 %61)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %24
64: ; preds = %60, %17, %27, %21, %12
%65 = phi i32 [ 0, %12 ], [ 0, %21 ], [ 0, %27 ], [ 1, %17 ], [ 1, %60 ]
ret i32 %65
}