#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 */

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
int readData(int fd, void *ptr, int len) {
  unsigned char *data = ptr;
  int r;
  while (len > 0) {
    r = read(fd, data, len);
    if (r <= 0) {
      fprintf(stderr, "*UNDERFLOW*\n");
      return -1;
    }
    len -= r;
    data += r;
  }
  return 0;
}


x86 assembly (clang, -O0):
<readData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11a2 <readData+0x82>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     1181 <readData+0x61>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <readData+0x89>
mov    -0x24(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    113a <readData+0x1a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readData>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
jle    1177 <readData+0x57>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    115c <readData+0x3c>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%ebp
jg     1140 <readData+0x20>
jmp    1177 <readData+0x57>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
lea    0xe94(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"*UNDERFLOW*\0A\00"
define i32 @readData(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %6
store i8* %10, i8** %8
br label %11
11: ; preds = %24, %3
%12 = load i32, i32* %7
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %32
14: ; preds = %11
%15 = load i32, i32* %5
%16 = load i8*, i8** %8
%17 = load i32, i32* %7
%18 = call i32 @read(i32 %15, i8* %16, i32 %17)
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp sle i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %33
24: ; preds = %14
%25 = load i32, i32* %9
%26 = load i32, i32* %7
%27 = sub nsw i32 %26, %25
store i32 %27, i32* %7
%28 = load i32, i32* %9
%29 = load i8*, i8** %8
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %29, i64 %30
store i8* %31, i8** %8
br label %11
32: ; preds = %11
store i32 0, i32* %4
br label %33
33: ; preds = %32, %21
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"*UNDERFLOW*\0A\00"
define i32 @readData(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %18
5: ; preds = %3, %13
%6 = phi i8* [ %16, %13 ], [ %1, %3 ]
%7 = phi i32 [ %14, %13 ], [ %2, %3 ]
%8 = tail call i32 @read(i32 %0, i8* %6, i32 %7)
%9 = icmp slt i32 %8, 1
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %18
13: ; preds = %5
%14 = sub nsw i32 %7, %8
%15 = zext i32 %8 to i64
%16 = getelementptr inbounds i8, i8* %6, i64 %15
%17 = icmp sgt i32 %14, 0
br i1 %17, label %5, label %18
18: ; preds = %13, %3, %10
%19 = phi i32 [ -1, %10 ], [ 0, %3 ], [ 0, %13 ]
ret i32 %19
}