#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 timeval {
  double tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write_data(int, int);
char read_data_d(int usbdev, unsigned long int wait_time, int n_wait_states) {
  struct timeval start, end, clock_;
  double time = 0;
  gettimeofday(&start, NULL);
  int wait_ = 0;
  int rd = 0;
  char response_[5] = {0, 0, 0, 0, 0};
  while (rd < 1) {
    rd = read(usbdev, response_, 1);
    gettimeofday(&end, NULL);
    time = end.tv_sec - start.tv_sec;
    if (time >= wait_time) {
      if (wait_ == n_wait_states) {
        printf("Error: Esceeded wait states.. Exit\n");
        exit(EXIT_SUCCESS);
      }
      printf("Error: Exceeded wait time \nSending null character '~'\n");
      write_data(usbdev, 0x3e);
      wait_ = wait_ + 1;
      time = 0;
      gettimeofday(&start, NULL);
    }
  }
  return response_[0];
}


x86 assembly (clang, -O0):
<read_data_d>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
lea    -0x45(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1050 <memset@plt>
cmpl   $0x1,-0x40(%rbp)
jge    127b <read_data_d+0x11b>
mov    -0x4(%rbp),%edi
lea    -0x45(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x40(%rbp)
xor    %eax,%eax
mov    %eax,%esi
lea    -0x28(%rbp),%rdi
call   1040 <gettimeofday@plt>
movsd  -0x28(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
movaps 0xe11(%rip),%xmm2
movq   -0x10(%rbp),%xmm1
punpckldq %xmm2,%xmm1
movapd 0xe10(%rip),%xmm2
subpd  %xmm2,%xmm1
movaps %xmm1,%xmm2
unpckhpd %xmm1,%xmm1
addsd  %xmm2,%xmm1
ucomisd %xmm1,%xmm0
jb     1276 <read_data_d+0x116>
mov    -0x3c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    123f <read_data_d+0xdf>
lea    0xdf4(%rip),%rdi
call   1030 <printf@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xdfe(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0x3e,%esi
call   1070 <write_data@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
jmp    11a5 <read_data_d+0x45>
movsbl -0x45(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data_d>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r13d,%r13d
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
movb   $0x0,0xc(%rsp)
movq   %rbx,%xmm0
punpckldq 0xe79(%rip),%xmm0
movl   $0x0,0x8(%rsp)
subpd  0xe79(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
movapd %xmm1,0x20(%rsp)
lea    0x8(%rsp),%r12
lea    0x18(%rsp),%rbx
jmp    11c5 <read_data_d+0x75>
cs nopw 0x0(%rax,%rax,1)
nop
test   %r14d,%r14d
jg     1224 <read_data_d+0xd4>
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
movsd  0x18(%rsp),%xmm0
subsd  0x10(%rsp),%xmm0
ucomisd 0x20(%rsp),%xmm0
jb     11c0 <read_data_d+0x70>
cmp    %r15d,%r13d
je     1237 <read_data_d+0xe7>
lea    0xe1f(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    $0x3e,%esi
call   1060 <write_data@plt>
add    $0x1,%r13d
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1040 <gettimeofday@plt>
jmp    11c0 <read_data_d+0x70>
mov    0x8(%rsp),%al
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
%struct.timeval = type { double }
@EXIT_SUCCESS = global i32 0
@str = [54 x i8] c"Error: Exceeded wait time \0ASending null character '~'\00"
@str.2 = [35 x i8] c"Error: Esceeded wait states.. Exit\00"
define signext i8 @read_data_d(i32 %0, i64 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca [5 x i8]
%7 = bitcast %struct.timeval* %4 to i8*
%8 = bitcast %struct.timeval* %5 to i8*
%9 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%13 = uitofp i64 %1 to double
br label %14
14: ; preds = %3, %33
%15 = phi i32 [ 0, %3 ], [ %34, %33 ]
%16 = call i32 @read(i32 %0, i8* %10, i32 1)
%17 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%18 = load double, double* %11
%19 = load double, double* %12
%20 = fsub double %18, %19
%21 = fcmp ult double %20, %13
br i1 %21, label %33, label %22
22: ; preds = %14
%23 = icmp eq i32 %15, %2
br i1 %23, label %24, label %28
24: ; preds = %22
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_SUCCESS
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %22
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%30 = tail call i32 @write_data(i32 %0, i32 62)
%31 = add nsw i32 %15, 1
%32 = call i32 @gettimeofday(%struct.timeval* %4, i32* null)
br label %33
33: ; preds = %28, %14
%34 = phi i32 [ %31, %28 ], [ %15, %14 ]
%35 = icmp slt i32 %16, 1
br i1 %35, label %14, label %36
36: ; preds = %33
%37 = load i8, i8* %10
ret i8 %37
}