#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 *);
int str_newlines(char *, int);
char *strstr(char *, char const *);
int buffer_find_str_next(struct buffer *buf, int from, const char *str,
                         int *newlines) {
  char *text, *start, *s;
  int p = -1;
  text = buffer_get_content(buf);
  start = text + from;
  s = strstr(start, str);
  *newlines = 0;
  if (s) {
    p = s - start;
    *newlines = str_newlines(start, p);
  }
  free(text);
  return p;
}


x86 assembly (clang, -O0):
<buffer_find_str_next>:
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,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <buffer_get_content@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0xc(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strstr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
cmpq   $0x0,-0x38(%rbp)
je     11c2 <buffer_find_str_next+0x82>
mov    -0x38(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1050 <str_newlines@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buffer_find_str_next>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r12
mov    %esi,%ebx
call   1040 <buffer_get_content@plt>
mov    %rax,%r15
movslq %ebx,%rbx
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <strstr@plt>
movl   $0x0,(%r14)
test   %rax,%rax
je     118b <buffer_find_str_next+0x4b>
mov    %rax,%r12
sub    %ebx,%r12d
mov    %rbx,%rdi
mov    %r12d,%esi
call   1050 <str_newlines@plt>
mov    %eax,(%r14)
jmp    1191 <buffer_find_str_next+0x51>
mov    $0xffffffff,%r12d
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
define i32 @buffer_find_str_next(%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 i8*
%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* %12
%13 = load %struct.buffer*, %struct.buffer** %5
%14 = call i8* @buffer_get_content(%struct.buffer* %13)
store i8* %14, i8** %9
%15 = load i8*, i8** %9
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
store i8* %18, i8** %10
%19 = load i8*, i8** %10
%20 = load i8*, i8** %7
%21 = call i8* @strstr(i8* %19, i8* %20)
store i8* %21, i8** %11
%22 = load i32*, i32** %8
store i32 0, i32* %22
%23 = load i8*, i8** %11
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %36
25: ; preds = %4
%26 = load i8*, i8** %11
%27 = load i8*, i8** %10
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = trunc i64 %30 to i32
store i32 %31, i32* %12
%32 = load i8*, i8** %10
%33 = load i32, i32* %12
%34 = call i32 @str_newlines(i8* %32, i32 %33)
%35 = load i32*, i32** %8
store i32 %34, i32* %35
br label %36
36: ; preds = %25, %4
%37 = load i8*, i8** %9
%38 = call i32 @free(i8* %37)
%39 = load i32, i32* %12
ret i32 %39
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
define i32 @buffer_find_str_next(%struct.buffer* %0, i32 %1, i8* nocapture %2, i32* nocapture writeonly %3) {
%5 = tail call i8* @buffer_get_content(%struct.buffer* %0)
%6 = sext i32 %1 to i64
%7 = getelementptr inbounds i8, i8* %5, i64 %6
%8 = tail call i8* @strstr(i8* dereferenceable(1) %7, i8* dereferenceable(1) %2)
store i32 0, i32* %3
%9 = icmp eq i8* %8, null
br i1 %9, label %16, label %10
10: ; preds = %4
%11 = ptrtoint i8* %8 to i64
%12 = ptrtoint i8* %7 to i64
%13 = sub i64 %11, %12
%14 = trunc i64 %13 to i32
%15 = tail call i32 @str_newlines(i8* %7, i32 %14)
store i32 %15, i32* %3
br label %16
16: ; preds = %10, %4
%17 = phi i32 [ %14, %10 ], [ -1, %4 ]
%18 = tail call i32 @free(i8* %5)
ret i32 %17
}