#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 *strstr(char *, char const *);
char *find_sentence(char *buf, int bsz, const char *prfx) {
  char *bp = NULL, *ep = NULL;
  buf[bsz - 1] = '\0';
  if (prfx == NULL)
    bp = strstr(buf, "$");
  else
    bp = strstr(buf, prfx);
  if (bp == NULL)
    return NULL;
  ep = strstr(bp, "\r\n");
  if (ep == NULL)
    return NULL;
  if (ep + 2 - buf < bsz)
    ep[2] = '\0';
  else
    return NULL;
  return bp;
}


x86 assembly (clang, -O0):
<find_sentence>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpq   $0x0,-0x20(%rbp)
jne    1168 <find_sentence+0x58>
mov    -0x10(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x28(%rbp)
jmp    1179 <find_sentence+0x69>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1191 <find_sentence+0x81>
movq   $0x0,-0x8(%rbp)
jmp    11fb <find_sentence+0xeb>
mov    -0x28(%rbp),%rdi
lea    0xe66(%rip),%rsi
call   1030 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11bd <find_sentence+0xad>
movq   $0x0,-0x8(%rbp)
jmp    11fb <find_sentence+0xeb>
mov    -0x30(%rbp),%rax
add    $0x2,%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jge    11e6 <find_sentence+0xd6>
mov    -0x30(%rbp),%rax
movb   $0x0,0x2(%rax)
jmp    11f3 <find_sentence+0xe3>
movq   $0x0,-0x8(%rbp)
jmp    11fb <find_sentence+0xeb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_sentence>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
movslq %esi,%r15
movb   $0x0,-0x1(%r15,%rdi,1)
test   %rdx,%rdx
je     1140 <find_sentence+0x20>
mov    %rdx,%rsi
call   1040 <strstr@plt>
jmp    114a <find_sentence+0x2a>
mov    $0x24,%esi
call   1030 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     117a <find_sentence+0x5a>
lea    0xea7(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strstr@plt>
test   %rax,%rax
je     117a <find_sentence+0x5a>
add    $0x2,%rax
mov    %rax,%rcx
sub    %r14,%rcx
cmp    %r15,%rcx
jge    117a <find_sentence+0x5a>
movb   $0x0,(%rax)
jmp    117c <find_sentence+0x5c>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"$\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i8* @find_sentence(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* null, i8** %8
store i8* null, i8** %9
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = sub nsw i32 %11, 1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %10, i64 %13
store i8 0, i8* %14
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i8*, i8** %5
%19 = call i8* @strstr(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %8
br label %24
20: ; preds = %3
%21 = load i8*, i8** %5
%22 = load i8*, i8** %7
%23 = call i8* @strstr(i8* %21, i8* %22)
store i8* %23, i8** %8
br label %24
24: ; preds = %20, %17
%25 = load i8*, i8** %8
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %28
27: ; preds = %24
store i8* null, i8** %4
br label %50
28: ; preds = %24
%29 = load i8*, i8** %8
%30 = call i8* @strstr(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %30, i8** %9
%31 = load i8*, i8** %9
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %28
store i8* null, i8** %4
br label %50
34: ; preds = %28
%35 = load i8*, i8** %9
%36 = getelementptr inbounds i8, i8* %35, i64 2
%37 = load i8*, i8** %5
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = icmp slt i64 %40, %42
br i1 %43, label %44, label %47
44: ; preds = %34
%45 = load i8*, i8** %9
%46 = getelementptr inbounds i8, i8* %45, i64 2
store i8 0, i8* %46
br label %48
47: ; preds = %34
store i8* null, i8** %4
br label %50
48: ; preds = %44
%49 = load i8*, i8** %8
store i8* %49, i8** %4
br label %50
50: ; preds = %48, %47, %33, %27
%51 = load i8*, i8** %4
ret i8* %51
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"\0D\0A\00"
define i8* @find_sentence(i8* %0, i32 %1, i8* %2) {
%4 = add nsw i32 %1, -1
%5 = sext i32 %4 to i64
%6 = getelementptr inbounds i8, i8* %0, i64 %5
store i8 0, i8* %6
%7 = icmp eq i8* %2, null
br i1 %7, label %8, label %10
8: ; preds = %3
%9 = tail call i8* @strchr(i8* dereferenceable(1) %0, i32 36)
br label %12
10: ; preds = %3
%11 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* %2)
br label %12
12: ; preds = %10, %8
%13 = phi i8* [ %9, %8 ], [ %11, %10 ]
%14 = icmp eq i8* %13, null
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = tail call i8* @strstr(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %26, label %18
18: ; preds = %15
%19 = getelementptr inbounds i8, i8* %16, i64 2
%20 = ptrtoint i8* %19 to i64
%21 = ptrtoint i8* %0 to i64
%22 = sub i64 %20, %21
%23 = sext i32 %1 to i64
%24 = icmp slt i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %18
store i8 0, i8* %19
br label %26
26: ; preds = %18, %15, %12, %25
%27 = phi i8* [ %13, %25 ], [ null, %12 ], [ null, %15 ], [ null, %18 ]
ret i8* %27
}