#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 /*<<< orphan*/ spi_xfer(int, int);
scalar_t__ spi_xfer_buf_get(int /*<<< orphan*/);
int /*<<< orphan*/ spi_xfer_buf_set(int /*<<< orphan*/, int);
int __attribute__((no)) wait_for_write_completion(void) {
  unsigned int m = 0;
  for (m = 0; m < 32768; ++m) {
    spi_xfer_buf_set(0, 0xff);
    spi_xfer(1, 1);
    if (spi_xfer_buf_get(0) != 0)
      return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<wait_for_write_completion>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x8000,-0x8(%rbp)
jae    119b <wait_for_write_completion+0x6b>
xor    %edi,%edi
mov    $0xff,%esi
call   1030 <spi_xfer_buf_set@plt>
mov    $0x1,%esi
mov    %esi,%edi
call   1040 <spi_xfer@plt>
xor    %edi,%edi
call   1050 <spi_xfer_buf_get@plt>
cmp    $0x0,%rax
je     1188 <wait_for_write_completion+0x58>
movl   $0x0,-0x4(%rbp)
jmp    11a2 <wait_for_write_completion+0x72>
jmp    118d <wait_for_write_completion+0x5d>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1146 <wait_for_write_completion+0x16>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_write_completion>:
push   %rbp
push   %rbx
push   %rax
mov    $0x8000,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    $0xff,%esi
call   1030 <spi_xfer_buf_set@plt>
mov    $0x1,%edi
mov    $0x1,%esi
call   1040 <spi_xfer@plt>
xor    %edi,%edi
call   1050 <spi_xfer_buf_get@plt>
test   %rax,%rax
jne    1171 <wait_for_write_completion+0x41>
add    $0xffffffff,%ebp
jne    1140 <wait_for_write_completion+0x10>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @wait_for_write_completion() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %2
store i32 0, i32* %2
br label %3
3: ; preds = %13, %0
%4 = load i32, i32* %2
%5 = icmp ult i32 %4, 32768
br i1 %5, label %6, label %16
6: ; preds = %3
%7 = call i32 @spi_xfer_buf_set(i32 0, i32 255)
%8 = call i32 @spi_xfer(i32 1, i32 1)
%9 = call i64 @spi_xfer_buf_get(i32 0)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %6
store i32 0, i32* %1
br label %17
12: ; preds = %6
br label %13
13: ; preds = %12
%14 = load i32, i32* %2
%15 = add i32 %14, 1
store i32 %15, i32* %2
br label %3
16: ; preds = %3
store i32 -1, i32* %1
br label %17
17: ; preds = %16, %11
%18 = load i32, i32* %1
ret i32 %18
}

LLVM IR (-O2):
define i32 @wait_for_write_completion() {
br label %4
1: ; preds = %4
%2 = add nuw nsw i32 %5, 1
%3 = icmp eq i32 %2, 32768
br i1 %3, label %10, label %4
4: ; preds = %0, %1
%5 = phi i32 [ 0, %0 ], [ %2, %1 ]
%6 = tail call i32 @spi_xfer_buf_set(i32 0, i32 255)
%7 = tail call i32 @spi_xfer(i32 1, i32 1)
%8 = tail call i64 @spi_xfer_buf_get(i32 0)
%9 = icmp eq i64 %8, 0
br i1 %9, label %1, label %10
10: ; preds = %1, %4
%11 = phi i32 [ 0, %4 ], [ -1, %1 ]
ret i32 %11
}