#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*/ frb_buf;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ lprintf(int, char *, int);
int read(int, char *, int);
int mdm_read_byte(int fd, char *c) {
  static char frb_buf[512];
  static int frb_rp = 0;
  static int frb_len = 0;
  if (frb_rp >= frb_len) {
    frb_len = read(fd, frb_buf, sizeof(frb_buf));
    if (frb_len <= 0) {
      if (frb_len == 0)
        errno = 0;
      lprintf(1, "mdm_read_byte: read returned %d", frb_len);
      return frb_len;
    }
    frb_rp = 0;
  }
  *c = frb_buf[frb_rp++];
  return 1;
}


x86 assembly (clang, -O0):
<mdm_read_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x310b(%rip),%eax
cmp    0x3109(%rip),%eax
jl     11b2 <mdm_read_byte+0x92>
mov    -0x8(%rbp),%edi
lea    0x2ef5(%rip),%rsi
mov    $0x200,%edx
call   1030 <read@plt>
mov    %eax,0x30e9(%rip)
cmpl   $0x0,0x30e2(%rip)
jg     11a8 <mdm_read_byte+0x88>
cmpl   $0x0,0x30d5(%rip)
jne    1183 <mdm_read_byte+0x63>
mov    0x2e64(%rip),%rax
movq   $0x0,(%rax)
mov    0x30bb(%rip),%edx
mov    $0x1,%edi
lea    0xe6b(%rip),%rsi
call   1040 <lprintf@plt>
mov    0x30a4(%rip),%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <mdm_read_byte+0xbd>
movl   $0x0,0x308e(%rip)
mov    0x3088(%rip),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,0x307d(%rip)
movslq %eax,%rcx
lea    0x2e73(%rip),%rax
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mdm_read_byte>:
push   %rbx
mov    %rsi,%rbx
mov    0x3116(%rip),%ecx
cmp    0x3114(%rip),%ecx
jl     114f <mdm_read_byte+0x2f>
lea    0x2f07(%rip),%rsi
mov    $0x200,%edx
call   1030 <read@plt>
mov    %eax,0x30fb(%rip)
xor    %ecx,%ecx
test   %eax,%eax
jle    116e <mdm_read_byte+0x4e>
lea    0x1(%rcx),%eax
mov    %eax,0x30e8(%rip)
movslq %ecx,%rax
lea    0x2ede(%rip),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,(%rbx)
mov    $0x1,%eax
pop    %rbx
ret
jne    117e <mdm_read_byte+0x5e>
mov    0x2e69(%rip),%rcx
movq   $0x0,(%rcx)
lea    0xe7b(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
call   1040 <lprintf@plt>
mov    0x30ad(%rip),%eax
pop    %rbx
ret

LLVM IR (-O0):
@mdm_read_byte.frb_buf = internal global [512 x i8] zeroinitializer
@mdm_read_byte.frb_rp = internal global i32 0
@mdm_read_byte.frb_len = internal global i32 0
@errno = global i64 0
@.str = [32 x i8] c"mdm_read_byte: read returned %d\00"
define i32 @mdm_read_byte(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
%6 = load i32, i32* @mdm_read_byte.frb_rp
%7 = load i32, i32* @mdm_read_byte.frb_len
%8 = icmp sge i32 %6, %7
br i1 %8, label %9, label %23
9: ; preds = %2
%10 = load i32, i32* %4
%11 = call i32 @read(i32 %10, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @mdm_read_byte.frb_buf, i64 0, i64 0), i32 512)
store i32 %11, i32* @mdm_read_byte.frb_len
%12 = load i32, i32* @mdm_read_byte.frb_len
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %9
%15 = load i32, i32* @mdm_read_byte.frb_len
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
store i64 0, i64* @errno
br label %18
18: ; preds = %17, %14
%19 = load i32, i32* @mdm_read_byte.frb_len
%20 = call i32 @lprintf(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @mdm_read_byte.frb_len
store i32 %21, i32* %3
br label %30
22: ; preds = %9
store i32 0, i32* @mdm_read_byte.frb_rp
br label %23
23: ; preds = %22, %2
%24 = load i32, i32* @mdm_read_byte.frb_rp
%25 = add nsw i32 %24, 1
store i32 %25, i32* @mdm_read_byte.frb_rp
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds [512 x i8], [512 x i8]* @mdm_read_byte.frb_buf, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %5
store i8 %28, i8* %29
store i32 1, i32* %3
br label %30
30: ; preds = %23, %18
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@mdm_read_byte.frb_buf = internal global [512 x i8] zeroinitializer
@mdm_read_byte.frb_rp = internal unnamed_addr global i32 0
@mdm_read_byte.frb_len = internal unnamed_addr global i32 0
@errno = global i64 0
@.str = [32 x i8] c"mdm_read_byte: read returned %d\00"
define i32 @mdm_read_byte(i32 %0, i8* nocapture writeonly %1) {
%3 = load i32, i32* @mdm_read_byte.frb_rp
%4 = load i32, i32* @mdm_read_byte.frb_len
%5 = icmp slt i32 %3, %4
br i1 %5, label %15, label %6
6: ; preds = %2
%7 = tail call i32 @read(i32 %0, i8* getelementptr inbounds ([512 x i8], [512 x i8]* @mdm_read_byte.frb_buf, i64 0, i64 0), i32 512)
store i32 %7, i32* @mdm_read_byte.frb_len
%8 = icmp slt i32 %7, 1
br i1 %8, label %9, label %15
9: ; preds = %6
%10 = icmp eq i32 %7, 0
br i1 %10, label %11, label %12
11: ; preds = %9
store i64 0, i64* @errno
br label %12
12: ; preds = %11, %9
%13 = tail call i32 @lprintf(i32 1, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %7)
%14 = load i32, i32* @mdm_read_byte.frb_len
br label %21
15: ; preds = %6, %2
%16 = phi i32 [ %3, %2 ], [ 0, %6 ]
%17 = add nsw i32 %16, 1
store i32 %17, i32* @mdm_read_byte.frb_rp
%18 = sext i32 %16 to i64
%19 = getelementptr inbounds [512 x i8], [512 x i8]* @mdm_read_byte.frb_buf, i64 0, i64 %18
%20 = load i8, i8* %19
store i8 %20, i8* %1
br label %21
21: ; preds = %15, %12
%22 = phi i32 [ %14, %12 ], [ 1, %15 ]
ret i32 %22
}