#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*/ die(char *);
int read(int, void *, size_t);
void do_read(int fd, void *buf, size_t size) {
  while (size) {
    int ret = read(fd, buf, size);
    if (ret < 0)
      die("failed to read");
    if (ret == 0)
      die("failed to read: missing data");
    size -= ret;
    buf += ret;
  }
}


x86 assembly (clang, -O0):
<do_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a2 <do_read+0x82>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1167 <do_read+0x47>
lea    0xe9e(%rip),%rdi
call   1040 <die@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    117d <do_read+0x5d>
lea    0xe97(%rip),%rdi
call   1040 <die@plt>
movslq -0x1c(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1133 <do_read+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     117e <do_read+0x5e>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0xec0(%rip),%r12
lea    0xec8(%rip),%r13
jmp    1160 <do_read+0x40>
nopl   0x0(%rax)
call   1040 <die@plt>
movslq %ebp,%rax
add    %rax,%rbx
sub    %rax,%r14
je     117e <do_read+0x5e>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <read@plt>
mov    %eax,%ebp
mov    %r12,%rdi
test   %eax,%eax
js     1150 <do_read+0x30>
mov    %r13,%rdi
jne    1155 <do_read+0x35>
jmp    1150 <do_read+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [15 x i8] c"failed to read\00"
@.str.1 = [29 x i8] c"failed to read: missing data\00"
define void @do_read(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %20, label %5
5: ; preds = %3, %15
%6 = phi i8* [ %18, %15 ], [ %1, %3 ]
%7 = phi i64 [ %17, %15 ], [ %2, %3 ]
%8 = tail call i32 @read(i32 %0, i8* %6, i64 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %5
%11 = icmp eq i32 %8, 0
br i1 %11, label %12, label %15
12: ; preds = %10, %5
%13 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %5 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), %10 ]
%14 = tail call i32 @die(i8* %13)
br label %15
15: ; preds = %12, %10
%16 = sext i32 %8 to i64
%17 = sub i64 %7, %16
%18 = getelementptr i8, i8* %6, i64 %16
%19 = icmp eq i64 %17, 0
br i1 %19, label %20, label %5
20: ; preds = %15, %3
ret void
}