#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, void *, int);
int readchunk(int fd, char *buffy, int size) {
  char *ptr = buffy;
  int n, total = 0, eof = 0;
  while (!eof && total < size) {
    n = read(fd, (void *)ptr, size - total);
    switch (n) {
    case -1:
      perror("read");
      exit(0);
    case 0:
      eof = 1;
      break;
    default:
      ptr += n;
      total += n;
      break;
    }
  }
  return total;
}


x86 assembly (clang, -O0):
<readchunk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x2d(%rbp)
jne    1173 <readchunk+0x43>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    1183 <readchunk+0x53>
jmp    11fc <readchunk+0xcc>
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x28(%rbp),%eax
sub    %eax,%edx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0xffffffff,%eax
je     11be <readchunk+0x8e>
jmp    11ae <readchunk+0x7e>
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     11d1 <readchunk+0xa1>
jmp    11dd <readchunk+0xad>
lea    0xe3b(%rip),%rdi
call   1040 <perror@plt>
xor    %edi,%edi
call   1050 <exit@plt>
movl   $0x1,-0x2c(%rbp)
jmp    11f7 <readchunk+0xc7>
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1158 <readchunk+0x28>
mov    -0x28(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readchunk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1178 <readchunk+0x48>
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edx
sub    %ebx,%edx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1030 <read@plt>
test   %eax,%eax
je     117a <readchunk+0x4a>
cmp    $0xffffffff,%eax
je     1187 <readchunk+0x57>
movslq %eax,%rcx
add    %rcx,%rbp
add    %eax,%ebx
cmp    %r14d,%ebx
jl     1150 <readchunk+0x20>
jmp    117a <readchunk+0x4a>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe72(%rip),%rdi
call   1040 <perror@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
define i32 @readchunk(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load i8*, i8** %5
store i8* %11, i8** %7
store i32 0, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %41, %3
%13 = load i32, i32* %10
%14 = icmp ne i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = load i32, i32* %9
%17 = load i32, i32* %6
%18 = icmp slt i32 %16, %17
br label %19
19: ; preds = %15, %12
%20 = phi i1 [ false, %12 ], [ %18, %15 ]
br i1 %20, label %21, label %42
21: ; preds = %19
%22 = load i32, i32* %4
%23 = load i8*, i8** %7
%24 = load i32, i32* %6
%25 = load i32, i32* %9
%26 = sub nsw i32 %24, %25
%27 = call i32 @read(i32 %22, i8* %23, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
switch i32 %28, label %33 [
i32 -1, label %29
i32 0, label %32
]
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %21
store i32 1, i32* %10
br label %41
33: ; preds = %21
%34 = load i32, i32* %8
%35 = load i8*, i8** %7
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %7
%38 = load i32, i32* %8
%39 = load i32, i32* %9
%40 = add nsw i32 %39, %38
store i32 %40, i32* %9
br label %41
41: ; preds = %33, %32
br label %12
42: ; preds = %19
%43 = load i32, i32* %9
ret i32 %43
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
define i32 @readchunk(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 i32 [ %16, %13 ], [ 0, %3 ]
%7 = phi i8* [ %15, %13 ], [ %1, %3 ]
%8 = sub nsw i32 %2, %6
%9 = tail call i32 @read(i32 %0, i8* %7, i32 %8)
switch i32 %9, label %13 [
i32 -1, label %10
i32 0, label %18
]
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %5
%14 = sext i32 %9 to i64
%15 = getelementptr inbounds i8, i8* %7, i64 %14
%16 = add nsw i32 %9, %6
%17 = icmp slt i32 %16, %2
br i1 %17, label %5, label %18
18: ; preds = %5, %13, %3
%19 = phi i32 [ 0, %3 ], [ %6, %5 ], [ %16, %13 ]
ret i32 %19
}