#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ gettimeofday(struct timeval *, struct timezone *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ write_data(int, int);
char read_data_f(int usbdev, unsigned long int wait_time, int n_wait_states) {
  struct timeval tv;
  struct timezone tz;
  unsigned long int time, start, end;
  start = tv.tv_usec;
  time = 0;
  int wait_ = 0;
  int rd = 0;
  int cont = 0;
  char response_[5] = {0, 0, 0, 0, 0};
  gettimeofday(&tv, &tz);
  printf("n wait_states %d\n", n_wait_states);
  while (rd < 1) {
    rd = read(usbdev, response_, 1);
    gettimeofday(&tv, &tz);
    end = tv.tv_usec;
    time = end - start;
    cont = cont + 1;
    if (time > wait_time) {
      printf("Error: Exceeded wait time \nSending null character '~'\n");
      write_data(usbdev, 0x7e);
      wait_ = wait_ + 1;
      time = 0;
      gettimeofday(&tv, &tz);
      start = tv.tv_usec;
      if (wait_ == n_wait_states) {
        printf("Error: Esceeded wait states.. Exit\n");
        exit(EXIT_SUCCESS);
      }
    }
  }
  printf("response = %x, cont = %d\n", response_[0], cont);
  return response_[0];
}


x86 assembly (clang, -O0):
<read_data_f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x0,-0x4c(%rbp)
lea    -0x51(%rbp),%rdi
xor    %esi,%esi
mov    $0x5,%edx
call   1050 <memset@plt>
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <gettimeofday@plt>
mov    -0x14(%rbp),%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x1,-0x48(%rbp)
jge    128e <read_data_f+0x12e>
mov    -0x4(%rbp),%edi
lea    -0x51(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x48(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <gettimeofday@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
sub    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    1289 <read_data_f+0x129>
lea    0xdf0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    $0x7e,%esi
call   1070 <write_data@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <gettimeofday@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1284 <read_data_f+0x124>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1289 <read_data_f+0x129>
jmp    11c5 <read_data_f+0x65>
movsbl -0x51(%rbp),%esi
mov    -0x4c(%rbp),%edx
lea    0xdd1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movsbl -0x51(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data_f>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r14d
mov    %rsi,0x20(%rsp)
mov    %edi,%ebx
movb   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rdi
lea    0x30(%rsp),%rbp
mov    %rbp,%rsi
call   1050 <gettimeofday@plt>
lea    0xe62(%rip),%rdi
xor    %r13d,%r13d
mov    %r14d,0x18(%rsp)
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %r12d,%r12d
mov    %ebx,0x1c(%rsp)
jmp    11cd <read_data_f+0x6d>
nopl   0x0(%rax)
add    $0x1,%r13d
test   %r14d,%r14d
mov    0x1c(%rsp),%ebx
jg     123c <read_data_f+0xdc>
mov    %ebx,%edi
lea    0x8(%rsp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %rbp,%rsi
call   1050 <gettimeofday@plt>
mov    0x10(%rsp),%rax
sub    0x28(%rsp),%rax
cmp    0x20(%rsp),%rax
jbe    11c0 <read_data_f+0x60>
lea    0xe23(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x7e,%esi
call   1070 <write_data@plt>
add    $0x1,%r12d
mov    %r15,%rdi
mov    %rbp,%rsi
call   1050 <gettimeofday@plt>
cmp    0x18(%rsp),%r12d
je     1267 <read_data_f+0x107>
mov    0x10(%rsp),%rax
mov    %rax,0x28(%rsp)
jmp    11c0 <read_data_f+0x60>
mov    0x8(%rsp),%al
movsbl %al,%ebp
lea    0xdc8(%rip),%rdi
mov    %ebp,%esi
mov    %r13d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d66(%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 = [55 x i8] c"Error: Exceeded wait time \0ASending null character '~'\0A\00"
@.str.2 = [36 x i8] c"Error: Esceeded wait states.. Exit\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [26 x i8] c"response = %x, cont = %d\0A\00"
define signext i8 @read_data_f(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 i64
%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* %10
store i64 0, i64* %9
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%18 = bitcast [5 x i8]* %15 to i8*
%19 = call i32 @gettimeofday(%struct.timeval* %7, %struct.timezone* %8)
%20 = load i32, i32* %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %57, %3
%23 = load i32, i32* %13
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %58
25: ; preds = %22
%26 = load i32, i32* %4
%27 = getelementptr inbounds [5 x i8], [5 x i8]* %15, i64 0, i64 0
%28 = call i32 @read(i32 %26, i8* %27, i32 1)
store i32 %28, i32* %13
%29 = call i32 @gettimeofday(%struct.timeval* %7, %struct.timezone* %8)
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%31 = load i64, i64* %30
store i64 %31, i64* %11
%32 = load i64, i64* %11
%33 = load i64, i64* %10
%34 = sub i64 %32, %33
store i64 %34, i64* %9
%35 = load i32, i32* %14
%36 = add nsw i32 %35, 1
store i32 %36, i32* %14
%37 = load i64, i64* %9
%38 = load i64, i64* %5
%39 = icmp ugt i64 %37, %38
br i1 %39, label %40, label %57
40: ; preds = %25
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%42 = load i32, i32* %4
%43 = call i32 @write_data(i32 %42, i32 126)
%44 = load i32, i32* %12
%45 = add nsw i32 %44, 1
store i32 %45, i32* %12
store i64 0, i64* %9
%46 = call i32 @gettimeofday(%struct.timeval* %7, %struct.timezone* %8)
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%48 = load i64, i64* %47
store i64 %48, i64* %10
%49 = load i32, i32* %12
%50 = load i32, i32* %6
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %56
52: ; preds = %40
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %40
br label %57
57: ; preds = %56, %25
br label %22
58: ; preds = %22
%59 = getelementptr inbounds [5 x i8], [5 x i8]* %15, i64 0, i64 0
%60 = load i8, i8* %59
%61 = sext i8 %60 to i32
%62 = load i32, i32* %14
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %61, i32 %62)
%64 = getelementptr inbounds [5 x i8], [5 x i8]* %15, i64 0, i64 0
%65 = load i8, i8* %64
ret i8 %65
}

LLVM IR (-O2):
%struct.timeval = type { i64 }
%struct.timezone = type { i32 }
@.str = [18 x i8] c"n wait_states %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.3 = [26 x i8] c"response = %x, cont = %d\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_f(i32 %0, i64 %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timezone
%6 = alloca [5 x i8]
%7 = bitcast %struct.timeval* %4 to i8*
%8 = bitcast %struct.timezone* %5 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
%11 = call i32 @gettimeofday(%struct.timeval* %4, %struct.timezone* %5)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %13
13: ; preds = %3, %34
%14 = phi i32 [ 0, %3 ], [ %21, %34 ]
%15 = phi i32 [ 0, %3 ], [ %36, %34 ]
%16 = phi i64 [ undef, %3 ], [ %35, %34 ]
%17 = call i32 @read(i32 %0, i8* %10, i32 1)
%18 = call i32 @gettimeofday(%struct.timeval* %4, %struct.timezone* %5)
%19 = load i64, i64* %9
%20 = sub i64 %19, %16
%21 = add nuw nsw i32 %14, 1
%22 = icmp ugt i64 %20, %1
br i1 %22, label %23, label %34
23: ; preds = %13
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%25 = tail call i32 @write_data(i32 %0, i32 126)
%26 = add nsw i32 %15, 1
%27 = call i32 @gettimeofday(%struct.timeval* %4, %struct.timezone* %5)
%28 = load i64, i64* %9
%29 = icmp eq i32 %26, %2
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.4, i64 0, i64 0))
%32 = load i32, i32* @EXIT_SUCCESS
%33 = tail call i32 @exit(i32 %32)
unreachable
34: ; preds = %23, %13
%35 = phi i64 [ %28, %23 ], [ %16, %13 ]
%36 = phi i32 [ %26, %23 ], [ %15, %13 ]
%37 = icmp slt i32 %17, 1
br i1 %37, label %13, label %38
38: ; preds = %34
%39 = load i8, i8* %10
%40 = sext i8 %39 to i32
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %40, i32 %21)
ret i8 %39
}