#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 timeval {
  int tv_sec;
  int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ matchPrompt(char *);
int /*<<< orphan*/ negotiate(int, unsigned char *, int);
int recv(int, unsigned char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ *strstr(char *, char *);
int readUntil(int fd, char *toFind, int matchLePrompt, int timeout,
              int timeoutusec, char *buffer, int bufSize, int initialIndex) {
  int bufferUsed = initialIndex, got = 0, found = 0;
  fd_set myset;
  struct timeval tv;
  tv.tv_sec = timeout;
  tv.tv_usec = timeoutusec;
  unsigned char *initialRead = NULL;
  while (bufferUsed + 2 < bufSize && (tv.tv_sec > 0 || tv.tv_usec > 0)) {
    FD_ZERO(&myset);
    FD_SET(fd, &myset);
    if (select(fd + 1, &myset, NULL, NULL, &tv) < 1)
      break;
    initialRead = buffer + bufferUsed;
    got = recv(fd, initialRead, 1, 0);
    if (got == -1 || got == 0)
      return 0;
    bufferUsed += got;
    if (*initialRead == 0xFF) {
      got = recv(fd, initialRead + 1, 2, 0);
      if (got == -1 || got == 0)
        return 0;
      bufferUsed += got;
      if (!negotiate(fd, initialRead, 3))
        return 0;
    } else {
      if (strstr(buffer, toFind) != NULL ||
          (matchLePrompt && matchPrompt(buffer))) {
        found = 1;
        break;
      }
    }
  }
  if (found)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<readUntil>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9,-0x28(%rbp)
mov    0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x2c(%rbp),%ecx
add    $0x2,%ecx
xor    %eax,%eax
cmp    0x10(%rbp),%ecx
mov    %al,-0x49(%rbp)
jge    11ee <readUntil+0x7e>
mov    $0x1,%al
cmpl   $0x0,-0x40(%rbp)
mov    %al,-0x4a(%rbp)
jg     11e8 <readUntil+0x78>
cmpl   $0x0,-0x3c(%rbp)
setg   %al
mov    %al,-0x4a(%rbp)
mov    -0x4a(%rbp),%al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11fe <readUntil+0x8e>
jmp    1353 <readUntil+0x1e3>
lea    -0x38(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x1,%eax
jge    123b <readUntil+0xcb>
jmp    1353 <readUntil+0x1e3>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
je     1274 <readUntil+0x104>
cmpl   $0x0,-0x30(%rbp)
jne    1280 <readUntil+0x110>
movl   $0x0,-0x4(%rbp)
jmp    1370 <readUntil+0x200>
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x48(%rbp),%rax
movzbl (%rax),%eax
cmp    $0xff,%eax
jne    1309 <readUntil+0x199>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rsi
add    $0x1,%rsi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
je     12c9 <readUntil+0x159>
cmpl   $0x0,-0x30(%rbp)
jne    12d5 <readUntil+0x165>
movl   $0x0,-0x4(%rbp)
jmp    1370 <readUntil+0x200>
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    $0x3,%edx
call   1070 <negotiate@plt>
cmp    $0x0,%eax
jne    1304 <readUntil+0x194>
movl   $0x0,-0x4(%rbp)
jmp    1370 <readUntil+0x200>
jmp    134e <readUntil+0x1de>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
jne    133d <readUntil+0x1cd>
cmpl   $0x0,-0x14(%rbp)
je     1349 <readUntil+0x1d9>
mov    -0x28(%rbp),%rdi
call   1060 <matchPrompt@plt>
cmp    $0x0,%rax
je     1349 <readUntil+0x1d9>
movl   $0x1,-0x34(%rbp)
jmp    1353 <readUntil+0x1e3>
jmp    134e <readUntil+0x1de>
jmp    11bb <readUntil+0x4b>
cmpl   $0x0,-0x34(%rbp)
je     1369 <readUntil+0x1f9>
movl   $0x1,-0x4(%rbp)
jmp    1370 <readUntil+0x200>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readUntil>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,0xc(%rsp)
mov    %rsi,0x10(%rsp)
mov    0x68(%rsp),%r13d
mov    %ecx,(%rsp)
mov    %r8d,0x4(%rsp)
lea    0x2(%r13),%eax
xor    %r14d,%r14d
cmp    0x60(%rsp),%eax
jge    12e3 <readUntil+0x173>
mov    %edi,%ebp
mov    %r9,0x18(%rsp)
test   %ecx,%ecx
jg     11b9 <readUntil+0x49>
test   %r8d,%r8d
jle    12e3 <readUntil+0x173>
lea    0x1(%rbp),%eax
mov    %eax,0x8(%rsp)
lea    0x24(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
xor    %r14d,%r14d
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rsp,%r8
call   1050 <select@plt>
test   %eax,%eax
jle    12e3 <readUntil+0x173>
movslq %r13d,%r12
mov    %rbp,%rdi
mov    0x18(%rsp),%rbp
add    %rbp,%r12
mov    %rdi,%rbx
mov    %r12,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r15d
lea    0x1(%r15),%eax
cmp    $0x2,%eax
jb     12e0 <readUntil+0x170>
add    %r13d,%r15d
cmpb   $0xff,(%r12)
je     1270 <readUntil+0x100>
mov    %rbp,%rdi
mov    0x10(%rsp),%rsi
call   1090 <strstr@plt>
mov    $0x1,%r14d
test   %rax,%rax
jne    12e3 <readUntil+0x173>
cmpl   $0x0,0xc(%rsp)
je     12b0 <readUntil+0x140>
mov    %rbp,%rdi
call   1060 <matchPrompt@plt>
test   %rax,%rax
mov    %rbx,%rbp
je     12b3 <readUntil+0x143>
jmp    12e3 <readUntil+0x173>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0x1(%r12),%rsi
xor    %r14d,%r14d
mov    %rbx,%rbp
mov    %ebp,%edi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r13d
lea    0x1(%r13),%eax
cmp    $0x2,%eax
jb     12e3 <readUntil+0x173>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x3,%edx
call   1070 <negotiate@plt>
test   %eax,%eax
je     12e3 <readUntil+0x173>
add    %r15d,%r13d
mov    %r13d,%r15d
jmp    12b3 <readUntil+0x143>
mov    %rbx,%rbp
lea    0x2(%r15),%eax
cmp    0x60(%rsp),%eax
jge    12e0 <readUntil+0x170>
cmpl   $0x0,(%rsp)
mov    %r15d,%r13d
jg     11c0 <readUntil+0x50>
cmpl   $0x0,0x4(%rsp)
mov    %r15d,%r13d
mov    $0x0,%r14d
jg     11c0 <readUntil+0x50>
jmp    12e3 <readUntil+0x173>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @readUntil(i32 %0, i8* %1, i32 %2, i32 %3, i32 %4, i8* %5, i32 %6, i32 %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca %struct.timeval
%23 = alloca i8*
store i32 %0, i32* %10
store i8* %1, i8** %11
store i32 %2, i32* %12
store i32 %3, i32* %13
store i32 %4, i32* %14
store i8* %5, i8** %15
store i32 %6, i32* %16
store i32 %7, i32* %17
%24 = load i32, i32* %17
store i32 %24, i32* %18
store i32 0, i32* %19
store i32 0, i32* %20
%25 = load i32, i32* %13
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %14
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
store i32 %27, i32* %28
store i8* null, i8** %23
br label %29
29: ; preds = %112, %8
%30 = load i32, i32* %18
%31 = add nsw i32 %30, 2
%32 = load i32, i32* %16
%33 = icmp slt i32 %31, %32
br i1 %33, label %34, label %44
34: ; preds = %29
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 0
%36 = load i32, i32* %35
%37 = icmp sgt i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 1
%40 = load i32, i32* %39
%41 = icmp sgt i32 %40, 0
br label %42
42: ; preds = %38, %34
%43 = phi i1 [ true, %34 ], [ %41, %38 ]
br label %44
44: ; preds = %42, %29
%45 = phi i1 [ false, %29 ], [ %43, %42 ]
br i1 %45, label %46, label %113
46: ; preds = %44
%47 = call i32 @FD_ZERO(i32* %21)
%48 = load i32, i32* %10
%49 = call i32 @FD_SET(i32 %48, i32* %21)
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
%52 = call i32 @select(i32 %51, i32* %21, i32* null, i32* null, %struct.timeval* %22)
%53 = icmp slt i32 %52, 1
br i1 %53, label %54, label %55
54: ; preds = %46
br label %113
55: ; preds = %46
%56 = load i8*, i8** %15
%57 = load i32, i32* %18
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %56, i64 %58
store i8* %59, i8** %23
%60 = load i32, i32* %10
%61 = load i8*, i8** %23
%62 = call i32 @recv(i32 %60, i8* %61, i32 1, i32 0)
store i32 %62, i32* %19
%63 = load i32, i32* %19
%64 = icmp eq i32 %63, -1
br i1 %64, label %68, label %65
65: ; preds = %55
%66 = load i32, i32* %19
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %65, %55
store i32 0, i32* %9
br label %118
69: ; preds = %65
%70 = load i32, i32* %19
%71 = load i32, i32* %18
%72 = add nsw i32 %71, %70
store i32 %72, i32* %18
%73 = load i8*, i8** %23
%74 = load i8, i8* %73
%75 = zext i8 %74 to i32
%76 = icmp eq i32 %75, 255
br i1 %76, label %77, label %98
77: ; preds = %69
%78 = load i32, i32* %10
%79 = load i8*, i8** %23
%80 = getelementptr inbounds i8, i8* %79, i64 1
%81 = call i32 @recv(i32 %78, i8* %80, i32 2, i32 0)
store i32 %81, i32* %19
%82 = load i32, i32* %19
%83 = icmp eq i32 %82, -1
br i1 %83, label %87, label %84
84: ; preds = %77
%85 = load i32, i32* %19
%86 = icmp eq i32 %85, 0
br i1 %86, label %87, label %88
87: ; preds = %84, %77
store i32 0, i32* %9
br label %118
88: ; preds = %84
%89 = load i32, i32* %19
%90 = load i32, i32* %18
%91 = add nsw i32 %90, %89
store i32 %91, i32* %18
%92 = load i32, i32* %10
%93 = load i8*, i8** %23
%94 = call i32 @negotiate(i32 %92, i8* %93, i32 3)
%95 = icmp ne i32 %94, 0
br i1 %95, label %97, label %96
96: ; preds = %88
store i32 0, i32* %9
br label %118
97: ; preds = %88
br label %112
98: ; preds = %69
%99 = load i8*, i8** %15
%100 = load i8*, i8** %11
%101 = call i32* @strstr(i8* %99, i8* %100)
%102 = icmp ne i32* %101, null
br i1 %102, label %110, label %103
103: ; preds = %98
%104 = load i32, i32* %12
%105 = icmp ne i32 %104, 0
br i1 %105, label %106, label %111
106: ; preds = %103
%107 = load i8*, i8** %15
%108 = call i64 @matchPrompt(i8* %107)
%109 = icmp ne i64 %108, 0
br i1 %109, label %110, label %111
110: ; preds = %106, %98
store i32 1, i32* %20
br label %113
111: ; preds = %106, %103
br label %112
112: ; preds = %111, %97
br label %29
113: ; preds = %110, %54, %44
%114 = load i32, i32* %20
%115 = icmp ne i32 %114, 0
br i1 %115, label %116, label %117
116: ; preds = %113
store i32 1, i32* %9
br label %118
117: ; preds = %113
store i32 0, i32* %9
br label %118
118: ; preds = %117, %116, %96, %87, %68
%119 = load i32, i32* %9
ret i32 %119
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @readUntil(i32 %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4, i8* %5, i32 %6, i32 %7) {
%9 = alloca i32
%10 = alloca %struct.timeval
%11 = bitcast i32* %9 to i8*
%12 = bitcast %struct.timeval* %10 to i8*
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i64 0, i32 0
store i32 %3, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i64 0, i32 1
store i32 %4, i32* %14
%15 = add nsw i32 %7, 2
%16 = icmp slt i32 %15, %6
br i1 %16, label %17, label %65
17: ; preds = %8
%18 = add nsw i32 %0, 1
%19 = icmp eq i32 %2, 0
%20 = icmp sgt i32 %3, 0
%21 = icmp sgt i32 %4, 0
%22 = or i1 %20, %21
br i1 %22, label %23, label %65
23: ; preds = %17, %59
%24 = phi i32 [ %56, %59 ], [ %7, %17 ]
%25 = call i32 @FD_ZERO(i32* %9)
%26 = call i32 @FD_SET(i32 %0, i32* %9)
%27 = call i32 @select(i32 %18, i32* %9, i32* null, i32* null, %struct.timeval* %10)
%28 = icmp slt i32 %27, 1
br i1 %28, label %65, label %29
29: ; preds = %23
%30 = sext i32 %24 to i64
%31 = getelementptr inbounds i8, i8* %5, i64 %30
%32 = call i32 @recv(i32 %0, i8* %31, i32 1, i32 0)
%33 = add i32 %32, 1
%34 = icmp ult i32 %33, 2
br i1 %34, label %65, label %35
35: ; preds = %29
%36 = add nsw i32 %32, %24
%37 = load i8, i8* %31
%38 = icmp eq i8 %37, -1
br i1 %38, label %39, label %48
39: ; preds = %35
%40 = getelementptr inbounds i8, i8* %31, i64 1
%41 = call i32 @recv(i32 %0, i8* %40, i32 2, i32 0)
%42 = add i32 %41, 1
%43 = icmp ult i32 %42, 2
br i1 %43, label %65, label %44
44: ; preds = %39
%45 = add nsw i32 %41, %36
%46 = call i32 @negotiate(i32 %0, i8* %31, i32 3)
%47 = icmp eq i32 %46, 0
br i1 %47, label %65, label %55
48: ; preds = %35
%49 = call i32* @strstr(i8* dereferenceable(1) %5, i8* dereferenceable(1) %1)
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %65
51: ; preds = %48
br i1 %19, label %55, label %52
52: ; preds = %51
%53 = call i64 @matchPrompt(i8* %5)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %51, %52, %44
%56 = phi i32 [ %45, %44 ], [ %36, %52 ], [ %36, %51 ]
%57 = add nsw i32 %56, 2
%58 = icmp slt i32 %57, %6
br i1 %58, label %59, label %65
59: ; preds = %55
%60 = load i32, i32* %13
%61 = load i32, i32* %14
%62 = icmp sgt i32 %60, 0
%63 = icmp sgt i32 %61, 0
%64 = select i1 %62, i1 true, i1 %63
br i1 %64, label %23, label %65
65: ; preds = %48, %52, %55, %23, %59, %44, %39, %29, %17, %8
%66 = phi i32 [ 0, %8 ], [ 0, %17 ], [ 0, %29 ], [ 0, %39 ], [ 0, %44 ], [ 0, %59 ], [ 0, %23 ], [ 0, %55 ], [ 1, %52 ], [ 1, %48 ]
ret i32 %66
}