#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 */
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ jmp_buf;

/* Variables and functions */
int /*<<< orphan*/ longjmp(int /*<<< orphan*/, int);
scalar_t__ read(int, unsigned char *, size_t);
int /*<<< orphan*/ usleep(int);
void blocking_read(int fd, jmp_buf *jumpTarget, void *data, size_t len) {
  unsigned char *bytes = (unsigned char *)data;
  size_t remaining = len;
  size_t offset = 0;
  ssize_t rc;
  while (remaining > 0) {
    rc = read(fd, bytes + offset, remaining);
    if (rc < 0) {
      longjmp(*jumpTarget, 1);
    } else if (rc == 0) {
      usleep(1000 * 5);
    } else {
      remaining -= rc;
      offset += rc;
    }
  }
}


x86 assembly (clang, -O0):
<blocking_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11dc <blocking_read+0xac>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jge    119d <blocking_read+0x6d>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    $0x1,%esi
call   1040 <longjmp@plt>
cmpq   $0x0,-0x40(%rbp)
jne    11b7 <blocking_read+0x87>
mov    $0x1388,%edi
call   1050 <usleep@plt>
jmp    11d2 <blocking_read+0xa2>
mov    -0x40(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11d7 <blocking_read+0xa7>
jmp    115f <blocking_read+0x2f>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<blocking_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rcx,%rcx
je     1180 <blocking_read+0x50>
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %edi,%r12d
xor    %ebp,%ebp
jmp    115b <blocking_read+0x2b>
nopl   (%rax)
sub    %rax,%rbx
add    %rax,%rbp
test   %rbx,%rbx
je     1180 <blocking_read+0x50>
lea    (%r15,%rbp,1),%rsi
mov    %r12d,%edi
mov    %rbx,%rdx
call   1030 <read@plt>
test   %rax,%rax
js     1189 <blocking_read+0x59>
jne    1150 <blocking_read+0x20>
mov    $0x1388,%edi
call   1050 <usleep@plt>
test   %rbx,%rbx
jne    115b <blocking_read+0x2b>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r14),%edi
mov    $0x1,%esi
call   1040 <longjmp@plt>

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

LLVM IR (-O2):
define void @blocking_read(i32 %0, i32* nocapture %1, i8* nocapture %2, i64 %3) {
%5 = icmp eq i64 %3, 0
br i1 %5, label %26, label %6
6: ; preds = %4, %22
%7 = phi i64 [ %24, %22 ], [ %3, %4 ]
%8 = phi i64 [ %23, %22 ], [ 0, %4 ]
%9 = getelementptr inbounds i8, i8* %2, i64 %8
%10 = tail call i64 @read(i32 %0, i8* %9, i64 %7)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %6
%13 = load i32, i32* %1
%14 = tail call i32 @longjmp(i32 %13, i32 1)
unreachable
15: ; preds = %6
%16 = icmp eq i64 %10, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @usleep(i32 5000)
br label %22
19: ; preds = %15
%20 = sub i64 %7, %10
%21 = add i64 %10, %8
br label %22
22: ; preds = %17, %19
%23 = phi i64 [ %8, %17 ], [ %21, %19 ]
%24 = phi i64 [ %7, %17 ], [ %20, %19 ]
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %6
26: ; preds = %22, %4
ret void
}