#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*/ memset(char *, char, int);
int read(int, char *, int);
scalar_t__ strstr(char *, char *);
int stockfish_read_until(int fd, char *buff, int cap, char *terminator) {
  int i, n;
  int rc = -1;
  memset(buff, '\0', cap);
  for (i = 0; i < (cap - 1); ++i) {
    n = read(fd, buff + i, 1);
    if (n != 1) {
      goto cleanup;
    }
    if (strstr(buff, terminator)) {
      rc = i + 1;
      goto cleanup;
    }
  }
cleanup:
  return rc;
}


x86 assembly (clang, -O0):
<stockfish_read_until>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11d5 <stockfish_read_until+0xa5>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x24(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x1,-0x28(%rbp)
je     119d <stockfish_read_until+0x6d>
jmp    11da <stockfish_read_until+0xaa>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strstr@plt>
cmp    $0x0,%rax
je     11c2 <stockfish_read_until+0x92>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11da <stockfish_read_until+0xaa>
jmp    11c7 <stockfish_read_until+0x97>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1162 <stockfish_read_until+0x32>
jmp    11da <stockfish_read_until+0xaa>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stockfish_read_until>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %edx,%r13d
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    $0xffffffff,%r14d
cmp    $0x2,%r13d
jl     11a7 <stockfish_read_until+0x77>
add    $0xffffffff,%r13d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    (%rbx,%rbp,1),%rsi
mov    %r12d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11a7 <stockfish_read_until+0x77>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    11a1 <stockfish_read_until+0x71>
add    $0x1,%rbp
cmp    %rbp,%r13
jne    1170 <stockfish_read_until+0x40>
jmp    11a7 <stockfish_read_until+0x77>
add    $0x1,%ebp
mov    %ebp,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @stockfish_read_until(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
store i32 -1, i32* %11
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = call i32 @memset(i8* %12, i8 signext 0, i32 %13)
store i32 0, i32* %9
br label %15
15: ; preds = %39, %4
%16 = load i32, i32* %9
%17 = load i32, i32* %7
%18 = sub nsw i32 %17, 1
%19 = icmp slt i32 %16, %18
br i1 %19, label %20, label %42
20: ; preds = %15
%21 = load i32, i32* %5
%22 = load i8*, i8** %6
%23 = load i32, i32* %9
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = call i32 @read(i32 %21, i8* %25, i32 1)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %20
br label %43
30: ; preds = %20
%31 = load i8*, i8** %6
%32 = load i8*, i8** %8
%33 = call i64 @strstr(i8* %31, i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %11
br label %43
38: ; preds = %30
br label %39
39: ; preds = %38
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 1
store i32 %41, i32* %9
br label %15
42: ; preds = %15
br label %43
43: ; preds = %42, %35, %29
%44 = load i32, i32* %11
ret i32 %44
}

LLVM IR (-O2):
define i32 @stockfish_read_until(i32 %0, i8* %1, i32 %2, i8* %3) {
%5 = tail call i32 @memset(i8* %1, i8 signext 0, i32 %2)
%6 = icmp sgt i32 %2, 1
br i1 %6, label %7, label %24
7: ; preds = %4
%8 = add nsw i32 %2, -1
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %7, %21
%11 = phi i64 [ 0, %7 ], [ %22, %21 ]
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = tail call i32 @read(i32 %0, i8* %12, i32 1)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %24
15: ; preds = %10
%16 = tail call i64 @strstr(i8* %1, i8* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = trunc i64 %11 to i32
%20 = add nuw nsw i32 %19, 1
br label %24
21: ; preds = %15
%22 = add nuw nsw i64 %11, 1
%23 = icmp eq i64 %22, %9
br i1 %23, label %24, label %10
24: ; preds = %10, %21, %4, %18
%25 = phi i32 [ %20, %18 ], [ -1, %4 ], [ -1, %21 ], [ -1, %10 ]
ret i32 %25
}