#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 write(int, int /*<<< orphan*/ *, int);
uint32_t spi_write(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 = write(fd, ptr, 1);
    ptr++;
  }
  return (uint32_t)ptr - (uint32_t)buffer;
}


x86 assembly (clang, -O0):
<spi_write>:
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    1165 <spi_write+0x55>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    112b <spi_write+0x1b>
mov    -0x28(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
jle    114b <spi_write+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 <write@plt>
add    $0x4,%rbx
cmp    %r14,%rbx
jl     1130 <spi_write+0x20>
jmp    114d <spi_write+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_write(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 = %19, %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 %25
19: ; preds = %11
%20 = load i32, i32* %4
%21 = load i32*, i32** %9
%22 = call i32 @write(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
br label %11
25: ; preds = %11
%26 = load i32*, i32** %9
%27 = ptrtoint i32* %26 to i64
%28 = load i32*, i32** %5
%29 = ptrtoint i32* %28 to i64
%30 = sub nsw i64 %27, %29
ret i64 %30
}

LLVM IR (-O2):
define i64 @spi_write(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 @write(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
}