#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*/ STDERR_FILENO;
int /*<<< orphan*/ dprintf(int /*<<< orphan*/, char *, ...);
int errno;
int /*<<< orphan*/ msleep(int);
int read(int, unsigned char *, int);
int /*<<< orphan*/ strerror(int);
int read_serial(int fd, unsigned char *buf, unsigned int size) {
  int result_bytes = size;
  int readcnt = 0, bufptr = 0, progress = 0;
  unsigned int tick_size = size / 64;
  if (tick_size < 1024)
    tick_size = 1024;
  unsigned int ticks = size / tick_size;
  unsigned char *blank =
      "                                                                ";
  if (ticks > 1) {
    dprintf(STDERR_FILENO, "[%s]\r[", blank + (64 - ticks));
  }
  while (bufptr < result_bytes) {
    readcnt = read(fd, buf + bufptr, result_bytes - bufptr);
    if (readcnt < 0 && errno != 11)
      dprintf(STDERR_FILENO, "readcnd less than zero: (%d) %s\n", errno,
              strerror(errno));
    if (readcnt > 0) {
      bufptr += readcnt;
      progress += readcnt;
      if (progress >= tick_size) {
        progress -= tick_size;
        dprintf(STDERR_FILENO, ".");
      }
    }
    msleep(16);
  }
  dprintf(STDERR_FILENO, "\nRead %d bytes\n", bufptr);
  return errno;
}


x86 assembly (clang, -O0):
<read_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x14(%rbp),%eax
shr    $0x6,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x400,-0x28(%rbp)
jae    118a <read_serial+0x4a>
movl   $0x400,-0x28(%rbp)
mov    -0x14(%rbp),%eax
xor    %edx,%edx
divl   -0x28(%rbp)
mov    %eax,-0x2c(%rbp)
lea    0xe64(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x1,-0x2c(%rbp)
jbe    11d2 <read_serial+0x92>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
mov    $0x40,%eax
sub    -0x2c(%rbp),%eax
mov    %eax,%eax
add    %rax,%rdx
lea    0xe76(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
jmp    11d7 <read_serial+0x97>
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12b3 <read_serial+0x173>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
sub    -0x20(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1255 <read_serial+0x115>
mov    0x2dd0(%rip),%rax
cmpl   $0xb,(%rax)
je     1255 <read_serial+0x115>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    12a4 <read_serial+0x164>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jb     129f <read_serial+0x15f>
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
jmp    12a4 <read_serial+0x164>
mov    $0x10,%edi
call   1030 <msleep@plt>
jmp    11d7 <read_serial+0x97>
mov    0x2d1e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1040 <dprintf@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_serial>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,0x4(%rsp)
mov    %edx,%eax
shr    $0x6,%eax
cmp    $0x401,%eax
mov    $0x400,%ecx
cmovae %eax,%ecx
xor    %ebp,%ebp
mov    %edx,%eax
xor    %edx,%edx
mov    %ecx,(%rsp)
div    %ecx
cmp    $0x2,%eax
jb     119f <read_serial+0x5f>
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%edi
mov    $0x40,%ecx
sub    %eax,%ecx
lea    0xe72(%rip),%rdx
add    %rcx,%rdx
lea    0xea9(%rip),%rsi
xor    %eax,%eax
call   1040 <dprintf@plt>
test   %r15d,%r15d
jle    1243 <read_serial+0x103>
xor    %ebp,%ebp
xor    %r14d,%r14d
jmp    11e7 <read_serial+0xa7>
nop
mov    0x2e21(%rip),%rax
mov    (%rax),%r12d
mov    %r13d,%edi
call   1060 <strerror@plt>
mov    %r12d,%edi
lea    0xe7c(%rip),%rsi
mov    %r13d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <dprintf@plt>
mov    $0x10,%edi
call   1030 <msleep@plt>
cmp    %r15d,%ebp
jge    1243 <read_serial+0x103>
movslq %ebp,%rax
lea    (%rbx,%rax,1),%rsi
mov    %r15d,%edx
sub    %eax,%edx
mov    0x4(%rsp),%edi
call   1050 <read@plt>
test   %eax,%eax
jns    1210 <read_serial+0xd0>
mov    0x2dd9(%rip),%rcx
mov    (%rcx),%r13d
cmp    $0xb,%r13d
jne    11b0 <read_serial+0x70>
test   %eax,%eax
jle    11d8 <read_serial+0x98>
mov    %rbx,%r12
add    %eax,%ebp
add    %eax,%r14d
mov    %r14d,%ebx
sub    (%rsp),%ebx
jb     123e <read_serial+0xfe>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
xor    %eax,%eax
call   1040 <dprintf@plt>
mov    %ebx,%r14d
mov    %r12,%rbx
jmp    11d8 <read_serial+0x98>
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <dprintf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [65 x i8] c" \00"
@STDERR_FILENO = global i32 0
@.str.1 = [7 x i8] c"[%s]\0D[\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"readcnd less than zero: (%d) %s\0A\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [16 x i8] c"\0ARead %d bytes\0A\00"
define i32 @read_serial(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%14 = load i32, i32* %6
store i32 %14, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%15 = load i32, i32* %6
%16 = udiv i32 %15, 64
store i32 %16, i32* %11
%17 = load i32, i32* %11
%18 = icmp ult i32 %17, 1024
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 1024, i32* %11
br label %20
20: ; preds = %19, %3
%21 = load i32, i32* %6
%22 = load i32, i32* %11
%23 = udiv i32 %21, %22
store i32 %23, i32* %12
store i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str, i64 0, i64 0), i8** %13
%24 = load i32, i32* %12
%25 = icmp ugt i32 %24, 1
br i1 %25, label %26, label %34
26: ; preds = %20
%27 = load i32, i32* @STDERR_FILENO
%28 = load i8*, i8** %13
%29 = load i32, i32* %12
%30 = sub i32 64, %29
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = call i32 (i32, i8*, ...) @dprintf(i32 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %26, %20
br label %35
35: ; preds = %80, %34
%36 = load i32, i32* %9
%37 = load i32, i32* %7
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %82
39: ; preds = %35
%40 = load i32, i32* %4
%41 = load i8*, i8** %5
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i32, i32* %7
%46 = load i32, i32* %9
%47 = sub nsw i32 %45, %46
%48 = call i32 @read(i32 %40, i8* %44, i32 %47)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %60
51: ; preds = %39
%52 = load i32, i32* @errno
%53 = icmp ne i32 %52, 11
br i1 %53, label %54, label %60
54: ; preds = %51
%55 = load i32, i32* @STDERR_FILENO
%56 = load i32, i32* @errno
%57 = load i32, i32* @errno
%58 = call i32 @strerror(i32 %57)
%59 = call i32 (i32, i8*, ...) @dprintf(i32 %55, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %56, i32 %58)
br label %60
60: ; preds = %54, %51, %39
%61 = load i32, i32* %8
%62 = icmp sgt i32 %61, 0
br i1 %62, label %63, label %80
63: ; preds = %60
%64 = load i32, i32* %8
%65 = load i32, i32* %9
%66 = add nsw i32 %65, %64
store i32 %66, i32* %9
%67 = load i32, i32* %8
%68 = load i32, i32* %10
%69 = add nsw i32 %68, %67
store i32 %69, i32* %10
%70 = load i32, i32* %10
%71 = load i32, i32* %11
%72 = icmp uge i32 %70, %71
br i1 %72, label %73, label %79
73: ; preds = %63
%74 = load i32, i32* %11
%75 = load i32, i32* %10
%76 = sub i32 %75, %74
store i32 %76, i32* %10
%77 = load i32, i32* @STDERR_FILENO
%78 = call i32 (i32, i8*, ...) @dprintf(i32 %77, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %79
79: ; preds = %73, %63
br label %80
80: ; preds = %79, %60
%81 = call i32 @msleep(i32 16)
br label %35
82: ; preds = %35
%83 = load i32, i32* @STDERR_FILENO
%84 = load i32, i32* %9
%85 = call i32 (i32, i8*, ...) @dprintf(i32 %83, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %84)
%86 = load i32, i32* @errno
ret i32 %86
}

LLVM IR (-O2):
@.str = [65 x i8] c" \00"
@STDERR_FILENO = global i32 0
@.str.1 = [7 x i8] c"[%s]\0D[\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"readcnd less than zero: (%d) %s\0A\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [16 x i8] c"\0ARead %d bytes\0A\00"
define i32 @read_serial(i32 %0, i8* nocapture %1, i32 %2) {
%4 = lshr i32 %2, 6
%5 = icmp ugt i32 %4, 1024
%6 = select i1 %5, i32 %4, i32 1024
%7 = udiv i32 %2, %6
%8 = icmp ugt i32 %7, 1
br i1 %8, label %9, label %15
9: ; preds = %3
%10 = load i32, i32* @STDERR_FILENO
%11 = sub nsw i32 64, %7
%12 = zext i32 %11 to i64
%13 = getelementptr inbounds [65 x i8], [65 x i8]* @.str, i64 0, i64 %12
%14 = tail call i32 (i32, i8*, ...) @dprintf(i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %9, %3
%16 = icmp sgt i32 %2, 0
br i1 %16, label %17, label %47
17: ; preds = %15, %42
%18 = phi i32 [ %44, %42 ], [ 0, %15 ]
%19 = phi i32 [ %43, %42 ], [ 0, %15 ]
%20 = sext i32 %18 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = sub nsw i32 %2, %18
%23 = tail call i32 @read(i32 %0, i8* %21, i32 %22)
%24 = icmp slt i32 %23, 0
%25 = load i32, i32* @errno
%26 = icmp ne i32 %25, 11
%27 = select i1 %24, i1 %26, i1 false
br i1 %27, label %28, label %32
28: ; preds = %17
%29 = load i32, i32* @STDERR_FILENO
%30 = tail call i32 @strerror(i32 %25)
%31 = tail call i32 (i32, i8*, ...) @dprintf(i32 %29, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %30)
br label %42
32: ; preds = %17
%33 = icmp sgt i32 %23, 0
br i1 %33, label %34, label %42
34: ; preds = %32
%35 = add nsw i32 %23, %18
%36 = add nsw i32 %23, %19
%37 = icmp ult i32 %36, %6
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = sub i32 %36, %6
%40 = load i32, i32* @STDERR_FILENO
%41 = tail call i32 (i32, i8*, ...) @dprintf(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %42
42: ; preds = %28, %34, %38, %32
%43 = phi i32 [ %39, %38 ], [ %36, %34 ], [ %19, %32 ], [ %19, %28 ]
%44 = phi i32 [ %35, %38 ], [ %35, %34 ], [ %18, %32 ], [ %18, %28 ]
%45 = tail call i32 @msleep(i32 16)
%46 = icmp slt i32 %44, %2
br i1 %46, label %17, label %47
47: ; preds = %42, %15
%48 = phi i32 [ 0, %15 ], [ %44, %42 ]
%49 = load i32, i32* @STDERR_FILENO
%50 = tail call i32 (i32, i8*, ...) @dprintf(i32 %49, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %48)
%51 = load i32, i32* @errno
ret i32 %51
}