#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 buspirate_sendrecv(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memcmp(unsigned char *, char const *, unsigned int);
int /*<<< orphan*/ memmove(unsigned char *, unsigned char *, unsigned int);
unsigned int strlen(char const *);
int buspirate_wait_for_string(unsigned char *buf, const char *key) {
  unsigned int keylen = strlen(key);
  int ret;
  ret = buspirate_sendrecv(buf, 0, keylen);
  while (!ret) {
    if (!memcmp(buf, key, keylen))
      return 0;
    memmove(buf, buf + 1, keylen - 1);
    ret = buspirate_sendrecv(buf + keylen - 1, 0, 1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<buspirate_wait_for_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1040 <buspirate_sendrecv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1183 <buspirate_wait_for_string+0x43>
jmp    11e1 <buspirate_wait_for_string+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcmp@plt>
cmp    $0x0,%eax
jne    11a8 <buspirate_wait_for_string+0x68>
movl   $0x0,-0x4(%rbp)
jmp    11e7 <buspirate_wait_for_string+0xa7>
mov    -0x10(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1060 <memmove@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%eax
add    %rax,%rdi
add    $0xffffffffffffffff,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <buspirate_sendrecv@plt>
mov    %eax,-0x20(%rbp)
jmp    116d <buspirate_wait_for_string+0x2d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buspirate_wait_for_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
call   1040 <buspirate_sendrecv@plt>
test   %eax,%eax
jne    11c6 <buspirate_wait_for_string+0x86>
lea    0x1(%rbx),%r12
mov    %r13d,%eax
mov    %r13d,%ebp
add    $0xffffffff,%ebp
lea    (%rbx,%rax,1),%r15
add    $0xffffffffffffffff,%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   1050 <memcmp@plt>
test   %eax,%eax
je     11c4 <buspirate_wait_for_string+0x84>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   1060 <memmove@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x1,%edx
call   1040 <buspirate_sendrecv@plt>
test   %eax,%eax
je     1190 <buspirate_wait_for_string+0x50>
jmp    11c6 <buspirate_wait_for_string+0x86>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @buspirate_wait_for_string(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i8*, i8** %4
%11 = load i32, i32* %6
%12 = call i32 @buspirate_sendrecv(i8* %10, i32 0, i32 %11)
store i32 %12, i32* %7
br label %13
13: ; preds = %24, %2
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
%16 = xor i1 %15, true
br i1 %16, label %17, label %37
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = load i32, i32* %6
%21 = call i32 @memcmp(i8* %18, i8* %19, i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %17
store i32 0, i32* %3
br label %39
24: ; preds = %17
%25 = load i8*, i8** %4
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i32, i32* %6
%29 = sub i32 %28, 1
%30 = call i32 @memmove(i8* %25, i8* %27, i32 %29)
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = zext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = getelementptr inbounds i8, i8* %34, i64 -1
%36 = call i32 @buspirate_sendrecv(i8* %35, i32 0, i32 1)
store i32 %36, i32* %7
br label %13
37: ; preds = %13
%38 = load i32, i32* %7
store i32 %38, i32* %3
br label %39
39: ; preds = %37, %23
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
define i32 @buspirate_wait_for_string(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = tail call i32 @buspirate_sendrecv(i8* %0, i32 0, i32 %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %2
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = add i32 %3, -1
%9 = zext i32 %3 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = getelementptr inbounds i8, i8* %10, i64 -1
br label %12
12: ; preds = %6, %15
%13 = tail call i32 @memcmp(i8* %0, i8* %1, i32 %3)
%14 = icmp eq i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = tail call i32 @memmove(i8* %0, i8* %7, i32 %8)
%17 = tail call i32 @buspirate_sendrecv(i8* %11, i32 0, i32 1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %12, label %19
19: ; preds = %12, %15, %2
%20 = phi i32 [ %4, %2 ], [ %17, %15 ], [ 0, %12 ]
ret i32 %20
}