#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*/ uint8_t;
typedef scalar_t__ uint32_t;

/* Variables and functions */
int read(int, int /*<<< orphan*/ *, int);
uint32_t spi_read(int fd, uint8_t *buffer, uint32_t size) {
  int status, i;
  uint8_t *ptr;
  ptr = buffer;
  while ((uint32_t)ptr - (uint32_t)buffer < size) {
    status = read(fd, ptr, 1);
    ptr++;
    for (i = 0; i < 800; i++)
      ;
  }
  return (uint32_t)ptr - (uint32_t)buffer;
}


x86 assembly (clang, -O0):
<spi_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
cmp    -0x18(%rbp),%rax
jge    118c <spi_read+0x7c>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x320,-0x20(%rbp)
jge    1187 <spi_read+0x77>
jmp    1179 <spi_read+0x69>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1167 <spi_read+0x57>
jmp    112b <spi_read+0x1b>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
jle    114b <spi_read+0x3b>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1030 <read@plt>
add    $0x4,%rbx
cmp    %r14,%rbx
jl     1130 <spi_read+0x20>
jmp    114d <spi_read+0x3d>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i64 @spi_read(i32 %0, i32* %1, i64 %2) {
%4 = ptrtoint i32* %1 to i64
%5 = icmp sgt i64 %2, 0
br i1 %5, label %6, label %13
6: ; preds = %3, %6
%7 = phi i32* [ %9, %6 ], [ %1, %3 ]
%8 = tail call i32 @read(i32 %0, i32* %7, i32 1)
%9 = getelementptr inbounds i32, i32* %7, i64 1
%10 = ptrtoint i32* %9 to i64
%11 = sub nsw i64 %10, %4
%12 = icmp slt i64 %11, %2
br i1 %12, label %6, label %13
13: ; preds = %6, %3
%14 = phi i64 [ 0, %3 ], [ %11, %6 ]
ret i64 %14
}