#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 /*<<< orphan*/ uint8_t;
typedef size_t uint32_t;
typedef int ssize_t;
typedef int int32_t;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int read(int, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ stdout;
bool serial_read(int32_t fd, uint8_t *buffer, uint32_t size) {
  uint32_t nbRead;
  ssize_t nbCurrentyRead;
  int32_t attempt;
  bool bResult;
  bResult = true;
  attempt = 0;
  nbRead = 0;
  while (nbRead < size) {
    nbCurrentyRead = read(fd, &(buffer[nbRead]), size - nbRead);
    if (nbCurrentyRead == -1) {
      fprintf(stdout, "Error on read data errno = %d\n", errno);
      bResult = false;
      break;
    } else if (nbCurrentyRead == 0) {
      attempt++;
      if (attempt > 2) {
        return false;
      }
    } else {
      nbRead += nbCurrentyRead;
    }
  }
  return bResult;
}


x86 assembly (clang, -O0):
<serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11f1 <serial_read+0xd1>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x20(%rbp),%rdx
call   1030 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11ad <serial_read+0x8d>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
lea    0xe64(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
jmp    11f1 <serial_read+0xd1>
cmpl   $0x0,-0x24(%rbp)
jne    11db <serial_read+0xbb>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x2,-0x28(%rbp)
jle    11d6 <serial_read+0xb6>
movl   $0x0,-0x4(%rbp)
jmp    11f7 <serial_read+0xd7>
jmp    11e7 <serial_read+0xc7>
movslq -0x24(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    11ec <serial_read+0xcc>
jmp    1149 <serial_read+0x29>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%r14d
test   %rdx,%rdx
je     11ad <serial_read+0x8d>
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r12d
xor    %r13d,%r13d
xor    %ebp,%ebp
jmp    1155 <serial_read+0x35>
cs nopw 0x0(%rax,%rax,1)
cmp    %rbx,%rbp
jae    11ad <serial_read+0x8d>
lea    (%r15,%rbp,4),%rsi
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r12d,%edi
call   1030 <read@plt>
test   %eax,%eax
je     1180 <serial_read+0x60>
cmp    $0xffffffff,%eax
je     118c <serial_read+0x6c>
cltq
add    %rax,%rbp
jmp    1150 <serial_read+0x30>
nopw   0x0(%rax,%rax,1)
cmp    $0x1,%r13d
jg     11aa <serial_read+0x8a>
add    $0x1,%r13d
jmp    1150 <serial_read+0x30>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
lea    0xe5b(%rip),%rsi
call   1040 <fprintf@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@stdout = global i32 0
@.str = [31 x i8] c"Error on read data errno = %d\0A\00"
@errno = global i32 0
define i32 @serial_read(i32 %0, i32* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %25, label %5
5: ; preds = %3, %21
%6 = phi i32 [ %23, %21 ], [ 0, %3 ]
%7 = phi i64 [ %22, %21 ], [ 0, %3 ]
%8 = getelementptr inbounds i32, i32* %1, i64 %7
%9 = sub i64 %2, %7
%10 = tail call i32 @read(i32 %0, i32* %8, i64 %9)
switch i32 %10, label %18 [
i32 -1, label %11
i32 0, label %15
]
11: ; preds = %5
%12 = load i32, i32* @stdout
%13 = load i32, i32* @errno
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %25
15: ; preds = %5
%16 = add nsw i32 %6, 1
%17 = icmp sgt i32 %6, 1
br i1 %17, label %25, label %21
18: ; preds = %5
%19 = sext i32 %10 to i64
%20 = add i64 %7, %19
br label %21
21: ; preds = %18, %15
%22 = phi i64 [ %7, %15 ], [ %20, %18 ]
%23 = phi i32 [ %16, %15 ], [ %6, %18 ]
%24 = icmp ult i64 %22, %2
br i1 %24, label %5, label %25
25: ; preds = %15, %21, %3, %11
%26 = phi i32 [ 0, %11 ], [ 1, %3 ], [ 0, %15 ], [ 1, %21 ]
ret i32 %26
}