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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
size_t read(int, char *, size_t);
ssize_t misc_read_buf(int fd, char *buf, size_t count) {
  ssize_t rc, done;
  for (done = 0; done < (ssize_t)count; done += rc) {
    rc = read(fd, &buf[done], count - done);
    if (rc == -1 && errno == EINTR)
      continue;
    if (rc == -1)
      return -1;
    if (rc == 0)
      break;
  }
  return done;
}


x86 assembly (clang, -O0):
<misc_read_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    11bd <misc_read_buf+0xad>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
add    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
sub    -0x30(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    117f <misc_read_buf+0x6f>
mov    0x2e79(%rip),%rax
mov    (%rax),%rax
mov    0x2e67(%rip),%rcx
cmp    (%rcx),%rax
jne    117f <misc_read_buf+0x6f>
jmp    11ac <misc_read_buf+0x9c>
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1197 <misc_read_buf+0x87>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c5 <misc_read_buf+0xb5>
cmpq   $0x0,-0x28(%rbp)
jne    11a7 <misc_read_buf+0x97>
jmp    11bd <misc_read_buf+0xad>
jmp    11ac <misc_read_buf+0x9c>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    112b <misc_read_buf+0x1b>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_read_buf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1170 <misc_read_buf+0x60>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
mov    0x2eaf(%rip),%r12
mov    0x2ea0(%rip),%r13
jmp    1152 <misc_read_buf+0x42>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%rcx
cmp    0x0(%r13),%rcx
jne    1174 <misc_read_buf+0x64>
add    %rax,%rbx
cmp    %r14,%rbx
jae    117b <misc_read_buf+0x6b>
lea    (%r15,%rbx,1),%rsi
mov    %r14,%rdx
sub    %rbx,%rdx
mov    %ebp,%edi
call   1030 <read@plt>
cmp    $0xffffffffffffffff,%rax
je     1140 <misc_read_buf+0x30>
test   %rax,%rax
jne    114a <misc_read_buf+0x3a>
jmp    117b <misc_read_buf+0x6b>
xor    %ebx,%ebx
jmp    117b <misc_read_buf+0x6b>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define i64 @misc_read_buf(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %14
%6 = phi i64 [ %15, %14 ], [ 0, %3 ]
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = sub i64 %2, %6
%9 = tail call i64 @read(i32 %0, i8* %7, i64 %8)
switch i64 %9, label %14 [
i64 -1, label %10
i64 0, label %17
]
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %17
14: ; preds = %5, %10
%15 = add i64 %9, %6
%16 = icmp ult i64 %15, %2
br i1 %16, label %5, label %17
17: ; preds = %14, %10, %5, %3
%18 = phi i64 [ 0, %3 ], [ %6, %5 ], [ -1, %10 ], [ %15, %14 ]
ret i64 %18
}