#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*/ epicfail(char *);
int read(int, char *, int);
int client_read(int fd, char *buf, int *buffer_pos) {
  if (4096 - *buffer_pos - 1 == 0)
    epicfail("buffer full");
  int bytes_read = read(fd, buf + *buffer_pos, 4096 - *buffer_pos - 1);
  if (bytes_read == 0)
    return 0;
  if (bytes_read == -1)
    return -1;
  *buffer_pos += bytes_read;
  return bytes_read;
}


x86 assembly (clang, -O0):
<client_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
mov    $0x1000,%eax
sub    (%rcx),%eax
sub    $0x1,%eax
cmp    $0x0,%eax
jne    1156 <client_read+0x36>
lea    0xeaf(%rip),%rdi
call   1040 <epicfail@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rax
mov    $0x1000,%edx
sub    (%rax),%edx
sub    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1193 <client_read+0x73>
movl   $0x0,-0x4(%rbp)
jmp    11ba <client_read+0x9a>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a9 <client_read+0x89>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ba <client_read+0x9a>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_read>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    (%rdx),%eax
cmp    $0xfff,%eax
jne    1144 <client_read+0x24>
lea    0xec4(%rip),%rdi
call   1040 <epicfail@plt>
mov    (%r14),%eax
cltq
add    %rax,%rbx
mov    $0xfff,%edx
sub    %eax,%edx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <read@plt>
mov    %eax,%ecx
add    $0x1,%ecx
cmp    $0x2,%ecx
jb     1167 <client_read+0x47>
add    %eax,(%r14)
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"buffer full\00"
define i32 @client_read(i32 %0, i8* nocapture %1, i32* nocapture %2) {
%4 = load i32, i32* %2
%5 = icmp eq i32 %4, 4095
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @epicfail(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %2
br label %9
9: ; preds = %6, %3
%10 = phi i32 [ %8, %6 ], [ %4, %3 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = sub i32 4095, %10
%14 = tail call i32 @read(i32 %0, i8* %12, i32 %13)
switch i32 %14, label %15 [
i32 0, label %18
i32 -1, label %18
]
15: ; preds = %9
%16 = load i32, i32* %2
%17 = add nsw i32 %16, %14
store i32 %17, i32* %2
br label %18
18: ; preds = %9, %9, %15
ret i32 %14
}