#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ obd_read(int, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *strstr(char *, char *);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
void obd_wait_until_echo(int fd, bool enable) {
  int max_iter = 10;
  char buf[50];
  for (int i = 0; i < max_iter; i++) {
    write(fd, (enable ? "ATE1\r" : "ATE0\r"), 5);
    obd_read(fd, buf);
    if (strstr(buf, "OK") != NULL) {
      return;
    }
    usleep(10000);
  }
  printf("Echo was not set in an acceptable amount of time\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<obd_wait_until_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xa,-0xc(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f1 <obd_wait_until_echo+0x91>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%ecx
lea    0xe71(%rip),%rsi
lea    0xe64(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1050 <obd_read@plt>
lea    -0x40(%rbp),%rdi
lea    0xe48(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     11d9 <obd_wait_until_echo+0x79>
add    $0x50,%rsp
pop    %rbp
ret
mov    $0x2710,%edi
call   1080 <usleep@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    117c <obd_wait_until_echo+0x1c>
lea    0xe17(%rip),%rdi
call   1040 <printf@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<obd_wait_until_echo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
test   %esi,%esi
lea    0xe91(%rip),%rax
lea    0xe84(%rip),%r14
cmove  %rax,%r14
mov    %edi,%ebp
mov    $0xa,%ebx
mov    %rsp,%r12
lea    0xe7b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %r12,%rsi
call   1050 <obd_read@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
jne    11f2 <obd_wait_until_echo+0x92>
mov    $0x2710,%edi
call   1080 <usleep@plt>
add    $0xffffffff,%ebx
jne    11a0 <obd_wait_until_echo+0x40>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"ATE1\0D\00"
@.str.1 = [6 x i8] c"ATE0\0D\00"
@.str.2 = [3 x i8] c"OK\00"
@.str.3 = [50 x i8] c"Echo was not set in an acceptable amount of time\0A\00"
@EXIT_FAILURE = global i32 0
define void @obd_wait_until_echo(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [50 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 10, i32* %5
store i32 0, i32* %7
br label %8
8: ; preds = %28, %2
%9 = load i32, i32* %7
%10 = load i32, i32* %5
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %31
12: ; preds = %8
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 0
%16 = zext i1 %15 to i64
%17 = select i1 %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0)
%18 = call i32 @write(i32 %13, i8* %17, i32 5)
%19 = load i32, i32* %3
%20 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%21 = call i32 @obd_read(i32 %19, i8* %20)
%22 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%23 = call i32* @strstr(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %26
25: ; preds = %12
ret void
26: ; preds = %12
%27 = call i32 @usleep(i32 10000)
br label %28
28: ; preds = %26
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %8
31: ; preds = %8
%32 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
}

LLVM IR (-O2):
@.str = [6 x i8] c"ATE1\0D\00"
@.str.1 = [6 x i8] c"ATE0\0D\00"
@.str.2 = [3 x i8] c"OK\00"
@EXIT_FAILURE = global i32 0
@str = [49 x i8] c"Echo was not set in an acceptable amount of time\00"
define void @obd_wait_until_echo(i32 %0, i32 %1) {
%3 = alloca [50 x i8]
%4 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %1, 0
%6 = select i1 %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0)
br label %7
7: ; preds = %2, %13
%8 = phi i32 [ 0, %2 ], [ %15, %13 ]
%9 = call i32 @write(i32 %0, i8* %6, i32 5)
%10 = call i32 @obd_read(i32 %0, i8* %4)
%11 = call i32* @strstr(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %21
13: ; preds = %7
%14 = call i32 @usleep(i32 10000)
%15 = add nuw nsw i32 %8, 1
%16 = icmp eq i32 %15, 10
br i1 %16, label %17, label %7
17: ; preds = %13
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %7
ret void
}