#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 */
struct buffer {
  int dummy;
};

/* Variables and functions */
char *buffer_get_content(struct buffer *);
int /*<<< orphan*/ free(char *);
scalar_t__ str_newlines(char *, int);
char *strstr(char *, char const *);
int buffer_find_str_prev(struct buffer *buf, int from, const char *str,
                         int *newlines) {
  char *text, *s;
  int p = -1;
  text = buffer_get_content(buf);
  *newlines = 0;
  s = strstr(text, str);
  if (s) {
    while (1) {
      int len;
      s = strstr(text + p + 1, str);
      if (s) {
        len = s - (text + p);
        if (p + len < from)
          p += len;
        else
          break;
      } else {
        break;
      }
    }
    *newlines += str_newlines(text, p);
  }
  free(text);
  return p;
}


x86 assembly (clang, -O0):
<buffer_find_str_prev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <buffer_get_content@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     121e <buffer_find_str_prev+0xde>
jmp    1196 <buffer_find_str_prev+0x56>
mov    -0x28(%rbp),%rdi
movslq -0x34(%rbp),%rax
add    %rax,%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11f9 <buffer_find_str_prev+0xb9>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movslq -0x34(%rbp),%rdx
add    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ef <buffer_find_str_prev+0xaf>
mov    -0x38(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    11f4 <buffer_find_str_prev+0xb4>
jmp    1203 <buffer_find_str_prev+0xc3>
jmp    11fe <buffer_find_str_prev+0xbe>
jmp    1203 <buffer_find_str_prev+0xc3>
jmp    1196 <buffer_find_str_prev+0x56>
mov    -0x28(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1050 <str_newlines@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq (%rax),%rcx
add    %rdx,%rcx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_find_str_prev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r12d
call   1040 <buffer_get_content@plt>
mov    %rax,%r13
movl   $0x0,(%r14)
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11b5 <buffer_find_str_prev+0x75>
mov    $0xffffffff,%eax
nopl   0x0(%rax,%rax,1)
mov    %eax,%ebp
movslq %eax,%rbx
lea    (%rbx,%r13,1),%rdi
add    $0x1,%rdi
mov    %r15,%rsi
call   1060 <strstr@plt>
test   %rax,%rax
je     11a6 <buffer_find_str_prev+0x66>
add    %r13,%rbx
sub    %ebx,%eax
add    %ebp,%eax
cmp    %r12d,%eax
jl     1180 <buffer_find_str_prev+0x40>
mov    %r13,%rdi
mov    %ebp,%esi
call   1050 <str_newlines@plt>
add    %eax,(%r14)
jmp    11ba <buffer_find_str_prev+0x7a>
mov    $0xffffffff,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
define i32 @buffer_find_str_prev(%struct.buffer* %0, i32 %1, i8* %2, i32* %3) {
%5 = alloca %struct.buffer*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store %struct.buffer* %0, %struct.buffer** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32* %3, i32** %8
store i32 -1, i32* %11
%13 = load %struct.buffer*, %struct.buffer** %5
%14 = call i8* @buffer_get_content(%struct.buffer* %13)
store i8* %14, i8** %9
%15 = load i32*, i32** %8
store i32 0, i32* %15
%16 = load i8*, i8** %9
%17 = load i8*, i8** %7
%18 = call i8* @strstr(i8* %16, i8* %17)
store i8* %18, i8** %10
%19 = load i8*, i8** %10
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %64
21: ; preds = %4
br label %22
22: ; preds = %21, %54
%23 = load i8*, i8** %9
%24 = load i32, i32* %11
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8*, i8** %7
%29 = call i8* @strstr(i8* %27, i8* %28)
store i8* %29, i8** %10
%30 = load i8*, i8** %10
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %53
32: ; preds = %22
%33 = load i8*, i8** %10
%34 = load i8*, i8** %9
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = ptrtoint i8* %33 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = trunc i64 %40 to i32
store i32 %41, i32* %12
%42 = load i32, i32* %11
%43 = load i32, i32* %12
%44 = add nsw i32 %42, %43
%45 = load i32, i32* %6
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %32
%48 = load i32, i32* %12
%49 = load i32, i32* %11
%50 = add nsw i32 %49, %48
store i32 %50, i32* %11
br label %52
51: ; preds = %32
br label %55
52: ; preds = %47
br label %54
53: ; preds = %22
br label %55
54: ; preds = %52
br label %22
55: ; preds = %53, %51
%56 = load i8*, i8** %9
%57 = load i32, i32* %11
%58 = call i64 @str_newlines(i8* %56, i32 %57)
%59 = load i32*, i32** %8
%60 = load i32, i32* %59
%61 = sext i32 %60 to i64
%62 = add nsw i64 %61, %58
%63 = trunc i64 %62 to i32
store i32 %63, i32* %59
br label %64
64: ; preds = %55, %4
%65 = load i8*, i8** %9
%66 = call i32 @free(i8* %65)
%67 = load i32, i32* %11
ret i32 %67
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
define i32 @buffer_find_str_prev(%struct.buffer* %0, i32 %1, i8* nocapture %2, i32* nocapture %3) {
%5 = tail call i8* @buffer_get_content(%struct.buffer* %0)
store i32 0, i32* %3
%6 = tail call i8* @strstr(i8* dereferenceable(1) %5, i8* dereferenceable(1) %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %27, label %8
8: ; preds = %4, %15
%9 = phi i32 [ %20, %15 ], [ -1, %4 ]
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %5, i64 %10
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = tail call i8* @strstr(i8* %12, i8* dereferenceable(1) %2)
%14 = icmp eq i8* %13, null
br i1 %14, label %22, label %15
15: ; preds = %8
%16 = ptrtoint i8* %13 to i64
%17 = ptrtoint i8* %11 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = add nsw i32 %9, %19
%21 = icmp slt i32 %20, %1
br i1 %21, label %8, label %22
22: ; preds = %15, %8
%23 = tail call i64 @str_newlines(i8* %5, i32 %9)
%24 = load i32, i32* %3
%25 = trunc i64 %23 to i32
%26 = add i32 %24, %25
store i32 %26, i32* %3
br label %27
27: ; preds = %22, %4
%28 = phi i32 [ %9, %22 ], [ -1, %4 ]
%29 = tail call i32 @free(i8* %5)
ret i32 %28
}