#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 difference();
char read_a_char_from_circular_buffer();
int read_from_circular_buffer(char *buf, int bytes_to_read) {
  int bytes_avail_to_read = difference();
  int i = 0;
  while (bytes_avail_to_read > 0 && bytes_to_read > 0) {
    buf[i] = read_a_char_from_circular_buffer();
    bytes_avail_to_read--;
    bytes_to_read--;
    i++;
  }
  if (i > 1)
    buf[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<read_from_circular_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1040 <difference@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x15(%rbp)
jle    1159 <read_from_circular_buffer+0x39>
cmpl   $0x0,-0xc(%rbp)
setg   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1169 <read_from_circular_buffer+0x49>
jmp    119d <read_from_circular_buffer+0x7d>
mov    $0x0,%al
call   1030 <read_a_char_from_circular_buffer@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1140 <read_from_circular_buffer+0x20>
cmpl   $0x1,-0x14(%rbp)
jle    11b3 <read_from_circular_buffer+0x93>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_circular_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <difference@plt>
test   %eax,%eax
jle    117e <read_from_circular_buffer+0x5e>
test   %r15d,%r15d
jle    117e <read_from_circular_buffer+0x5e>
mov    %eax,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <read_a_char_from_circular_buffer@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    $0x2,%ebp
jb     1174 <read_from_circular_buffer+0x54>
add    $0xffffffff,%ebp
lea    -0x1(%r15),%eax
cmp    $0x1,%r15d
mov    %eax,%r15d
ja     1150 <read_from_circular_buffer+0x30>
cmp    $0x1,%ebx
jbe    117e <read_from_circular_buffer+0x5e>
movb   $0x0,(%r14,%rbx,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @read_from_circular_buffer(i8* nocapture writeonly %0, i32 %1) {
%3 = tail call i32 (...) @difference()
%4 = icmp sgt i32 %3, 0
%5 = icmp sgt i32 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %25
7: ; preds = %2, %7
%8 = phi i64 [ %15, %7 ], [ 0, %2 ]
%9 = phi i32 [ %13, %7 ], [ %3, %2 ]
%10 = phi i32 [ %14, %7 ], [ %1, %2 ]
%11 = tail call signext i8 (...) @read_a_char_from_circular_buffer()
%12 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 %11, i8* %12
%13 = add nsw i32 %9, -1
%14 = add nsw i32 %10, -1
%15 = add nuw nsw i64 %8, 1
%16 = icmp ugt i32 %9, 1
%17 = icmp ugt i32 %10, 1
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %7, label %19
19: ; preds = %7
%20 = trunc i64 %15 to i32
%21 = icmp ugt i32 %20, 1
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = and i64 %15, 4294967295
%24 = getelementptr inbounds i8, i8* %0, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %2, %22, %19
%26 = phi i32 [ %20, %22 ], [ %20, %19 ], [ 0, %2 ]
ret i32 %26
}