#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*/ block(int);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int read(int, char *, int);
char *realloc(char *, long);
int /*<<< orphan*/ stdout;
scalar_t__ strstr(char *, char *);
char *wait(int fd, char *search_str) {
  char buf[100 * 2];
  int bytes, offs = 0;
  int i, found = 0;
  static char *saved = NULL;
  static long saved_size;
  int buf_pos = 0;
  if (saved == NULL) {
    saved = malloc(4096);
    saved_size = 4096;
  }
  do {
    do {
      bytes = read(fd, buf + offs, 100 * 2 - offs);
      if (bytes == -1)
        block(fd);
    } while (bytes == -1);
    for (i = 0; i < bytes; i++) {
      fputc(*(buf + offs + i), stdout);
      if (buf_pos == saved_size) {
        saved = realloc(saved, saved_size * 2);
        saved_size *= 2;
      }
      if (*(buf + offs + i) != '\r')
        saved[buf_pos++] = *(buf + offs + i);
    }
    saved[buf_pos] = '\0';
    *(buf + offs + bytes) = '\0';
    if (strstr(buf, search_str))
      found = 1;
    offs += bytes;
    if (offs > 100) {
      memcpy(buf, buf + offs - 100, 100);
      offs = 0;
    }
  } while (!found);
  return saved;
}


x86 assembly (clang, -O0):
<wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0xe8(%rbp)
movl   $0x0,-0xf0(%rbp)
movl   $0x0,-0xf4(%rbp)
cmpq   $0x0,0x2eb8(%rip)
jne    11ca <wait+0x5a>
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,0x2ea1(%rip)
movq   $0x1000,0x2e9e(%rip)
jmp    11cf <wait+0x5f>
jmp    11d4 <wait+0x64>
mov    -0x4(%rbp),%edi
lea    -0xe0(%rbp),%rsi
movslq -0xe8(%rbp),%rax
add    %rax,%rsi
mov    $0xc8,%edx
sub    -0xe8(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0xe4(%rbp)
cmpl   $0xffffffff,-0xe4(%rbp)
jne    1213 <wait+0xa3>
mov    -0x4(%rbp),%edi
call   1080 <block@plt>
jmp    1218 <wait+0xa8>
cmpl   $0xffffffff,-0xe4(%rbp)
je     11d4 <wait+0x64>
movl   $0x0,-0xec(%rbp)
mov    -0xec(%rbp),%eax
cmp    -0xe4(%rbp),%eax
jge    1328 <wait+0x1b8>
lea    -0xe0(%rbp),%rax
movslq -0xe8(%rbp),%rcx
add    %rcx,%rax
movslq -0xec(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    0x2d7d(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   1030 <fputc@plt>
movslq -0xf4(%rbp),%rax
cmp    0x2ded(%rip),%rax
jne    12b1 <wait+0x141>
mov    0x2dd8(%rip),%rdi
mov    0x2dd9(%rip),%rsi
shl    $0x1,%rsi
call   1070 <realloc@plt>
mov    %rax,0x2dc1(%rip)
mov    0x2dc2(%rip),%rax
shl    $0x1,%rax
mov    %rax,0x2db7(%rip)
lea    -0xe0(%rbp),%rax
movslq -0xe8(%rbp),%rcx
add    %rcx,%rax
movslq -0xec(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
je     130f <wait+0x19f>
lea    -0xe0(%rbp),%rax
movslq -0xe8(%rbp),%rcx
add    %rcx,%rax
movslq -0xec(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    0x2d68(%rip),%rax
mov    -0xf4(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0xf4(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1314 <wait+0x1a4>
mov    -0xec(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xec(%rbp)
jmp    122f <wait+0xbf>
mov    0x2d31(%rip),%rax
movslq -0xf4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0xe0(%rbp),%rax
movslq -0xe8(%rbp),%rcx
add    %rcx,%rax
movslq -0xe4(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
lea    -0xe0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     137a <wait+0x20a>
movl   $0x1,-0xf0(%rbp)
mov    -0xe4(%rbp),%eax
add    -0xe8(%rbp),%eax
mov    %eax,-0xe8(%rbp)
cmpl   $0x64,-0xe8(%rbp)
jle    13c9 <wait+0x259>
lea    -0xe0(%rbp),%rdi
lea    -0xe0(%rbp),%rsi
movslq -0xe8(%rbp),%rax
add    %rax,%rsi
add    $0xffffffffffffff9c,%rsi
mov    $0x64,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0xe8(%rbp)
jmp    13ce <wait+0x25e>
cmpl   $0x0,-0xf0(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11cf <wait+0x5f>
mov    0x2c77(%rip),%rax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
mov    %rsi,0x10(%rsp)
mov    %edi,%ebx
cmpq   $0x0,0x2ed0(%rip)
jne    11ae <wait+0x3e>
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,0x2ebd(%rip)
movq   $0x1000,0x2eba(%rip)
xor    %r15d,%r15d
movl   $0x0,(%rsp)
mov    %ebx,0x4(%rsp)
jmp    11cd <wait+0x5d>
xchg   %ax,%ax
mov    0x4(%rsp),%ebx
test   %rbp,%rbp
jne    1319 <wait+0x1a9>
movslq %r15d,%rax
lea    (%rsp,%rax,1),%rbp
add    $0x20,%rbp
mov    $0xc8,%r13d
sub    %eax,%r13d
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r13d,%edx
call   1040 <read@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    121c <wait+0xac>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1080 <block@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r13d,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1200 <wait+0x90>
mov    %eax,%r14d
mov    %rbp,0x8(%rsp)
test   %r14d,%r14d
mov    %r15,0x18(%rsp)
jle    12b0 <wait+0x140>
mov    %r14d,%ebp
xor    %r12d,%r12d
mov    (%rsp),%ebx
mov    0x8(%rsp),%r13
jmp    1249 <wait+0xd9>
nop
add    $0x1,%r12
cmp    %r12,%rbp
je     12b8 <wait+0x148>
movsbl 0x0(%r13,%r12,1),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
call   1030 <fputc@plt>
movslq %ebx,%r15
cmp    %r15,0x2e01(%rip)
jne    1295 <wait+0x125>
mov    0x2df0(%rip),%rdi
mov    %ebx,%r13d
lea    (%r15,%r15,1),%rbx
mov    %rbx,%rsi
call   1070 <realloc@plt>
mov    %rax,0x2dda(%rip)
mov    %rbx,0x2ddb(%rip)
mov    %r13d,%ebx
mov    0x8(%rsp),%r13
movzbl 0x0(%r13,%r12,1),%eax
cmp    $0xd,%al
je     1240 <wait+0xd0>
mov    0x2dba(%rip),%rcx
lea    0x1(%r15),%ebx
mov    %al,(%rcx,%r15,1)
jmp    1240 <wait+0xd0>
mov    (%rsp),%ebx
mov    0x8(%rsp),%r13
mov    0x2da1(%rip),%rax
mov    %ebx,(%rsp)
movslq %ebx,%rcx
movb   $0x0,(%rax,%rcx,1)
movslq %r14d,%rax
movb   $0x0,0x0(%r13,%rax,1)
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    0x10(%rsp),%rsi
call   1090 <strstr@plt>
mov    %rax,%rbp
mov    0x18(%rsp),%r15
add    %r14d,%r15d
cmp    $0x65,%r15d
jl     11c0 <wait+0x50>
mov    %r15d,%eax
lea    (%rsp,%rax,1),%rsi
add    $0xffffffffffffffbc,%rsi
mov    %rbx,%rdi
mov    $0x64,%edx
call   1050 <memcpy@plt>
xor    %r15d,%r15d
jmp    11c0 <wait+0x50>
mov    0x2d40(%rip),%rax
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@wait.saved = internal global i8* null
@wait.saved_size = internal global i64 0
@stdout = global i32 0
define i8* @wait(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [200 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %7
store i32 0, i32* %9
store i32 0, i32* %10
%11 = load i8*, i8** @wait.saved
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i8* @malloc(i32 4096)
store i8* %14, i8** @wait.saved
store i64 4096, i64* @wait.saved_size
br label %15
15: ; preds = %13, %2
br label %16
16: ; preds = %123, %15
br label %17
17: ; preds = %32, %16
%18 = load i32, i32* %3
%19 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i32, i32* %7
%24 = sub nsw i32 200, %23
%25 = call i32 @read(i32 %18, i8* %22, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %17
%29 = load i32, i32* %3
%30 = call i32 @block(i32 %29)
br label %31
31: ; preds = %28, %17
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %17, label %35
35: ; preds = %32
store i32 0, i32* %8
br label %36
36: ; preds = %88, %35
%37 = load i32, i32* %8
%38 = load i32, i32* %6
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %91
40: ; preds = %36
%41 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = load i32, i32* @stdout
%50 = call i32 @fputc(i8 signext %48, i32 %49)
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = load i64, i64* @wait.saved_size
%54 = icmp eq i64 %52, %53
br i1 %54, label %55, label %62
55: ; preds = %40
%56 = load i8*, i8** @wait.saved
%57 = load i64, i64* @wait.saved_size
%58 = mul nsw i64 %57, 2
%59 = call i8* @realloc(i8* %56, i64 %58)
store i8* %59, i8** @wait.saved
%60 = load i64, i64* @wait.saved_size
%61 = mul nsw i64 %60, 2
store i64 %61, i64* @wait.saved_size
br label %62
62: ; preds = %55, %40
%63 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i32, i32* %8
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = icmp ne i32 %71, 13
br i1 %72, label %73, label %87
73: ; preds = %62
%74 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%75 = load i32, i32* %7
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %74, i64 %76
%78 = load i32, i32* %8
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %77, i64 %79
%81 = load i8, i8* %80
%82 = load i8*, i8** @wait.saved
%83 = load i32, i32* %10
%84 = add nsw i32 %83, 1
store i32 %84, i32* %10
%85 = sext i32 %83 to i64
%86 = getelementptr inbounds i8, i8* %82, i64 %85
store i8 %81, i8* %86
br label %87
87: ; preds = %73, %62
br label %88
88: ; preds = %87
%89 = load i32, i32* %8
%90 = add nsw i32 %89, 1
store i32 %90, i32* %8
br label %36
91: ; preds = %36
%92 = load i8*, i8** @wait.saved
%93 = load i32, i32* %10
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds i8, i8* %92, i64 %94
store i8 0, i8* %95
%96 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%97 = load i32, i32* %7
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds i8, i8* %96, i64 %98
%100 = load i32, i32* %6
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds i8, i8* %99, i64 %101
store i8 0, i8* %102
%103 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%104 = load i8*, i8** %4
%105 = call i64 @strstr(i8* %103, i8* %104)
%106 = icmp ne i64 %105, 0
br i1 %106, label %107, label %108
107: ; preds = %91
store i32 1, i32* %9
br label %108
108: ; preds = %107, %91
%109 = load i32, i32* %6
%110 = load i32, i32* %7
%111 = add nsw i32 %110, %109
store i32 %111, i32* %7
%112 = load i32, i32* %7
%113 = icmp sgt i32 %112, 100
br i1 %113, label %114, label %122
114: ; preds = %108
%115 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%116 = getelementptr inbounds [200 x i8], [200 x i8]* %5, i64 0, i64 0
%117 = load i32, i32* %7
%118 = sext i32 %117 to i64
%119 = getelementptr inbounds i8, i8* %116, i64 %118
%120 = getelementptr inbounds i8, i8* %119, i64 -100
%121 = call i32 @memcpy(i8* %115, i8* %120, i32 100)
store i32 0, i32* %7
br label %122
122: ; preds = %114, %108
br label %123
123: ; preds = %122
%124 = load i32, i32* %9
%125 = icmp ne i32 %124, 0
%126 = xor i1 %125, true
br i1 %126, label %16, label %127
127: ; preds = %123
%128 = load i8*, i8** @wait.saved
ret i8* %128
}

LLVM IR (-O2):
@wait.saved = internal unnamed_addr global i8* null
@wait.saved_size = internal unnamed_addr global i64 0
@stdout = global i32 0
define i8* @wait(i32 %0, i8* %1) {
%3 = alloca [200 x i8]
%4 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%5 = load i8*, i8** @wait.saved
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call dereferenceable_or_null(4096) i8* @malloc(i32 4096)
store i8* %8, i8** @wait.saved
store i64 4096, i64* @wait.saved_size
br label %9
9: ; preds = %7, %2
br label %10
10: ; preds = %9, %68
%11 = phi i32 [ %69, %68 ], [ 0, %9 ]
%12 = phi i32 [ %53, %68 ], [ 0, %9 ]
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 %13
%15 = sub nsw i32 200, %11
%16 = call i32 @read(i32 %0, i8* %14, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %23, label %18
18: ; preds = %23, %10
%19 = phi i32 [ %16, %10 ], [ %25, %23 ]
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %52
21: ; preds = %18
%22 = zext i32 %19 to i64
br label %27
23: ; preds = %10, %23
%24 = call i32 @block(i32 %0)
%25 = call i32 @read(i32 %0, i8* %14, i32 %15)
%26 = icmp eq i32 %25, -1
br i1 %26, label %23, label %18
27: ; preds = %21, %48
%28 = phi i64 [ 0, %21 ], [ %50, %48 ]
%29 = phi i32 [ %12, %21 ], [ %49, %48 ]
%30 = getelementptr inbounds i8, i8* %14, i64 %28
%31 = load i8, i8* %30
%32 = load i32, i32* @stdout
%33 = call i32 @fputc(i8 signext %31, i32 %32)
%34 = sext i32 %29 to i64
%35 = load i64, i64* @wait.saved_size
%36 = icmp eq i64 %35, %34
br i1 %36, label %37, label %41
37: ; preds = %27
%38 = load i8*, i8** @wait.saved
%39 = shl nsw i64 %34, 1
%40 = call i8* @realloc(i8* %38, i64 %39)
store i8* %40, i8** @wait.saved
store i64 %39, i64* @wait.saved_size
br label %41
41: ; preds = %37, %27
%42 = load i8, i8* %30
%43 = icmp eq i8 %42, 13
br i1 %43, label %48, label %44
44: ; preds = %41
%45 = load i8*, i8** @wait.saved
%46 = add nsw i32 %29, 1
%47 = getelementptr inbounds i8, i8* %45, i64 %34
store i8 %42, i8* %47
br label %48
48: ; preds = %41, %44
%49 = phi i32 [ %46, %44 ], [ %29, %41 ]
%50 = add nuw nsw i64 %28, 1
%51 = icmp eq i64 %50, %22
br i1 %51, label %52, label %27
52: ; preds = %48, %18
%53 = phi i32 [ %12, %18 ], [ %49, %48 ]
%54 = load i8*, i8** @wait.saved
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 0, i8* %56
%57 = sext i32 %19 to i64
%58 = getelementptr inbounds i8, i8* %14, i64 %57
store i8 0, i8* %58
%59 = call i64 @strstr(i8* %4, i8* %1)
%60 = icmp eq i64 %59, 0
%61 = add nsw i32 %19, %11
%62 = icmp sgt i32 %61, 100
br i1 %62, label %63, label %68
63: ; preds = %52
%64 = zext i32 %61 to i64
%65 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 %64
%66 = getelementptr inbounds i8, i8* %65, i64 -100
%67 = call i32 @memcpy(i8* %4, i8* %66, i32 100)
br label %68
68: ; preds = %52, %63
%69 = phi i32 [ 0, %63 ], [ %61, %52 ]
br i1 %60, label %10, label %70
70: ; preds = %68
%71 = load i8*, i8** @wait.saved
ret i8* %71
}