#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_on(int fd) {
  int max_iter = 100;
  char buf[50];
  for (int i = 0; i < max_iter; i++) {
    write(fd, "ATIGN\r", 6);
    obd_read(fd, buf);
    if (strstr(buf, "ON") != NULL) {
      return;
    }
    usleep(10000);
  }
  printf("Device was not ready in an acceptable amount of time\n");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<obd_wait_until_on>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x64,-0x8(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11dd <obd_wait_until_on+0x7d>
mov    -0x4(%rbp),%edi
lea    0xe71(%rip),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1050 <obd_read@plt>
lea    -0x40(%rbp),%rdi
lea    0xe57(%rip),%rsi
call   1070 <strstr@plt>
cmp    $0x0,%rax
je     11c5 <obd_wait_until_on+0x65>
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    1179 <obd_wait_until_on+0x19>
lea    0xe26(%rip),%rdi
call   1040 <printf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<obd_wait_until_on>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebx
mov    $0x64,%ebp
lea    0xe86(%rip),%r14
mov    %rsp,%r15
lea    0xe83(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1050 <obd_read@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <strstr@plt>
test   %rax,%rax
jne    11e2 <obd_wait_until_on+0x82>
mov    $0x2710,%edi
call   1080 <usleep@plt>
add    $0xffffffff,%ebp
jne    1190 <obd_wait_until_on+0x30>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e05(%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 = [7 x i8] c"ATIGN\0D\00"
@.str.1 = [3 x i8] c"ON\00"
@.str.2 = [54 x i8] c"Device was not ready in an acceptable amount of time\0A\00"
@EXIT_FAILURE = global i32 0
define void @obd_wait_until_on(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [50 x i8]
%5 = alloca i32
store i32 %0, i32* %2
store i32 100, i32* %3
store i32 0, i32* %5
br label %6
6: ; preds = %22, %1
%7 = load i32, i32* %5
%8 = load i32, i32* %3
%9 = icmp slt i32 %7, %8
br i1 %9, label %10, label %25
10: ; preds = %6
%11 = load i32, i32* %2
%12 = call i32 @write(i32 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 6)
%13 = load i32, i32* %2
%14 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%15 = call i32 @obd_read(i32 %13, i8* %14)
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %4, i64 0, i64 0
%17 = call i32* @strstr(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %10
ret void
20: ; preds = %10
%21 = call i32 @usleep(i32 10000)
br label %22
22: ; preds = %20
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %6
25: ; preds = %6
%26 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
}

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