#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 */
char *addStrings(char *, char *);
char *doubleToString(double);
int indexOf(char *, char *, int /*<<< orphan*/, int);
int indexOfChar(char *, char, int, int);
int strlen(char *);
char *substring(char *, int, int);
char *replaceProbability(char *s, char *ngram, double newP) {
  int slen = strlen(s);
  char *withColon = addStrings(ngram, ":");
  int ngIndex = indexOf(s, withColon, 0, slen);
  if (ngIndex < 0)
    return addStrings(addStrings(s, addStrings("\n", withColon)),
                      doubleToString(newP));
  ngIndex += strlen(ngram) + 1;
  int endingIndex = indexOfChar(s, '\n', ngIndex, slen);
  if (endingIndex == -1)
    endingIndex = slen;
  char *beforeNG = substring(s, 0, ngIndex);
  char *afterNG = substring(s, endingIndex, slen);
  char *newNG = doubleToString(newP);
  char *bngng = addStrings(beforeNG, newNG);
  char *newS = addStrings(bngng, afterNG);
  return newS;
}


x86 assembly (clang, -O0):
<replaceProbability>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe74(%rip),%rsi
call   1060 <addStrings@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%ecx
xor    %edx,%edx
call   1050 <indexOf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11fb <replaceProbability+0x9b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xe3b(%rip),%rdi
call   1060 <addStrings@plt>
mov    -0x70(%rbp),%rdi
mov    %rax,%rsi
call   1060 <addStrings@plt>
mov    %rax,-0x68(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1080 <doubleToString@plt>
mov    -0x68(%rbp),%rdi
mov    %rax,%rsi
call   1060 <addStrings@plt>
mov    %rax,-0x8(%rbp)
jmp    1291 <replaceProbability+0x131>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    $0xa,%esi
call   1070 <indexOfChar@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1234 <replaceProbability+0xd4>
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x34(%rbp),%edx
xor    %esi,%esi
call   1040 <substring@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1040 <substring@plt>
mov    %rax,-0x48(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1080 <doubleToString@plt>
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x50(%rbp),%rsi
call   1060 <addStrings@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1060 <addStrings@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replaceProbability>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movsd  %xmm0,0x8(%rsp)
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%r15d
lea    0xe79(%rip),%rsi
mov    %r14,%rdi
call   1060 <addStrings@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %rax,%rsi
xor    %edx,%edx
mov    %r15d,%ecx
call   1050 <indexOf@plt>
test   %eax,%eax
js     1212 <replaceProbability+0xb2>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rax,%rbp,1),%r14d
add    $0x1,%r14d
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
call   1070 <indexOfChar@plt>
cmp    $0xffffffff,%eax
mov    %r15d,%ebp
cmovne %eax,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1040 <substring@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
call   1040 <substring@plt>
mov    %rax,%rbx
movsd  0x8(%rsp),%xmm0
call   1080 <doubleToString@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1060 <addStrings@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
jmp    1240 <replaceProbability+0xe0>
lea    0xde9(%rip),%rdi
mov    %r12,%rsi
call   1060 <addStrings@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <addStrings@plt>
mov    %rax,%rbx
movsd  0x8(%rsp),%xmm0
call   1080 <doubleToString@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <addStrings@plt>

LLVM IR (-O0):
@.str = [2 x i8] c":\00"
@.str.1 = [2 x i8] c"\0A\00"
define i8* @replaceProbability(i8* %0, i8* %1, double %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca double
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store double %2, double* %7
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %8
%19 = load i8*, i8** %6
%20 = call i8* @addStrings(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %20, i8** %9
%21 = load i8*, i8** %5
%22 = load i8*, i8** %9
%23 = load i32, i32* %8
%24 = call i32 @indexOf(i8* %21, i8* %22, i32 0, i32 %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %3
%28 = load i8*, i8** %5
%29 = load i8*, i8** %9
%30 = call i8* @addStrings(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = call i8* @addStrings(i8* %28, i8* %30)
%32 = load double, double* %7
%33 = call i8* @doubleToString(double %32)
%34 = call i8* @addStrings(i8* %31, i8* %33)
store i8* %34, i8** %4
br label %66
35: ; preds = %3
%36 = load i8*, i8** %6
%37 = call i32 @strlen(i8* %36)
%38 = add nsw i32 %37, 1
%39 = load i32, i32* %10
%40 = add nsw i32 %39, %38
store i32 %40, i32* %10
%41 = load i8*, i8** %5
%42 = load i32, i32* %10
%43 = load i32, i32* %8
%44 = call i32 @indexOfChar(i8* %41, i8 signext 10, i32 %42, i32 %43)
store i32 %44, i32* %11
%45 = load i32, i32* %11
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %35
%48 = load i32, i32* %8
store i32 %48, i32* %11
br label %49
49: ; preds = %47, %35
%50 = load i8*, i8** %5
%51 = load i32, i32* %10
%52 = call i8* @substring(i8* %50, i32 0, i32 %51)
store i8* %52, i8** %12
%53 = load i8*, i8** %5
%54 = load i32, i32* %11
%55 = load i32, i32* %8
%56 = call i8* @substring(i8* %53, i32 %54, i32 %55)
store i8* %56, i8** %13
%57 = load double, double* %7
%58 = call i8* @doubleToString(double %57)
store i8* %58, i8** %14
%59 = load i8*, i8** %12
%60 = load i8*, i8** %14
%61 = call i8* @addStrings(i8* %59, i8* %60)
store i8* %61, i8** %15
%62 = load i8*, i8** %15
%63 = load i8*, i8** %13
%64 = call i8* @addStrings(i8* %62, i8* %63)
store i8* %64, i8** %16
%65 = load i8*, i8** %16
store i8* %65, i8** %4
br label %66
66: ; preds = %49, %27
%67 = load i8*, i8** %4
ret i8* %67
}

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
@.str.1 = [2 x i8] c"\0A\00"
define i8* @replaceProbability(i8* %0, i8* %1, double %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i8* @addStrings(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @indexOf(i8* %0, i8* %5, i32 0, i32 %4)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = tail call i8* @addStrings(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%10 = tail call i8* @addStrings(i8* %0, i8* %9)
%11 = tail call i8* @doubleToString(double %2)
%12 = tail call i8* @addStrings(i8* %10, i8* %11)
br label %25
13: ; preds = %3
%14 = tail call i32 @strlen(i8* %1)
%15 = add nuw i32 %6, 1
%16 = add i32 %15, %14
%17 = tail call i32 @indexOfChar(i8* %0, i8 signext 10, i32 %16, i32 %4)
%18 = icmp eq i32 %17, -1
%19 = select i1 %18, i32 %4, i32 %17
%20 = tail call i8* @substring(i8* %0, i32 0, i32 %16)
%21 = tail call i8* @substring(i8* %0, i32 %19, i32 %4)
%22 = tail call i8* @doubleToString(double %2)
%23 = tail call i8* @addStrings(i8* %20, i8* %22)
%24 = tail call i8* @addStrings(i8* %23, i8* %21)
br label %25
25: ; preds = %13, %8
%26 = phi i8* [ %12, %8 ], [ %24, %13 ]
ret i8* %26
}