#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 */
char *readline();
int /*<<< orphan*/ sleep(int);
scalar_t__ strStartsWith(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int writeline(char const *);
int write_wait_read(const char *s, const char *r, char *output) {
  char *line;
  int count = 0;
  int ret;
  ret = writeline(s);
  if (ret < 0)
    return -1;
  for (;;) {
    line = readline();
    if (strStartsWith(line, r)) {
      strcpy(output, line);
      break;
    } else {
      count++;
      sleep(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_wait_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <writeline@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    118d <write_wait_read+0x3d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e5 <write_wait_read+0x95>
jmp    1192 <write_wait_read+0x42>
mov    $0x0,%al
call   1050 <readline@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strStartsWith@plt>
cmp    $0x0,%rax
je     11c6 <write_wait_read+0x76>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11de <write_wait_read+0x8e>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1192 <write_wait_read+0x42>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_wait_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
call   1060 <writeline@plt>
test   %eax,%eax
js     11a3 <write_wait_read+0x53>
xor    %r15d,%r15d
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <readline@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strStartsWith@plt>
test   %rax,%rax
jne    1196 <write_wait_read+0x46>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    1170 <write_wait_read+0x20>
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
jmp    11a9 <write_wait_read+0x59>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_wait_read(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %9
%11 = load i8*, i8** %5
%12 = call i32 @writeline(i8* %11)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %33
16: ; preds = %3
br label %17
17: ; preds = %31, %16
%18 = call i8* (...) @readline()
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = load i8*, i8** %6
%21 = call i64 @strStartsWith(i8* %19, i8* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %17
%24 = load i8*, i8** %7
%25 = load i8*, i8** %8
%26 = call i32 @strcpy(i8* %24, i8* %25)
br label %32
27: ; preds = %17
%28 = load i32, i32* %9
%29 = add nsw i32 %28, 1
store i32 %29, i32* %9
%30 = call i32 @sleep(i32 1)
br label %31
31: ; preds = %27
br label %17
32: ; preds = %23
store i32 0, i32* %4
br label %33
33: ; preds = %32, %15
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
define i32 @write_wait_read(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @writeline(i8* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %3
%7 = tail call i8* (...) @readline()
%8 = tail call i64 @strStartsWith(i8* %7, i8* %1)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %13, %6
%11 = phi i8* [ %7, %6 ], [ %15, %13 ]
%12 = tail call i32 @strcpy(i8* %2, i8* %11)
br label %18
13: ; preds = %6, %13
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i8* (...) @readline()
%16 = tail call i64 @strStartsWith(i8* %15, i8* %1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %13, label %10
18: ; preds = %3, %10
%19 = phi i32 [ 0, %10 ], [ -1, %3 ]
ret i32 %19
}