#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*/ db(int, char *, ...);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ rd_drain();
int /*<<< orphan*/ rd_line(char *, int);
scalar_t__ starts_with(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ wr_cmdl(char const *);
int do_cmd(const char *cmd, const char *expect, char *buf, int len) {
  int try;
  rd_drain();
  wr_cmdl(cmd);
  db(2, "Cmd: %s\n", cmd);
  for (try = 0; try < 60; try++) {
    rd_line(buf, len);
    db(3, "Got: %s\n", buf);
    if (starts_with(buf, expect)) {
      db(2, "Matched: %s\n", buf);
      return strlen(expect);
    }
    db(2, "Skip %d: %s\n", try, buf);
  }
  fatal("WBT-100/200"
        ": Bad response from unit\n");
  return 0;
}


x86 assembly (clang, -O0):
<do_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    $0x0,%al
call   1060 <rd_drain@plt>
mov    -0x10(%rbp),%rdi
call   1030 <wr_cmdl@plt>
mov    -0x10(%rbp),%rdx
mov    $0x2,%edi
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1080 <db@plt>
movl   $0x0,-0x28(%rbp)
cmpl   $0x3c,-0x28(%rbp)
jge    1249 <do_cmd+0xd9>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1050 <rd_line@plt>
mov    -0x20(%rbp),%rdx
mov    $0x3,%edi
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1080 <db@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <starts_with@plt>
cmp    $0x0,%rax
je     1221 <do_cmd+0xb1>
mov    -0x20(%rbp),%rdx
mov    $0x2,%edi
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1080 <db@plt>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4(%rbp)
jmp    125c <do_cmd+0xec>
mov    -0x28(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x2,%edi
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1080 <db@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b5 <do_cmd+0x45>
lea    0xddc(%rip),%rdi
call   1090 <fatal@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%rbp
xor    %eax,%eax
call   1060 <rd_drain@plt>
mov    %rbp,%rdi
call   1030 <wr_cmdl@plt>
lea    0xe63(%rip),%rsi
mov    $0x2,%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <db@plt>
lea    0xe56(%rip),%r15
lea    0xe65(%rip),%r12
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r13d,%esi
call   1050 <rd_line@plt>
mov    $0x3,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <db@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <starts_with@plt>
test   %rax,%rax
jne    1226 <do_cmd+0xb6>
mov    $0x2,%edi
mov    %r12,%rsi
mov    %ebp,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1080 <db@plt>
add    $0x1,%ebp
cmp    $0x3c,%ebp
jne    11c0 <do_cmd+0x50>
lea    0xe1c(%rip),%rdi
call   1090 <fatal@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde5(%rip),%rsi
mov    $0x2,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1080 <db@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strlen@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"Cmd: %s\0A\00"
@.str.1 = [9 x i8] c"Got: %s\0A\00"
@.str.2 = [13 x i8] c"Matched: %s\0A\00"
@.str.3 = [13 x i8] c"Skip %d: %s\0A\00"
@.str.4 = [37 x i8] c"WBT-100/200: Bad response from unit\0A\00"
define i32 @do_cmd(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = call i32 (...) @rd_drain()
%12 = load i8*, i8** %6
%13 = call i32 @wr_cmdl(i8* %12)
%14 = load i8*, i8** %6
%15 = call i32 (i32, i8*, ...) @db(i32 2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 0, i32* %10
br label %16
16: ; preds = %38, %4
%17 = load i32, i32* %10
%18 = icmp slt i32 %17, 60
br i1 %18, label %19, label %41
19: ; preds = %16
%20 = load i8*, i8** %8
%21 = load i32, i32* %9
%22 = call i32 @rd_line(i8* %20, i32 %21)
%23 = load i8*, i8** %8
%24 = call i32 (i32, i8*, ...) @db(i32 3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %8
%26 = load i8*, i8** %7
%27 = call i64 @starts_with(i8* %25, i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = load i8*, i8** %8
%31 = call i32 (i32, i8*, ...) @db(i32 2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %30)
%32 = load i8*, i8** %7
%33 = call i32 @strlen(i8* %32)
store i32 %33, i32* %5
br label %43
34: ; preds = %19
%35 = load i32, i32* %10
%36 = load i8*, i8** %8
%37 = call i32 (i32, i8*, ...) @db(i32 2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %35, i8* %36)
br label %38
38: ; preds = %34
%39 = load i32, i32* %10
%40 = add nsw i32 %39, 1
store i32 %40, i32* %10
br label %16
41: ; preds = %16
%42 = call i32 @fatal(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %5
br label %43
43: ; preds = %41, %29
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@.str = [9 x i8] c"Cmd: %s\0A\00"
@.str.1 = [9 x i8] c"Got: %s\0A\00"
@.str.2 = [13 x i8] c"Matched: %s\0A\00"
@.str.3 = [13 x i8] c"Skip %d: %s\0A\00"
@.str.4 = [37 x i8] c"WBT-100/200: Bad response from unit\0A\00"
define i32 @do_cmd(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = tail call i32 (...) @rd_drain()
%6 = tail call i32 @wr_cmdl(i8* %0)
%7 = tail call i32 (i32, i8*, ...) @db(i32 2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %8
8: ; preds = %4, %17
%9 = phi i32 [ 0, %4 ], [ %19, %17 ]
%10 = tail call i32 @rd_line(i8* %2, i32 %3)
%11 = tail call i32 (i32, i8*, ...) @db(i32 3, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%12 = tail call i64 @starts_with(i8* %2, i8* %1)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = tail call i32 (i32, i8*, ...) @db(i32 2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %2)
%16 = tail call i32 @strlen(i8* %1)
br label %23
17: ; preds = %8
%18 = tail call i32 (i32, i8*, ...) @db(i32 2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %9, i8* %2)
%19 = add nuw nsw i32 %9, 1
%20 = icmp eq i32 %19, 60
br i1 %20, label %21, label %8
21: ; preds = %17
%22 = tail call i32 @fatal(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %23
23: ; preds = %21, %14
%24 = phi i32 [ %16, %14 ], [ 0, %21 ]
ret i32 %24
}