#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*/ perror(char *);
int read(int, void *, int);
int bp_serial_readto(int fd, void *buf, unsigned bytes) {
  int n = bytes;
  int retry = 0;
  while (n > 0 && retry < 3) {
    int er = read(fd, buf, n);
    if (er == -1) {
      perror("Read fault");
      return -1;
    }
    if (er == 0) {
      retry++;
    }
    n -= er;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<bp_serial_readto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x21(%rbp)
jle    1158 <bp_serial_readto+0x38>
cmpl   $0x3,-0x1c(%rbp)
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1168 <bp_serial_readto+0x48>
jmp    11bf <bp_serial_readto+0x9f>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    119c <bp_serial_readto+0x7c>
lea    0xe75(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <bp_serial_readto+0xa5>
cmpl   $0x0,-0x20(%rbp)
jne    11af <bp_serial_readto+0x8f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    113f <bp_serial_readto+0x1f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bp_serial_readto>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
test   %edx,%edx
jle    1161 <bp_serial_readto+0x41>
mov    %rsi,%r15
mov    %edi,%r12d
xor    %ebp,%ebp
mov    %r14d,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     116d <bp_serial_readto+0x4d>
cmp    $0x1,%eax
adc    $0x0,%ebp
sub    %eax,%ebx
jle    1161 <bp_serial_readto+0x41>
cmp    $0x3,%ebp
jb     1140 <bp_serial_readto+0x20>
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe8c(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    1161 <bp_serial_readto+0x41>

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

LLVM IR (-O2):
@.str = [11 x i8] c"Read fault\00"
define i32 @bp_serial_readto(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %20
5: ; preds = %3, %12
%6 = phi i32 [ %15, %12 ], [ 0, %3 ]
%7 = phi i32 [ %16, %12 ], [ %2, %3 ]
%8 = tail call i32 @read(i32 %0, i8* %1, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %20
12: ; preds = %5
%13 = icmp eq i32 %8, 0
%14 = zext i1 %13 to i32
%15 = add nuw nsw i32 %6, %14
%16 = sub nsw i32 %7, %8
%17 = icmp sgt i32 %16, 0
%18 = icmp ult i32 %15, 3
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %5, label %20
20: ; preds = %12, %3, %10
%21 = phi i32 [ -1, %10 ], [ %2, %3 ], [ %2, %12 ]
ret i32 %21
}