#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ memmove(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ printf(char *, size_t);
scalar_t__ read(int /*<<< orphan*/, int /*<<< orphan*/ *, unsigned int const);
size_t scan(int /*<<< orphan*/ *, size_t);
int /*<<< orphan*/ stderr;
void test_from_stdin(void) {
  const unsigned N = 2000;
  uint8_t buf[N];
  size_t n = 0;
  for (;;) {
    ssize_t r = read(STDIN_FILENO, &(buf[n]), N - n);
    if (r < 0) {
      fputs("read() error", stderr);
      exit(1);
    } else if (r == 0) {
      break;
    }
    n += (size_t)r;
    printf("\nread %lu bytes\n", n);
    if (n < 10) {
      break;
    }
    size_t d = 0, dd;
    do {
      dd = scan(&(buf[d]), n - d);
      d += dd;
    } while (dd > 0);
    printf("processed %lu bytes\n", d);
    memmove(buf, &(buf[d]), n - d);
    n = n - d;
  }
  printf("%lu trailing bytes\n", n);
}


x86 assembly (clang, -O0):
<test_from_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1f70,%rsp
movl   $0x7d0,-0x4(%rbp)
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
lea    -0x1f70(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x7d0,%eax
sub    -0x18(%rbp),%rax
mov    %eax,%edx
call   1050 <read@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11da <test_from_stdin+0x7a>
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11ea <test_from_stdin+0x8a>
jmp    12a4 <test_from_stdin+0x144>
jmp    11ef <test_from_stdin+0x8f>
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe07(%rip),%rdi
call   1030 <printf@plt>
cmpq   $0xa,-0x18(%rbp)
jae    121b <test_from_stdin+0xbb>
jmp    12a4 <test_from_stdin+0x144>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
lea    -0x1f70(%rbp),%rdi
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
sub    -0x28(%rbp),%rsi
call   1070 <scan@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x30(%rbp)
ja     1223 <test_from_stdin+0xc3>
mov    -0x28(%rbp),%rsi
lea    0xdb6(%rip),%rdi
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
lea    -0x1f70(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
sub    -0x28(%rbp),%rdx
lea    -0x1f70(%rbp),%rdi
call   1060 <memmove@plt>
mov    -0x18(%rbp),%rax
sub    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1181 <test_from_stdin+0x21>
mov    -0x18(%rbp),%rsi
lea    0xd84(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
add    $0x1f70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_from_stdin>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f48,%rsp
mov    0x2e60(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %rsp,%rsi
mov    $0x7d0,%edx
call   1050 <read@plt>
test   %rax,%rax
js     121d <test_from_stdin+0xbd>
xor    %r13d,%r13d
lea    0xe72(%rip),%r14
lea    0xe7c(%rip),%r15
mov    %rsp,%r12
cs nopw 0x0(%rax,%rax,1)
nop
test   %rax,%rax
je     123c <test_from_stdin+0xdc>
add    %rax,%r13
mov    %r14,%rdi
mov    %r13,%rsi
call   1030 <printf@plt>
cmp    $0xa,%r13
jb     123c <test_from_stdin+0xdc>
xor    %ebx,%ebx
nop
lea    (%rsp,%rbx,4),%rdi
mov    %r13,%rsi
sub    %rbx,%rsi
call   1070 <scan@plt>
add    %rax,%rbx
test   %rax,%rax
jne    11d0 <test_from_stdin+0x70>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
lea    (%rsp,%rbx,4),%rsi
sub    %rbx,%r13
mov    %r12,%rdi
mov    %r13,%rdx
call   1060 <memmove@plt>
mov    0x0(%rbp),%edi
lea    (%rsp,%r13,4),%rsi
mov    $0x7d0,%edx
sub    %r13d,%edx
call   1050 <read@plt>
test   %rax,%rax
jns    11b0 <test_from_stdin+0x50>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    0xdd3(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdf0(%rip),%rdi
mov    %r13,%rsi
call   1030 <printf@plt>
add    $0x1f48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [13 x i8] c"read() error\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"\0Aread %lu bytes\0A\00"
@.str.2 = [21 x i8] c"processed %lu bytes\0A\00"
@.str.3 = [20 x i8] c"%lu trailing bytes\0A\00"
define void @test_from_stdin() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 2000, i32* %1
%7 = call i8* @llvm.stacksave()
store i8* %7, i8** %2
%8 = alloca i32, i64 2000
store i64 0, i64* %3
br label %9
9: ; preds = %51, %0
%10 = load i32, i32* @STDIN_FILENO
%11 = load i64, i64* %3
%12 = getelementptr inbounds i32, i32* %8, i64 %11
%13 = load i64, i64* %3
%14 = sub i64 2000, %13
%15 = trunc i64 %14 to i32
%16 = call i64 @read(i32 %10, i32* %12, i32 %15)
store i64 %16, i64* %4
%17 = load i64, i64* %4
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %9
%20 = load i32, i32* @stderr
%21 = call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %9
%24 = load i64, i64* %4
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %23
br label %63
27: ; preds = %23
br label %28
28: ; preds = %27
%29 = load i64, i64* %4
%30 = load i64, i64* %3
%31 = add i64 %30, %29
store i64 %31, i64* %3
%32 = load i64, i64* %3
%33 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %32)
%34 = load i64, i64* %3
%35 = icmp ult i64 %34, 10
br i1 %35, label %36, label %37
36: ; preds = %28
br label %63
37: ; preds = %28
store i64 0, i64* %5
br label %38
38: ; preds = %48, %37
%39 = load i64, i64* %5
%40 = getelementptr inbounds i32, i32* %8, i64 %39
%41 = load i64, i64* %3
%42 = load i64, i64* %5
%43 = sub i64 %41, %42
%44 = call i64 @scan(i32* %40, i64 %43)
store i64 %44, i64* %6
%45 = load i64, i64* %6
%46 = load i64, i64* %5
%47 = add i64 %46, %45
store i64 %47, i64* %5
br label %48
48: ; preds = %38
%49 = load i64, i64* %6
%50 = icmp ugt i64 %49, 0
br i1 %50, label %38, label %51
51: ; preds = %48
%52 = load i64, i64* %5
%53 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %52)
%54 = load i64, i64* %5
%55 = getelementptr inbounds i32, i32* %8, i64 %54
%56 = load i64, i64* %3
%57 = load i64, i64* %5
%58 = sub i64 %56, %57
%59 = call i32 @memmove(i32* %8, i32* %55, i64 %58)
%60 = load i64, i64* %3
%61 = load i64, i64* %5
%62 = sub i64 %60, %61
store i64 %62, i64* %3
br label %9
63: ; preds = %36, %26
%64 = load i64, i64* %3
%65 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %64)
%66 = load i8*, i8** %2
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [13 x i8] c"read() error\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"\0Aread %lu bytes\0A\00"
@.str.2 = [21 x i8] c"processed %lu bytes\0A\00"
@.str.3 = [20 x i8] c"%lu trailing bytes\0A\00"
define void @test_from_stdin() {
%1 = alloca [2000 x i32]
%2 = getelementptr inbounds [2000 x i32], [2000 x i32]* %1, i64 0, i64 0
%3 = load i32, i32* @STDIN_FILENO
%4 = call i64 @read(i32 %3, i32* %2, i32 2000)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %25, %0
%7 = load i32, i32* @stderr
%8 = call i32 @fputs(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0, %25
%11 = phi i64 [ %34, %25 ], [ %4, %0 ]
%12 = phi i64 [ %28, %25 ], [ 0, %0 ]
%13 = icmp eq i64 %11, 0
br i1 %13, label %36, label %14
14: ; preds = %10
%15 = add i64 %11, %12
%16 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = icmp ult i64 %15, 10
br i1 %17, label %36, label %18
18: ; preds = %14, %18
%19 = phi i64 [ %23, %18 ], [ 0, %14 ]
%20 = getelementptr inbounds [2000 x i32], [2000 x i32]* %1, i64 0, i64 %19
%21 = sub i64 %15, %19
%22 = call i64 @scan(i32* %20, i64 %21)
%23 = add i64 %22, %19
%24 = icmp eq i64 %22, 0
br i1 %24, label %25, label %18
25: ; preds = %18
%26 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 %23)
%27 = getelementptr inbounds [2000 x i32], [2000 x i32]* %1, i64 0, i64 %23
%28 = sub i64 %15, %23
%29 = call i32 @memmove(i32* %2, i32* %27, i64 %28)
%30 = load i32, i32* @STDIN_FILENO
%31 = getelementptr inbounds [2000 x i32], [2000 x i32]* %1, i64 0, i64 %28
%32 = trunc i64 %28 to i32
%33 = sub i32 2000, %32
%34 = call i64 @read(i32 %30, i32* %31, i32 %33)
%35 = icmp slt i64 %34, 0
br i1 %35, label %6, label %10
36: ; preds = %10, %14
%37 = phi i64 [ %15, %14 ], [ %12, %10 ]
%38 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 %37)
ret void
}