#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
size_t read(int, unsigned char *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int v4lconvert_helper_read(int fd, void *b, size_t count, char *progname) {
  unsigned char *buf = b;
  size_t ret, r = 0;
  while (r < count) {
    ret = read(fd, buf + r, count - r);
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      fprintf(stderr, "%s: error reading: %s\n", progname, strerror(errno));
      return -1;
    }
    if (ret == 0)
      exit(0);
    r += ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<v4lconvert_helper_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1223 <v4lconvert_helper_read+0xe3>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
sub    -0x38(%rbp),%rdx
call   1030 <read@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0xffffffffffffffff,-0x30(%rbp)
jne    1200 <v4lconvert_helper_read+0xc0>
mov    0x2e35(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11bb <v4lconvert_helper_read+0x7b>
jmp    1167 <v4lconvert_helper_read+0x27>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe11(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <v4lconvert_helper_read+0xea>
cmpq   $0x0,-0x30(%rbp)
jne    1212 <v4lconvert_helper_read+0xd2>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1167 <v4lconvert_helper_read+0x27>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<v4lconvert_helper_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rdx,%rdx
je     11ba <v4lconvert_helper_read+0x7a>
mov    %edi,%r13d
mov    %rcx,(%rsp)
xor    %r14d,%r14d
mov    0x2e74(%rip),%r15
mov    0x2e65(%rip),%r12
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdx,0x10(%rsp)
mov    %rsi,0x8(%rsp)
nopl   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1030 <read@plt>
cmp    $0xffffffffffffffff,%rax
jne    119f <v4lconvert_helper_read+0x5f>
mov    (%r15),%rdi
cmp    (%r12),%rdi
je     1180 <v4lconvert_helper_read+0x40>
jmp    11be <v4lconvert_helper_read+0x7e>
test   %rax,%rax
je     11f5 <v4lconvert_helper_read+0xb5>
add    %rax,%r14
mov    0x8(%rsp),%rsi
lea    (%rsi,%r14,1),%rbp
mov    0x10(%rsp),%rbx
sub    %r14,%rbx
ja     1180 <v4lconvert_helper_read+0x40>
xor    %eax,%eax
jmp    11e6 <v4lconvert_helper_read+0xa6>
mov    0x2e1b(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xe2d(%rip),%rsi
mov    %ebx,%edi
mov    (%rsp),%rdx
mov    %rax,%rcx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"%s: error reading: %s\0A\00"
define i32 @v4lconvert_helper_read(i32 %0, i8* %1, i64 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i64
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %7
store i8* %13, i8** %10
store i64 0, i64* %12
br label %14
14: ; preds = %45, %33, %4
%15 = load i64, i64* %12
%16 = load i64, i64* %8
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %49
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load i8*, i8** %10
%21 = load i64, i64* %12
%22 = getelementptr inbounds i8, i8* %20, i64 %21
%23 = load i64, i64* %8
%24 = load i64, i64* %12
%25 = sub i64 %23, %24
%26 = call i64 @read(i32 %19, i8* %22, i64 %25)
store i64 %26, i64* %11
%27 = load i64, i64* %11
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %40
29: ; preds = %18
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %14
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %9
%37 = load i64, i64* @errno
%38 = call i8* @strerror(i64 %37)
%39 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %36, i8* %38)
store i32 -1, i32* %5
br label %50
40: ; preds = %18
%41 = load i64, i64* %11
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %40
%46 = load i64, i64* %11
%47 = load i64, i64* %12
%48 = add i64 %47, %46
store i64 %48, i64* %12
br label %14
49: ; preds = %14
store i32 0, i32* %5
br label %50
50: ; preds = %49, %34
%51 = load i32, i32* %5
ret i32 %51
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [23 x i8] c"%s: error reading: %s\0A\00"
define i32 @v4lconvert_helper_read(i32 %0, i8* nocapture %1, i64 %2, i8* %3) {
%5 = icmp eq i64 %2, 0
br i1 %5, label %27, label %6
6: ; preds = %4, %22
%7 = phi i64 [ %26, %22 ], [ %2, %4 ]
%8 = phi i8* [ %25, %22 ], [ %1, %4 ]
%9 = phi i64 [ %23, %22 ], [ 0, %4 ]
br label %10
10: ; preds = %6, %12
%11 = tail call i64 @read(i32 %0, i8* %8, i64 %7)
switch i64 %11, label %22 [
i64 -1, label %12
i64 0, label %20
]
12: ; preds = %10
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %10, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i8* @strerror(i64 %13)
%19 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %18)
br label %27
20: ; preds = %10
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %10
%23 = add i64 %11, %9
%24 = icmp ult i64 %23, %2
%25 = getelementptr inbounds i8, i8* %1, i64 %23
%26 = sub i64 %2, %23
br i1 %24, label %6, label %27
27: ; preds = %22, %4, %16
%28 = phi i32 [ -1, %16 ], [ 0, %4 ], [ 0, %22 ]
ret i32 %28
}