#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 */
unsigned int rb(int);
int rd(int len, char *data) {
  unsigned int c, i;
  for (i = 0; i < len; i++) {
    c = rb(1);
    if (c == -1)
      return -1;
    if (c == 0xff)
      c = rb(1);
    if (c == -1)
      return -1;
    data[i] = c;
  }
  return len;
}


x86 assembly (clang, -O0):
<rd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    11a2 <rd+0x92>
mov    $0x1,%edi
call   1030 <rb@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1155 <rd+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <rd+0x98>
cmpl   $0xff,-0x14(%rbp)
jne    116f <rd+0x5f>
mov    $0x1,%edi
call   1030 <rb@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1185 <rd+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a8 <rd+0x98>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <rd+0x16>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rd>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edi,%edi
je     1164 <rd+0x54>
mov    %rsi,%r15
mov    %edi,%r14d
mov    %edi,%ebp
xor    %ebx,%ebx
jmp    114c <rd+0x3c>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1030 <rb@plt>
cmp    $0xffffffff,%eax
je     116d <rd+0x5d>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
je     1168 <rd+0x58>
mov    $0x1,%edi
call   1030 <rb@plt>
cmp    $0xff,%eax
je     1130 <rd+0x20>
cmp    $0xffffffff,%eax
jne    113f <rd+0x2f>
jmp    1172 <rd+0x62>
xor    %eax,%eax
jmp    1172 <rd+0x62>
mov    %r14d,%eax
jmp    1172 <rd+0x62>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @rd(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %7
br label %8
8: ; preds = %33, %2
%9 = load i32, i32* %7
%10 = load i32, i32* %4
%11 = icmp ult i32 %9, %10
br i1 %11, label %12, label %36
12: ; preds = %8
%13 = call i32 @rb(i32 1)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %3
br label %38
17: ; preds = %12
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, 255
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 @rb(i32 1)
store i32 %21, i32* %6
br label %22
22: ; preds = %20, %17
%23 = load i32, i32* %6
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 -1, i32* %3
br label %38
26: ; preds = %22
%27 = load i32, i32* %6
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %5
%30 = load i32, i32* %7
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
br label %33
33: ; preds = %26
%34 = load i32, i32* %7
%35 = add i32 %34, 1
store i32 %35, i32* %7
br label %8
36: ; preds = %8
%37 = load i32, i32* %4
store i32 %37, i32* %3
br label %38
38: ; preds = %36, %25, %16
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
define i32 @rd(i32 %0, i8* nocapture writeonly %1) {
%3 = icmp eq i32 %0, 0
br i1 %3, label %18, label %4
4: ; preds = %2
%5 = zext i32 %0 to i64
br label %6
6: ; preds = %4, %12
%7 = phi i64 [ 0, %4 ], [ %16, %12 ]
%8 = tail call i32 @rb(i32 1)
switch i32 %8, label %12 [
i32 -1, label %18
i32 255, label %9
]
9: ; preds = %6
%10 = tail call i32 @rb(i32 1)
%11 = icmp eq i32 %10, -1
br i1 %11, label %18, label %12
12: ; preds = %6, %9
%13 = phi i32 [ %10, %9 ], [ %8, %6 ]
%14 = trunc i32 %13 to i8
%15 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %14, i8* %15
%16 = add nuw nsw i64 %7, 1
%17 = icmp eq i64 %16, %5
br i1 %17, label %18, label %6
18: ; preds = %6, %9, %12, %2
%19 = phi i32 [ 0, %2 ], [ %0, %12 ], [ -1, %9 ], [ %8, %6 ]
ret i32 %19
}