#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 mcharinp();
int mrd();
int /*<<< orphan*/ opabort();
int mread(char *buffer, int count, int timeout) {
  int i, c;
  i = 0;
  while (!(c = mrd()) && (timeout--) && !opabort())
    ;
  if (c)
    buffer[i++] = mcharinp();
  return i;
}


x86 assembly (clang, -O0):
<mread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1030 <mrd@plt>
mov    %eax,%ecx
mov    %ecx,-0x18(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
jne    118e <mread+0x5e>
mov    -0x10(%rbp),%ecx
mov    %ecx,%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x19(%rbp)
je     118e <mread+0x5e>
mov    $0x0,%al
call   1040 <opabort@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    119e <mread+0x6e>
jmp    11a3 <mread+0x73>
jmp    1149 <mread+0x19>
cmpl   $0x0,-0x18(%rbp)
je     11cb <mread+0x9b>
mov    $0x0,%al
call   1050 <mcharinp@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mread>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rdi,%r14
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <mrd@plt>
mov    %eax,%ebx
sub    $0x1,%ebp
setb   %al
test   %ebx,%ebx
jne    1162 <mread+0x32>
test   %al,%al
jne    1162 <mread+0x32>
xor    %eax,%eax
call   1040 <opabort@plt>
test   %eax,%eax
je     1140 <mread+0x10>
test   %ebx,%ebx
je     1177 <mread+0x47>
xor    %eax,%eax
call   1050 <mcharinp@plt>
mov    %al,(%r14)
mov    $0x1,%eax
jmp    1179 <mread+0x49>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mread(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %22, %3
%10 = call i32 (...) @mrd()
store i32 %10, i32* %8
%11 = icmp ne i32 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %9
%13 = load i32, i32* %6
%14 = add nsw i32 %13, -1
store i32 %14, i32* %6
%15 = icmp ne i32 %13, 0
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = call i32 (...) @opabort()
%18 = icmp ne i32 %17, 0
%19 = xor i1 %18, true
br label %20
20: ; preds = %16, %12, %9
%21 = phi i1 [ false, %12 ], [ false, %9 ], [ %19, %16 ]
br i1 %21, label %22, label %23
22: ; preds = %20
br label %9
23: ; preds = %20
%24 = load i32, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %23
%27 = call signext i8 (...) @mcharinp()
%28 = load i8*, i8** %4
%29 = load i32, i32* %7
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 %27, i8* %32
br label %33
33: ; preds = %26, %23
%34 = load i32, i32* %7
ret i32 %34
}

LLVM IR (-O2):
define i32 @mread(i8* nocapture writeonly %0, i32 %1, i32 %2) {
br label %4
4: ; preds = %11, %3
%5 = phi i32 [ %2, %3 ], [ %12, %11 ]
%6 = tail call i32 (...) @mrd()
%7 = icmp eq i32 %6, 0
%8 = xor i1 %7, true
%9 = icmp eq i32 %5, 0
%10 = select i1 %8, i1 true, i1 %9
br i1 %10, label %15, label %11
11: ; preds = %4
%12 = add nsw i32 %5, -1
%13 = tail call i32 (...) @opabort()
%14 = icmp eq i32 %13, 0
br i1 %14, label %4, label %15
15: ; preds = %4, %11
br i1 %7, label %18, label %16
16: ; preds = %15
%17 = tail call signext i8 (...) @mcharinp()
store i8 %17, i8* %0
br label %18
18: ; preds = %16, %15
%19 = phi i32 [ 1, %16 ], [ 0, %15 ]
ret i32 %19
}