#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 */
char SER_GetChar();
int /*<<< orphan*/ SER_PutChar(char);
int _read(int fd, char *ptr, int len) {
  char c;
  int i;
  for (i = 0; i < len; i++) {
    c = SER_GetChar();
    if (c == 0x0D)
      break;
    *ptr++ = c;
    SER_PutChar(c);
  }
  return (len - i);
}


x86 assembly (clang, -O0):
<_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    118c <_read+0x6c>
mov    $0x0,%al
call   1030 <SER_GetChar@plt>
mov    %al,-0x15(%rbp)
movsbl -0x15(%rbp),%eax
cmp    $0xd,%eax
jne    1161 <_read+0x41>
jmp    118c <_read+0x6c>
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movsbl -0x15(%rbp),%edi
call   1040 <SER_PutChar@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1139 <_read+0x19>
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
test   %edx,%edx
jle    1161 <_read+0x41>
mov    %rsi,%r15
mov    %r14d,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <SER_GetChar@plt>
cmp    $0xd,%al
je     1165 <_read+0x45>
mov    %al,(%r15,%rbp,1)
movsbl %al,%edi
call   1040 <SER_PutChar@plt>
add    $0x1,%rbp
cmp    %ebp,%ebx
jne    1140 <_read+0x20>
jmp    1167 <_read+0x47>
xor    %ebx,%ebx
jmp    1167 <_read+0x47>
mov    %ebp,%ebx
sub    %ebx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
br label %9
9: ; preds = %25, %3
%10 = load i32, i32* %8
%11 = load i32, i32* %6
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %28
13: ; preds = %9
%14 = call signext i8 (...) @SER_GetChar()
store i8 %14, i8* %7
%15 = load i8, i8* %7
%16 = sext i8 %15 to i32
%17 = icmp eq i32 %16, 13
br i1 %17, label %18, label %19
18: ; preds = %13
br label %28
19: ; preds = %13
%20 = load i8, i8* %7
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %5
store i8 %20, i8* %21
%23 = load i8, i8* %7
%24 = call i32 @SER_PutChar(i8 signext %23)
br label %25
25: ; preds = %19
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
store i32 %27, i32* %8
br label %9
28: ; preds = %18, %9
%29 = load i32, i32* %6
%30 = load i32, i32* %8
%31 = sub nsw i32 %29, %30
ret i32 %31
}

LLVM IR (-O2):
define i32 @_read(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %15
5: ; preds = %3, %10
%6 = phi i32 [ %13, %10 ], [ 0, %3 ]
%7 = phi i8* [ %11, %10 ], [ %1, %3 ]
%8 = tail call signext i8 (...) @SER_GetChar()
%9 = icmp eq i8 %8, 13
br i1 %9, label %15, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %8, i8* %7
%12 = tail call i32 @SER_PutChar(i8 signext %8)
%13 = add nuw nsw i32 %6, 1
%14 = icmp eq i32 %13, %2
br i1 %14, label %15, label %5
15: ; preds = %10, %5, %3
%16 = phi i32 [ 0, %3 ], [ %6, %5 ], [ %2, %10 ]
%17 = sub nsw i32 %2, %16
ret i32 %17
}