#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 read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int readto(int strm, char *buff, int nmax, int mstime) {
  int bpos = 0, nread;
  while (mstime > 0) {
    nread = read(strm, &buff[bpos], nmax - bpos);
    if (nread > 0)
      bpos += nread;
    if (bpos >= nmax || (bpos > 0 && buff[bpos - 1] == '\n'))
      return bpos;
    usleep(1000);
    mstime--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<readto>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11c8 <readto+0xa8>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1175 <readto+0x55>
mov    -0x20(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11a5 <readto+0x85>
cmpl   $0x0,-0x1c(%rbp)
jle    11b0 <readto+0x90>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11b0 <readto+0x90>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cf <readto+0xaf>
mov    $0x3e8,%edi
call   1040 <usleep@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    113c <readto+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readto>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %ecx,%ecx
jle    119b <readto+0x7b>
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
add    $0x1,%ebx
xor    %ebp,%ebp
jmp    1162 <readto+0x42>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x3e8,%edi
call   1040 <usleep@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
jle    119b <readto+0x7b>
mov    %ebp,%r13d
mov    %ebp,%esi
add    %r14,%rsi
mov    %r12d,%edx
sub    %ebp,%edx
mov    %r15d,%edi
call   1030 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
mov    $0x0,%eax
cmovs  %eax,%ebp
add    %r13d,%ebp
cmp    %r12d,%ebp
jge    119d <readto+0x7d>
test   %ebp,%ebp
jle    1150 <readto+0x30>
lea    -0x1(%rbp),%eax
cmpb   $0xa,(%r14,%rax,1)
jne    1150 <readto+0x30>
jmp    119d <readto+0x7d>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readto(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %49, %4
%13 = load i32, i32* %9
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %53
15: ; preds = %12
%16 = load i32, i32* %6
%17 = load i8*, i8** %7
%18 = load i32, i32* %10
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i32, i32* %8
%22 = load i32, i32* %10
%23 = sub nsw i32 %21, %22
%24 = call i32 @read(i32 %16, i8* %20, i32 %23)
store i32 %24, i32* %11
%25 = load i32, i32* %11
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %15
%28 = load i32, i32* %11
%29 = load i32, i32* %10
%30 = add nsw i32 %29, %28
store i32 %30, i32* %10
br label %31
31: ; preds = %27, %15
%32 = load i32, i32* %10
%33 = load i32, i32* %8
%34 = icmp sge i32 %32, %33
br i1 %34, label %47, label %35
35: ; preds = %31
%36 = load i32, i32* %10
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %35
%39 = load i8*, i8** %7
%40 = load i32, i32* %10
%41 = sub nsw i32 %40, 1
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 10
br i1 %46, label %47, label %49
47: ; preds = %38, %31
%48 = load i32, i32* %10
store i32 %48, i32* %5
br label %54
49: ; preds = %38, %35
%50 = call i32 @usleep(i32 1000)
%51 = load i32, i32* %9
%52 = add nsw i32 %51, -1
store i32 %52, i32* %9
br label %12
53: ; preds = %12
store i32 0, i32* %5
br label %54
54: ; preds = %53, %47
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
define i32 @readto(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %29
6: ; preds = %4, %25
%7 = phi i32 [ %15, %25 ], [ 0, %4 ]
%8 = phi i32 [ %27, %25 ], [ %3, %4 ]
%9 = zext i32 %7 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = sub nsw i32 %2, %7
%12 = tail call i32 @read(i32 %0, i8* %10, i32 %11)
%13 = icmp sgt i32 %12, 0
%14 = select i1 %13, i32 %12, i32 0
%15 = add nuw nsw i32 %14, %7
%16 = icmp slt i32 %15, %2
br i1 %16, label %17, label %29
17: ; preds = %6
%18 = icmp sgt i32 %15, 0
br i1 %18, label %19, label %25
19: ; preds = %17
%20 = add nsw i32 %15, -1
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %1, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 10
br i1 %24, label %29, label %25
25: ; preds = %19, %17
%26 = tail call i32 @usleep(i32 1000)
%27 = add nsw i32 %8, -1
%28 = icmp sgt i32 %8, 1
br i1 %28, label %6, label %29
29: ; preds = %19, %6, %25, %4
%30 = phi i32 [ 0, %4 ], [ 0, %25 ], [ %15, %6 ], [ %15, %19 ]
ret i32 %30
}