#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 int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EBADMSG;
int /*<<< orphan*/ ECONNABORTED;
int /*<<< orphan*/ errno;
int read(int, void *, int);
ssize_t read_chksize(int fd, void *buf, ssize_t size) {
  ssize_t ret = read(fd, buf, size);
  if (ret == size)
    return ret;
  else if (ret == 0)
    errno = ECONNABORTED;
  else if (ret == -1)
    ;
  else
    errno = EBADMSG;
  return -1;
}


x86 assembly (clang, -O0):
<read_chksize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    114b <read_chksize+0x3b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119e <read_chksize+0x8e>
cmpl   $0x0,-0x18(%rbp)
jne    116c <read_chksize+0x5c>
mov    0x2e74(%rip),%rax
mov    (%rax),%ecx
mov    0x2e73(%rip),%rax
mov    %ecx,(%rax)
jmp    1192 <read_chksize+0x82>
cmpl   $0xffffffff,-0x18(%rbp)
jne    117b <read_chksize+0x6b>
jmp    118d <read_chksize+0x7d>
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4d(%rip),%rax
mov    %ecx,(%rax)
jmp    1192 <read_chksize+0x82>
jmp    1197 <read_chksize+0x87>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_chksize>:
push   %rbx
mov    %edx,%ebx
call   1030 <read@plt>
cmp    %ebx,%eax
je     1149 <read_chksize+0x39>
cmp    $0xffffffff,%eax
je     112e <read_chksize+0x1e>
test   %eax,%eax
jne    1132 <read_chksize+0x22>
mov    0x2ea4(%rip),%rax
jmp    1139 <read_chksize+0x29>
mov    %eax,%ebx
jmp    1149 <read_chksize+0x39>
mov    0x2eaf(%rip),%rax
mov    (%rax),%eax
mov    0x2e96(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@ECONNABORTED = global i32 0
@errno = global i32 0
@EBADMSG = global i32 0
define i32 @read_chksize(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* %5
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = call i32 @read(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = load i32, i32* %7
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* %8
store i32 %17, i32* %4
br label %32
18: ; preds = %3
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i32, i32* @ECONNABORTED
store i32 %22, i32* @errno
br label %30
23: ; preds = %18
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %27
26: ; preds = %23
br label %29
27: ; preds = %23
%28 = load i32, i32* @EBADMSG
store i32 %28, i32* @errno
br label %29
29: ; preds = %27, %26
br label %30
30: ; preds = %29, %21
br label %31
31: ; preds = %30
store i32 -1, i32* %4
br label %32
32: ; preds = %31, %16
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@ECONNABORTED = global i32 0
@errno = global i32 0
@EBADMSG = global i32 0
define i32 @read_chksize(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%5 = icmp eq i32 %4, %2
br i1 %5, label %11, label %6
6: ; preds = %3
switch i32 %4, label %7 [
i32 0, label %8
i32 -1, label %11
]
7: ; preds = %6
br label %8
8: ; preds = %6, %7
%9 = phi i32* [ @EBADMSG, %7 ], [ @ECONNABORTED, %6 ]
%10 = load i32, i32* %9
store i32 %10, i32* @errno
br label %11
11: ; preds = %8, %6, %3
%12 = phi i32 [ %2, %3 ], [ %4, %6 ], [ -1, %8 ]
ret i32 %12
}