#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*/ F_SETFL;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, char *, int const);
int serial_read(int fd, char *buf, const int size) {
  fcntl(fd, F_SETFL, 0);
  int s, cur = 0;
  while (cur < size) {
    s = read(fd, buf + cur, size - cur);
    if (s <= 0)
      return -1;
    cur += s;
  }
  return 0;
}


x86 assembly (clang, -O0):
<serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1198 <serial_read+0x78>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     118a <serial_read+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    119f <serial_read+0x7f>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    114c <serial_read+0x2c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%r12d
mov    0x2ea9(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %ebx,%ebx
jle    1178 <serial_read+0x58>
xor    %ebp,%ebp
nopl   0x0(%rax)
movslq %ebp,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1040 <read@plt>
test   %eax,%eax
jle    1172 <serial_read+0x52>
add    %eax,%ebp
cmp    %ebx,%ebp
jl     1150 <serial_read+0x30>
xor    %r15d,%r15d
jmp    1178 <serial_read+0x58>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@F_SETFL = global i32 0
define i32 @serial_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = load i32, i32* @F_SETFL
%12 = call i32 @fcntl(i32 %10, i32 %11, i32 0)
store i32 0, i32* %9
br label %13
13: ; preds = %30, %3
%14 = load i32, i32* %9
%15 = load i32, i32* %7
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %34
17: ; preds = %13
%18 = load i32, i32* %5
%19 = load i8*, i8** %6
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i32, i32* %7
%24 = load i32, i32* %9
%25 = sub nsw i32 %23, %24
%26 = call i32 @read(i32 %18, i8* %22, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %17
store i32 -1, i32* %4
br label %35
30: ; preds = %17
%31 = load i32, i32* %8
%32 = load i32, i32* %9
%33 = add nsw i32 %32, %31
store i32 %33, i32* %9
br label %13
34: ; preds = %13
store i32 0, i32* %4
br label %35
35: ; preds = %34, %29
%36 = load i32, i32* %4
ret i32 %36
}

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