#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 */
struct timezone {
  int dummy;
};
struct timeval {
  unsigned long tv_usec;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ delay(unsigned long);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ write_data(int, int);
char read_data_b(int usbdev, unsigned long int wait_time, int n_wait_states) {
  struct timeval tv;
  struct timezone tz;
  unsigned long int start, end;
  start = tv.tv_usec;
  int time = 0;
  int wait_ = 0;
  int rd = 0;
  int cont = 0;
  char response_[5] = {0, 0, 0, 0, 0};
  rd = read(usbdev, response_, 1);
  printf("n wait_states %d\n", n_wait_states);
  while (rd < 1) {
    delay(wait_time);
    rd = read(usbdev, response_, 1);
    if (rd >= 1) {
      break;
    } else if (wait_ == n_wait_states) {
      printf("Error: Esceeded wait states.. Exit\n");
      exit(EXIT_SUCCESS);
    } else {
      write_data(usbdev, 0x3e);
      printf("Error: Exceeded wait time \nSending null character '~'\n");
      wait_ = wait_ + 1;
    }
  }
  printf("response = %x\n", response_[0]);
  return response_[0];
}


x86 assembly (clang, -O0):
<read_data_b>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
lea    -0x4d(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x4d(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x44(%rbp)
jge    1257 <read_data_b+0xf7>
mov    -0x10(%rbp),%rdi
call   1050 <delay@plt>
mov    -0x4(%rbp),%edi
lea    -0x4d(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x44(%rbp)
jl     1201 <read_data_b+0xa1>
jmp    1257 <read_data_b+0xf7>
mov    -0x40(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1229 <read_data_b+0xc9>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0x3e,%esi
call   1070 <write_data@plt>
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1252 <read_data_b+0xf2>
jmp    11cb <read_data_b+0x6b>
movsbl -0x4d(%rbp),%esi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x4d(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data_b>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
movb   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%r15d
lea    0xe67(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r15d,%r15d
jg     1214 <read_data_b+0xb4>
mov    %r14,%rdi
call   1050 <delay@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1214 <read_data_b+0xb4>
add    $0x1,%ebx
lea    0xe53(%rip),%r15
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%ebx
je     123a <read_data_b+0xda>
mov    %ebp,%edi
mov    $0x3e,%esi
call   1070 <write_data@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1050 <delay@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    11e0 <read_data_b+0x80>
mov    0x8(%rsp),%al
movsbl %al,%ebx
lea    0xdf0(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe16(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64 }
%struct.timezone = type { i32 }
@.str = [18 x i8] c"n wait_states %d\0A\00"
@.str.1 = [36 x i8] c"Error: Esceeded wait states.. Exit\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [55 x i8] c"Error: Exceeded wait time \0ASending null character '~'\0A\00"
@.str.3 = [15 x i8] c"response = %x\0A\00"
define signext i8 @read_data_b(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.timezone
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [5 x i8]
store i32 %0, i32* %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%17 = load i64, i64* %16
store i64 %17, i64* %9
store i32 0, i32* %11
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%18 = bitcast [5 x i8]* %15 to i8*
%19 = load i32, i32* %4
%20 = getelementptr inbounds [5 x i8], [5 x i8]* %15, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 1)
store i32 %21, i32* %13
%22 = load i32, i32* %6
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %51, %3
%25 = load i32, i32* %13
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %52
27: ; preds = %24
%28 = load i64, i64* %5
%29 = call i32 @delay(i64 %28)
%30 = load i32, i32* %4
%31 = getelementptr inbounds [5 x i8], [5 x i8]* %15, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 1)
store i32 %32, i32* %13
%33 = load i32, i32* %13
%34 = icmp sge i32 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %27
br label %52
36: ; preds = %27
%37 = load i32, i32* %12
%38 = load i32, i32* %6
%39 = icmp eq i32 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%42 = load i32, i32* @EXIT_SUCCESS
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %36
%45 = load i32, i32* %4
%46 = call i32 @write_data(i32 %45, i32 62)
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* %12
%49 = add nsw i32 %48, 1
store i32 %49, i32* %12
br label %50
50: ; preds = %44
br label %51
51: ; preds = %50
br label %24
52: ; preds = %35, %24
%53 = getelementptr inbounds [5 x i8], [5 x i8]* %15, i64 0, i64 0
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %55)
%57 = getelementptr inbounds [5 x i8], [5 x i8]* %15, i64 0, i64 0
%58 = load i8, i8* %57
ret i8 %58
}

LLVM IR (-O2):
@.str = [18 x i8] c"n wait_states %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [15 x i8] c"response = %x\0A\00"
@str = [54 x i8] c"Error: Exceeded wait time \0ASending null character '~'\00"
@str.4 = [35 x i8] c"Error: Esceeded wait states.. Exit\00"
define signext i8 @read_data_b(i32 %0, i64 %1, i32 %2) {
%4 = alloca [5 x i8]
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%6 = call i32 @read(i32 %0, i8* %5, i32 1)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %2)
%8 = icmp slt i32 %6, 1
br i1 %8, label %9, label %27
9: ; preds = %3
%10 = tail call i32 @delay(i64 %1)
%11 = call i32 @read(i32 %0, i8* %5, i32 1)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %27, label %13
13: ; preds = %9, %20
%14 = phi i32 [ %23, %20 ], [ 0, %9 ]
%15 = icmp eq i32 %14, %2
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.4, i64 0, i64 0))
%18 = load i32, i32* @EXIT_SUCCESS
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %13
%21 = tail call i32 @write_data(i32 %0, i32 62)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%23 = add nuw nsw i32 %14, 1
%24 = tail call i32 @delay(i64 %1)
%25 = call i32 @read(i32 %0, i8* %5, i32 1)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %13
27: ; preds = %20, %9, %3
%28 = load i8, i8* %5
%29 = sext i8 %28 to i32
%30 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %29)
ret i8 %28
}