#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 {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ assert(unsigned char *);
int /*<<< orphan*/ mssleep(int);
scalar_t__ read(int, unsigned char *, int);
int timerLap(struct timeval *);
int /*<<< orphan*/ timerStart(struct timeval *);
size_t amrReadBytes(int fd, unsigned char *buffer, size_t bytes) {
  assert(buffer);
  if (bytes == 0) {
    return 0;
  }
  unsigned char c;
  struct timeval timer;
  timerStart(&timer);
  size_t byteIndex = 0;
  do {
    ssize_t sread = read(fd, &c, 1);
    if (sread > 0) {
      buffer[byteIndex] = c;
      byteIndex++;
      if (byteIndex == bytes) {
        break;
      }
    } else {
      mssleep(10);
    }
  } while (timerLap(&timer) < 1000);
  return byteIndex;
}


x86 assembly (clang, -O0):
<amrReadBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1184 <amrReadBytes+0x34>
movq   $0x0,-0x8(%rbp)
jmp    1212 <amrReadBytes+0xc2>
lea    -0x28(%rbp),%rdi
call   1060 <timerStart@plt>
movq   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jle    11e7 <amrReadBytes+0x97>
mov    -0x21(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11e2 <amrReadBytes+0x92>
jmp    120a <amrReadBytes+0xba>
jmp    11f1 <amrReadBytes+0xa1>
mov    $0xa,%edi
call   1050 <mssleep@plt>
jmp    11f6 <amrReadBytes+0xa6>
lea    -0x28(%rbp),%rdi
call   1070 <timerLap@plt>
cmp    $0x3e8,%eax
jl     1195 <amrReadBytes+0x45>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<amrReadBytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <assert@plt>
test   %r14,%r14
je     11d4 <amrReadBytes+0x84>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   1060 <timerStart@plt>
xor    %ebx,%ebx
lea    0xf(%rsp),%r13
jmp    11a9 <amrReadBytes+0x59>
nopl   0x0(%rax)
mov    $0xa,%edi
call   1050 <mssleep@plt>
mov    %r12,%rdi
call   1070 <timerLap@plt>
cmp    $0x3e8,%eax
jge    11d6 <amrReadBytes+0x86>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    1190 <amrReadBytes+0x40>
movzbl 0xf(%rsp),%eax
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %r14,%rbx
jne    119a <amrReadBytes+0x4a>
mov    %r14,%rbx
jmp    11d6 <amrReadBytes+0x86>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
define i64 @amrReadBytes(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8
%9 = alloca %struct.timeval
%10 = alloca i64
%11 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%12 = load i8*, i8** %6
%13 = call i32 @assert(i8* %12)
%14 = load i64, i64* %7
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i64 0, i64* %4
br label %44
17: ; preds = %3
%18 = call i32 @timerStart(%struct.timeval* %9)
store i64 0, i64* %10
br label %19
19: ; preds = %39, %17
%20 = load i32, i32* %5
%21 = call i64 @read(i32 %20, i8* %8, i32 1)
store i64 %21, i64* %11
%22 = load i64, i64* %11
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %36
24: ; preds = %19
%25 = load i8, i8* %8
%26 = load i8*, i8** %6
%27 = load i64, i64* %10
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 %25, i8* %28
%29 = load i64, i64* %10
%30 = add i64 %29, 1
store i64 %30, i64* %10
%31 = load i64, i64* %10
%32 = load i64, i64* %7
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %24
br label %42
35: ; preds = %24
br label %38
36: ; preds = %19
%37 = call i32 @mssleep(i32 10)
br label %38
38: ; preds = %36, %35
br label %39
39: ; preds = %38
%40 = call i32 @timerLap(%struct.timeval* %9)
%41 = icmp slt i32 %40, 1000
br i1 %41, label %19, label %42
42: ; preds = %39, %34
%43 = load i64, i64* %10
store i64 %43, i64* %4
br label %44
44: ; preds = %42, %16
%45 = load i64, i64* %4
ret i64 %45
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
define i64 @amrReadBytes(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8
%5 = alloca %struct.timeval
%6 = tail call i32 @assert(i8* %1)
%7 = icmp eq i64 %2, 0
br i1 %7, label %28, label %8
8: ; preds = %3
%9 = bitcast %struct.timeval* %5 to i8*
%10 = call i32 @timerStart(%struct.timeval* %5)
br label %11
11: ; preds = %22, %8
%12 = phi i64 [ 0, %8 ], [ %23, %22 ]
%13 = call i64 @read(i32 %0, i8* %4, i32 1)
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i8, i8* %4
%17 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 %16, i8* %17
%18 = add i64 %12, 1
%19 = icmp eq i64 %18, %2
br i1 %19, label %26, label %22
20: ; preds = %11
%21 = call i32 @mssleep(i32 10)
br label %22
22: ; preds = %20, %15
%23 = phi i64 [ %18, %15 ], [ %12, %20 ]
%24 = call i32 @timerLap(%struct.timeval* %5)
%25 = icmp slt i32 %24, 1000
br i1 %25, label %11, label %26
26: ; preds = %15, %22
%27 = phi i64 [ %23, %22 ], [ %2, %15 ]
br label %28
28: ; preds = %3, %26
%29 = phi i64 [ %27, %26 ], [ 0, %3 ]
ret i64 %29
}