#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 scalar_t__ uint32_t;
typedef scalar_t__ ssize_t;

/* Variables and functions */
int EINVAL;
int errno;
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, scalar_t__);
scalar_t__ read(int, char *, scalar_t__);
int invoke_raw_read(int fd, void *buffer, uint32_t size) {
  uint32_t cnt = size;
  char *buf = (char *)buffer;
  if (NULL == buffer || 0 == size)
    return EINVAL;
  while (cnt > 0) {
    const ssize_t result = read(fd, buf, cnt);
    if (result > 0) {
      buf += result;
      cnt -= result;
    } else {
      memset(buf, 0, cnt);
      return errno;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<invoke_raw_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
je     115b <invoke_raw_read+0x3b>
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jne    116c <invoke_raw_read+0x4c>
mov    0x2e76(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <invoke_raw_read+0xc7>
jmp    1171 <invoke_raw_read+0x51>
cmpq   $0x0,-0x20(%rbp)
jle    11e0 <invoke_raw_read+0xc0>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jle    11bb <invoke_raw_read+0x9b>
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
jmp    11db <invoke_raw_read+0xbb>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11e7 <invoke_raw_read+0xc7>
jmp    1171 <invoke_raw_read+0x51>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invoke_raw_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     116c <invoke_raw_read+0x4c>
mov    %rdx,%r14
test   %rdx,%rdx
je     116c <invoke_raw_read+0x4c>
xor    %r15d,%r15d
test   %r14,%r14
jle    118c <invoke_raw_read+0x6c>
mov    %rsi,%rbx
mov    %edi,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <read@plt>
test   %rax,%rax
jle    1175 <invoke_raw_read+0x55>
add    %rax,%rbx
sub    %rax,%r14
jg     1150 <invoke_raw_read+0x30>
jmp    118c <invoke_raw_read+0x6c>
mov    0x2e65(%rip),%rax
jmp    1189 <invoke_raw_read+0x69>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @invoke_raw_read(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i64, i64* %7
store i64 %11, i64* %8
%12 = load i8*, i8** %6
store i8* %12, i8** %9
%13 = load i8*, i8** %6
%14 = icmp eq i8* null, %13
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = load i64, i64* %7
%17 = icmp eq i64 0, %16
br i1 %17, label %18, label %20
18: ; preds = %15, %3
%19 = load i32, i32* @EINVAL
store i32 %19, i32* %4
br label %45
20: ; preds = %15
br label %21
21: ; preds = %43, %20
%22 = load i64, i64* %8
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %44
24: ; preds = %21
%25 = load i32, i32* %5
%26 = load i8*, i8** %9
%27 = load i64, i64* %8
%28 = call i64 @read(i32 %25, i8* %26, i64 %27)
store i64 %28, i64* %10
%29 = load i64, i64* %10
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %24
%32 = load i64, i64* %10
%33 = load i8*, i8** %9
%34 = getelementptr inbounds i8, i8* %33, i64 %32
store i8* %34, i8** %9
%35 = load i64, i64* %10
%36 = load i64, i64* %8
%37 = sub nsw i64 %36, %35
store i64 %37, i64* %8
br label %43
38: ; preds = %24
%39 = load i8*, i8** %9
%40 = load i64, i64* %8
%41 = call i32 @memset(i8* %39, i32 0, i64 %40)
%42 = load i32, i32* @errno
store i32 %42, i32* %4
br label %45
43: ; preds = %31
br label %21
44: ; preds = %21
store i32 0, i32* %4
br label %45
45: ; preds = %44, %38, %18
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @invoke_raw_read(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %1, null
%5 = icmp eq i64 %2, 0
%6 = or i1 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %3
%8 = icmp sgt i64 %2, 0
br i1 %8, label %11, label %23
9: ; preds = %3
%10 = load i32, i32* @EINVAL
br label %23
11: ; preds = %7, %19
%12 = phi i8* [ %20, %19 ], [ %1, %7 ]
%13 = phi i64 [ %21, %19 ], [ %2, %7 ]
%14 = tail call i64 @read(i32 %0, i8* %12, i64 %13)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = tail call i32 @memset(i8* %12, i32 0, i64 %13)
%18 = load i32, i32* @errno
br label %23
19: ; preds = %11
%20 = getelementptr inbounds i8, i8* %12, i64 %14
%21 = sub nsw i64 %13, %14
%22 = icmp sgt i64 %21, 0
br i1 %22, label %11, label %23
23: ; preds = %19, %7, %16, %9
%24 = phi i32 [ %10, %9 ], [ %18, %16 ], [ 0, %7 ], [ 0, %19 ]
ret i32 %24
}