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

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sd_die(char *, ...);
scalar_t__ strcmp(char *, char *);
char *strdup(char *);
int strrepl(char *, char *, char *);
void test_strrepl(void) {
  int n;
  char *s;
  s = strdup("\\n");
  n = strrepl(s, "\\n", "_");
  if (n != 1)
    sd_die("bad number of replacements %d\n", n);
  if (strcmp(s, "_") != 0)
    sd_die("bad replacement '%s'\n", s);
  if (strrepl(s, "\\n", "___"))
    sd_die("should exit early if given longer new\n");
  free(s);
  s = strdup("s\\ns");
  n = strrepl(s, "\\n", "_");
  if (n != 1)
    sd_die("bad number of replacements %d\n", n);
  if (strcmp(s, "s_s") != 0)
    sd_die("bad replacement '%s'\n", s);
  free(s);
  s = strdup("\\ns\\ns\\n");
  n = strrepl(s, "\\n", "_");
  if (n != 3)
    sd_die("bad number of replacements %d\n", n);
  if (strcmp(s, "_s_s_") != 0)
    sd_die("bad replacement '%s'\n", s);
  free(s);
}


x86 assembly (clang, -O0):
<test_strrepl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xf10(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xefc(%rip),%rsi
lea    0xebd(%rip),%rdx
call   1050 <strrepl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
je     119d <test_strrepl+0x4d>
mov    -0x4(%rbp),%esi
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1040 <sd_die@plt>
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     11c9 <test_strrepl+0x79>
mov    -0x10(%rbp),%rsi
lea    0xe5d(%rip),%rdi
mov    $0x0,%al
call   1040 <sd_die@plt>
mov    -0x10(%rbp),%rdi
lea    0xe9b(%rip),%rsi
lea    0xe5a(%rip),%rdx
call   1050 <strrepl@plt>
cmp    $0x0,%eax
je     11f7 <test_strrepl+0xa7>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <sd_die@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xe59(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
lea    0xe15(%rip),%rdx
call   1050 <strrepl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x1,-0x4(%rbp)
je     1245 <test_strrepl+0xf5>
mov    -0x4(%rbp),%esi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1040 <sd_die@plt>
mov    -0x10(%rbp),%rdi
lea    0xe15(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1271 <test_strrepl+0x121>
mov    -0x10(%rbp),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <sd_die@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
lea    0xde8(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdda(%rip),%rsi
lea    0xd9b(%rip),%rdx
call   1050 <strrepl@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x3,-0x4(%rbp)
je     12bf <test_strrepl+0x16f>
mov    -0x4(%rbp),%esi
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1040 <sd_die@plt>
mov    -0x10(%rbp),%rdi
lea    0xda8(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12eb <test_strrepl+0x19b>
mov    -0x10(%rbp),%rsi
lea    0xd3b(%rip),%rdi
mov    $0x0,%al
call   1040 <sd_die@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_strrepl>:
push   %r14
push   %rbx
push   %rax
lea    0xf14(%rip),%r14
mov    %r14,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
lea    0xeca(%rip),%rdx
mov    %rax,%rdi
mov    %r14,%rsi
call   1050 <strrepl@plt>
cmp    $0x1,%eax
je     118d <test_strrepl+0x3d>
lea    0xe7c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <sd_die@plt>
lea    0xea3(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11b2 <test_strrepl+0x62>
lea    0xe77(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <sd_die@plt>
lea    0xeb6(%rip),%rsi
lea    0xe75(%rip),%rdx
mov    %rbx,%rdi
call   1050 <strrepl@plt>
test   %eax,%eax
je     11da <test_strrepl+0x8a>
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1040 <sd_die@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe77(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
lea    0xe77(%rip),%rsi
lea    0xe38(%rip),%rdx
mov    %rax,%rdi
call   1050 <strrepl@plt>
cmp    $0x1,%eax
je     121c <test_strrepl+0xcc>
lea    0xded(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <sd_die@plt>
lea    0xe42(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1241 <test_strrepl+0xf1>
lea    0xde8(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <sd_die@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe19(%rip),%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
lea    0xe10(%rip),%rsi
lea    0xdd1(%rip),%rdx
mov    %rax,%rdi
call   1050 <strrepl@plt>
cmp    $0x3,%eax
je     1283 <test_strrepl+0x133>
lea    0xd86(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <sd_die@plt>
lea    0xde8(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     12a8 <test_strrepl+0x158>
lea    0xd81(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <sd_die@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"\\n\00"
@.str.1 = [2 x i8] c"_\00"
@.str.2 = [31 x i8] c"bad number of replacements %d\0A\00"
@.str.3 = [22 x i8] c"bad replacement '%s'\0A\00"
@.str.4 = [4 x i8] c"___\00"
@.str.5 = [39 x i8] c"should exit early if given longer new\0A\00"
@.str.6 = [5 x i8] c"s\\ns\00"
@.str.7 = [4 x i8] c"s_s\00"
@.str.8 = [9 x i8] c"\\ns\\ns\\n\00"
@.str.9 = [6 x i8] c"_s_s_\00"
define void @test_strrepl() {
%1 = alloca i32
%2 = alloca i8*
%3 = call i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %3, i8** %2
%4 = load i8*, i8** %2
%5 = call i32 @strrepl(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp ne i32 %6, 1
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = load i32, i32* %1
%10 = call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %8, %0
%12 = load i8*, i8** %2
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i8*, i8** %2
%17 = call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %15, %11
%19 = load i8*, i8** %2
%20 = call i32 @strrepl(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %24
24: ; preds = %22, %18
%25 = load i8*, i8** %2
%26 = call i32 @free(i8* %25)
%27 = call i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
store i8* %27, i8** %2
%28 = load i8*, i8** %2
%29 = call i32 @strrepl(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %29, i32* %1
%30 = load i32, i32* %1
%31 = icmp ne i32 %30, 1
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = load i32, i32* %1
%34 = call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %32, %24
%36 = load i8*, i8** %2
%37 = call i64 @strcmp(i8* %36, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = load i8*, i8** %2
%41 = call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %35
%43 = load i8*, i8** %2
%44 = call i32 @free(i8* %43)
%45 = call i8* @strdup(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
store i8* %45, i8** %2
%46 = load i8*, i8** %2
%47 = call i32 @strrepl(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 %47, i32* %1
%48 = load i32, i32* %1
%49 = icmp ne i32 %48, 3
br i1 %49, label %50, label %53
50: ; preds = %42
%51 = load i32, i32* %1
%52 = call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %50, %42
%54 = load i8*, i8** %2
%55 = call i64 @strcmp(i8* %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %53
%58 = load i8*, i8** %2
%59 = call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %57, %53
%61 = load i8*, i8** %2
%62 = call i32 @free(i8* %61)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"\\n\00"
@.str.1 = [2 x i8] c"_\00"
@.str.2 = [31 x i8] c"bad number of replacements %d\0A\00"
@.str.3 = [22 x i8] c"bad replacement '%s'\0A\00"
@.str.4 = [4 x i8] c"___\00"
@.str.5 = [39 x i8] c"should exit early if given longer new\0A\00"
@.str.6 = [5 x i8] c"s\\ns\00"
@.str.7 = [4 x i8] c"s_s\00"
@.str.8 = [9 x i8] c"\\ns\\ns\\n\00"
@.str.9 = [6 x i8] c"_s_s_\00"
define void @test_strrepl() {
%1 = tail call dereferenceable_or_null(3) i8* @strdup(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%2 = tail call i32 @strrepl(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32 %2, 1
br i1 %3, label %6, label %4
4: ; preds = %0
%5 = tail call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %2)
br label %6
6: ; preds = %4, %0
%7 = tail call i64 @strcmp(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %11
11: ; preds = %9, %6
%12 = tail call i32 @strrepl(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = tail call i32 @free(i8* %1)
%18 = tail call dereferenceable_or_null(5) i8* @strdup(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%19 = tail call i32 @strrepl(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32 %19, 1
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = tail call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %19)
br label %23
23: ; preds = %21, %16
%24 = tail call i64 @strcmp(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = tail call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %18)
br label %28
28: ; preds = %26, %23
%29 = tail call i32 @free(i8* %18)
%30 = tail call dereferenceable_or_null(9) i8* @strdup(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%31 = tail call i32 @strrepl(i8* %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = icmp eq i32 %31, 3
br i1 %32, label %35, label %33
33: ; preds = %28
%34 = tail call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %31)
br label %35
35: ; preds = %33, %28
%36 = tail call i64 @strcmp(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = tail call i32 (i8*, ...) @sd_die(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %40
40: ; preds = %38, %35
%41 = tail call i32 @free(i8* %30)
ret void
}