#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, unsigned char *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, unsigned char *, int);
int OW_ReadBytes(int fd, unsigned char *pBuf, int len) {
  int i;
  int bits, bytes;
  unsigned char startSlot = 0xff;
  unsigned char inBuf[64];
  unsigned char newByte;
  for (bytes = 0; bytes < len; bytes++) {
    newByte = 0;
    for (bits = 0; bits < 8; bits++) {
      newByte >>= 1;
      write(fd, &startSlot, 1);
      usleep(500);
      i = read(fd, inBuf, 64);
      if (i <= 0) {
        return bytes;
      }
      if (inBuf[0] == 0xff) {
        newByte |= 0x80;
      }
    }
    pBuf[bytes] = newByte;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<OW_ReadBytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movb   $0xff,-0x21(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1209 <OW_ReadBytes+0xd9>
movb   $0x0,-0x71(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x8,-0x1c(%rbp)
jge    11ed <OW_ReadBytes+0xbd>
movzbl -0x71(%rbp),%eax
sar    $0x1,%eax
mov    %al,-0x71(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    -0x8(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     11bf <OW_ReadBytes+0x8f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120f <OW_ReadBytes+0xdf>
movzbl -0x70(%rbp),%eax
cmp    $0xff,%eax
jne    11da <OW_ReadBytes+0xaa>
movzbl -0x71(%rbp),%eax
or     $0x80,%eax
mov    %al,-0x71(%rbp)
jmp    11df <OW_ReadBytes+0xaf>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1167 <OW_ReadBytes+0x37>
mov    -0x71(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1150 <OW_ReadBytes+0x20>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OW_ReadBytes>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,0x18(%rsp)
movb   $0xff,0x7(%rsp)
test   %edx,%edx
jle    13a9 <OW_ReadBytes+0x279>
mov    %edi,%ebp
mov    %edx,%eax
mov    %rax,0x10(%rsp)
xor    %ebx,%ebx
lea    0x7(%rsp),%r12
lea    0x20(%rsp),%r15
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    13b2 <OW_ReadBytes+0x282>
cmpb   $0xff,0x20(%rsp)
sete   %r14b
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    13b2 <OW_ReadBytes+0x282>
movzbl 0x20(%rsp),%r13d
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    13b2 <OW_ReadBytes+0x282>
movzbl 0x20(%rsp),%eax
mov    %al,0x6(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    13b2 <OW_ReadBytes+0x282>
movzbl 0x20(%rsp),%eax
mov    %al,0x5(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    13b2 <OW_ReadBytes+0x282>
movzbl 0x20(%rsp),%eax
mov    %al,0x4(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    13b2 <OW_ReadBytes+0x282>
mov    %rbx,0x8(%rsp)
movzbl 0x20(%rsp),%ebx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    13ad <OW_ReadBytes+0x27d>
lea    0x2(%r14),%eax
cmp    $0xff,%r13b
mov    $0x0,%edx
mov    %r14b,%dl
movzbl %al,%eax
cmovne %edx,%eax
lea    0x4(%rax),%ecx
cmpb   $0xff,0x6(%rsp)
movzbl %cl,%ecx
cmovne %eax,%ecx
mov    %ecx,%eax
or     $0x8,%al
cmpb   $0xff,0x5(%rsp)
movzbl %al,%eax
cmovne %ecx,%eax
mov    %eax,%ecx
or     $0x10,%cl
cmpb   $0xff,0x4(%rsp)
movzbl %cl,%ecx
cmovne %eax,%ecx
mov    %ecx,%eax
or     $0x20,%al
cmp    $0xff,%bl
movzbl %al,%eax
cmovne %ecx,%eax
mov    %eax,%ecx
or     $0x40,%cl
cmpb   $0xff,0x20(%rsp)
movzbl %cl,%ebx
cmovne %eax,%ebx
mov    %ebp,%edi
mov    %r12,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    $0x1f4,%edi
call   1050 <usleep@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    13ad <OW_ReadBytes+0x27d>
movzbl %bl,%eax
or     $0x80,%bl
cmpb   $0xff,0x20(%rsp)
movzbl %bl,%ecx
cmovne %eax,%ecx
mov    0x8(%rsp),%rbx
mov    0x18(%rsp),%rax
mov    %cl,(%rax,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,0x10(%rsp)
jne    1165 <OW_ReadBytes+0x35>
mov    0x10(%rsp),%rbx
jmp    13b2 <OW_ReadBytes+0x282>
xor    %ebx,%ebx
jmp    13b2 <OW_ReadBytes+0x282>
mov    0x8(%rsp),%rbx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @OW_ReadBytes(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca [64 x i8]
%13 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8 -1, i8* %11
store i32 0, i32* %10
br label %14
14: ; preds = %57, %3
%15 = load i32, i32* %10
%16 = load i32, i32* %7
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %60
18: ; preds = %14
store i8 0, i8* %13
store i32 0, i32* %9
br label %19
19: ; preds = %48, %18
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 8
br i1 %21, label %22, label %51
22: ; preds = %19
%23 = load i8, i8* %13
%24 = zext i8 %23 to i32
%25 = ashr i32 %24, 1
%26 = trunc i32 %25 to i8
store i8 %26, i8* %13
%27 = load i32, i32* %5
%28 = call i32 @write(i32 %27, i8* %11, i32 1)
%29 = call i32 @usleep(i32 500)
%30 = load i32, i32* %5
%31 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 64)
store i32 %32, i32* %8
%33 = load i32, i32* %8
%34 = icmp sle i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = load i32, i32* %10
store i32 %36, i32* %4
br label %62
37: ; preds = %22
%38 = getelementptr inbounds [64 x i8], [64 x i8]* %12, i64 0, i64 0
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = icmp eq i32 %40, 255
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i8, i8* %13
%44 = zext i8 %43 to i32
%45 = or i32 %44, 128
%46 = trunc i32 %45 to i8
store i8 %46, i8* %13
br label %47
47: ; preds = %42, %37
br label %48
48: ; preds = %47
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %19
51: ; preds = %19
%52 = load i8, i8* %13
%53 = load i8*, i8** %6
%54 = load i32, i32* %10
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %53, i64 %55
store i8 %52, i8* %56
br label %57
57: ; preds = %51
%58 = load i32, i32* %10
%59 = add nsw i32 %58, 1
store i32 %59, i32* %10
br label %14
60: ; preds = %14
%61 = load i32, i32* %10
store i32 %61, i32* %4
br label %62
62: ; preds = %60, %35
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
define i32 @OW_ReadBytes(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
%5 = alloca [64 x i8]
store i8 -1, i8* %4
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %102
8: ; preds = %3
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %8, %78
%11 = phi i64 [ 0, %8 ], [ %84, %78 ]
%12 = call i32 @write(i32 %0, i8* %4, i32 1)
%13 = tail call i32 @usleep(i32 500)
%14 = call i32 @read(i32 %0, i8* %6, i32 64)
%15 = icmp slt i32 %14, 1
br i1 %15, label %100, label %16
16: ; preds = %10
%17 = load i8, i8* %6
%18 = icmp eq i8 %17, -1
%19 = zext i1 %18 to i8
%20 = call i32 @write(i32 %0, i8* %4, i32 1)
%21 = tail call i32 @usleep(i32 500)
%22 = call i32 @read(i32 %0, i8* %6, i32 64)
%23 = icmp slt i32 %22, 1
br i1 %23, label %98, label %24
24: ; preds = %16
%25 = load i8, i8* %6
%26 = call i32 @write(i32 %0, i8* %4, i32 1)
%27 = tail call i32 @usleep(i32 500)
%28 = call i32 @read(i32 %0, i8* %6, i32 64)
%29 = icmp slt i32 %28, 1
br i1 %29, label %96, label %30
30: ; preds = %24
%31 = load i8, i8* %6
%32 = call i32 @write(i32 %0, i8* %4, i32 1)
%33 = tail call i32 @usleep(i32 500)
%34 = call i32 @read(i32 %0, i8* %6, i32 64)
%35 = icmp slt i32 %34, 1
br i1 %35, label %94, label %36
36: ; preds = %30
%37 = load i8, i8* %6
%38 = call i32 @write(i32 %0, i8* %4, i32 1)
%39 = tail call i32 @usleep(i32 500)
%40 = call i32 @read(i32 %0, i8* %6, i32 64)
%41 = icmp slt i32 %40, 1
br i1 %41, label %92, label %42
42: ; preds = %36
%43 = load i8, i8* %6
%44 = call i32 @write(i32 %0, i8* %4, i32 1)
%45 = tail call i32 @usleep(i32 500)
%46 = call i32 @read(i32 %0, i8* %6, i32 64)
%47 = icmp slt i32 %46, 1
br i1 %47, label %90, label %48
48: ; preds = %42
%49 = load i8, i8* %6
%50 = call i32 @write(i32 %0, i8* %4, i32 1)
%51 = tail call i32 @usleep(i32 500)
%52 = call i32 @read(i32 %0, i8* %6, i32 64)
%53 = icmp slt i32 %52, 1
br i1 %53, label %88, label %54
54: ; preds = %48
%55 = icmp eq i8 %49, -1
%56 = icmp eq i8 %43, -1
%57 = icmp eq i8 %37, -1
%58 = icmp eq i8 %31, -1
%59 = icmp eq i8 %25, -1
%60 = load i8, i8* %6
%61 = icmp eq i8 %60, -1
%62 = or i8 %19, 2
%63 = select i1 %59, i8 %62, i8 %19
%64 = or i8 %63, 4
%65 = select i1 %58, i8 %64, i8 %63
%66 = or i8 %65, 8
%67 = select i1 %57, i8 %66, i8 %65
%68 = or i8 %67, 16
%69 = select i1 %56, i8 %68, i8 %67
%70 = or i8 %69, 32
%71 = select i1 %55, i8 %70, i8 %69
%72 = or i8 %71, 64
%73 = select i1 %61, i8 %72, i8 %71
%74 = call i32 @write(i32 %0, i8* %4, i32 1)
%75 = tail call i32 @usleep(i32 500)
%76 = call i32 @read(i32 %0, i8* %6, i32 64)
%77 = icmp slt i32 %76, 1
br i1 %77, label %86, label %78
78: ; preds = %54
%79 = load i8, i8* %6
%80 = icmp eq i8 %79, -1
%81 = or i8 %73, -128
%82 = select i1 %80, i8 %81, i8 %73
%83 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 %82, i8* %83
%84 = add nuw nsw i64 %11, 1
%85 = icmp eq i64 %84, %9
br i1 %85, label %102, label %10
86: ; preds = %54
%87 = trunc i64 %11 to i32
br label %102
88: ; preds = %48
%89 = trunc i64 %11 to i32
br label %102
90: ; preds = %42
%91 = trunc i64 %11 to i32
br label %102
92: ; preds = %36
%93 = trunc i64 %11 to i32
br label %102
94: ; preds = %30
%95 = trunc i64 %11 to i32
br label %102
96: ; preds = %24
%97 = trunc i64 %11 to i32
br label %102
98: ; preds = %16
%99 = trunc i64 %11 to i32
br label %102
100: ; preds = %10
%101 = trunc i64 %11 to i32
br label %102
102: ; preds = %78, %86, %88, %90, %92, %94, %96, %98, %100, %3
%103 = phi i32 [ 0, %3 ], [ %87, %86 ], [ %89, %88 ], [ %91, %90 ], [ %93, %92 ], [ %95, %94 ], [ %97, %96 ], [ %99, %98 ], [ %101, %100 ], [ %2, %78 ]
ret i32 %103
}