#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 */
struct spi_slave {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, unsigned int, int);
int spi_xfer(struct spi_slave *, unsigned int, void const *,
             int /*<<< orphan*/ *, int);
int spi_flash_cmd_write(struct spi_slave *spi, const unsigned char *cmd,
                        unsigned int cmd_len, const void *data,
                        unsigned int data_len) {
  unsigned long flags = 0x01;
  int ret;
  if (data_len == 0)
    flags |= 0x02;
  ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags);
  if (ret) {
    printf("SF: Failed to send read command (%zu bytes): %d\n", cmd_len, ret);
  } else if (data_len != 0) {
    ret = spi_xfer(spi, data_len * 8, data, NULL, 0x02);
    if (ret)
      printf("SF: Failed to read %zu bytes of data: %d\n", data_len, ret);
  }
  return ret;
}


x86 assembly (clang, -O0):
<spi_flash_cmd_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
movq   $0x1,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1159 <spi_flash_cmd_write+0x39>
mov    -0x30(%rbp),%rax
or     $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
shl    $0x3,%esi
mov    -0x10(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    %eax,%r8d
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <spi_xfer@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     119b <spi_flash_cmd_write+0x7b>
mov    -0x14(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
jmp    11eb <spi_flash_cmd_write+0xcb>
cmpl   $0x0,-0x24(%rbp)
je     11e6 <spi_flash_cmd_write+0xc6>
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
shl    $0x3,%esi
mov    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    $0x2,%r8d
call   1040 <spi_xfer@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11e1 <spi_flash_cmd_write+0xc1>
mov    -0x24(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xe55(%rip),%rdi
call   1030 <printf@plt>
jmp    11e6 <spi_flash_cmd_write+0xc6>
jmp    11eb <spi_flash_cmd_write+0xcb>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_flash_cmd_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r15d
mov    %rcx,%r14
mov    %edx,%r13d
mov    %rsi,%rdx
mov    %rdi,%r12
xor    %eax,%eax
test   %r8d,%r8d
sete   %al
lea    (%rax,%rax,1),%r8d
add    $0x1,%r8d
lea    0x0(,%r13,8),%esi
xor    %ebx,%ebx
xor    %ecx,%ecx
call   1040 <spi_xfer@plt>
test   %eax,%eax
je     116d <spi_flash_cmd_write+0x4d>
mov    %eax,%ebp
lea    0xe98(%rip),%rdi
mov    %r13d,%esi
jmp    119f <spi_flash_cmd_write+0x7f>
test   %r15d,%r15d
je     11a8 <spi_flash_cmd_write+0x88>
lea    0x0(,%r15,8),%esi
xor    %ebx,%ebx
mov    %r12,%rdi
mov    %r14,%rdx
xor    %ecx,%ecx
mov    $0x2,%r8d
call   1040 <spi_xfer@plt>
test   %eax,%eax
je     11a8 <spi_flash_cmd_write+0x88>
mov    %eax,%ebp
lea    0xe95(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
call   1030 <printf@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.spi_slave = type { i32 }
@.str = [49 x i8] c"SF: Failed to send read command (%zu bytes): %d\0A\00"
@.str.1 = [42 x i8] c"SF: Failed to read %zu bytes of data: %d\0A\00"
define i32 @spi_flash_cmd_write(%struct.spi_slave* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca %struct.spi_slave*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
store %struct.spi_slave* %0, %struct.spi_slave** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
store i64 1, i64* %11
%13 = load i32, i32* %10
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %5
%16 = load i64, i64* %11
%17 = or i64 %16, 2
store i64 %17, i64* %11
br label %18
18: ; preds = %15, %5
%19 = load %struct.spi_slave*, %struct.spi_slave** %6
%20 = load i32, i32* %8
%21 = mul i32 %20, 8
%22 = load i8*, i8** %7
%23 = load i64, i64* %11
%24 = trunc i64 %23 to i32
%25 = call i32 @spi_xfer(%struct.spi_slave* %19, i32 %21, i8* %22, i32* null, i32 %24)
store i32 %25, i32* %12
%26 = load i32, i32* %12
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %18
%29 = load i32, i32* %8
%30 = load i32, i32* %12
%31 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %29, i32 %30)
br label %49
32: ; preds = %18
%33 = load i32, i32* %10
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %32
%36 = load %struct.spi_slave*, %struct.spi_slave** %6
%37 = load i32, i32* %10
%38 = mul i32 %37, 8
%39 = load i8*, i8** %9
%40 = call i32 @spi_xfer(%struct.spi_slave* %36, i32 %38, i8* %39, i32* null, i32 2)
store i32 %40, i32* %12
%41 = load i32, i32* %12
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = load i32, i32* %10
%45 = load i32, i32* %12
%46 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %44, i32 %45)
br label %47
47: ; preds = %43, %35
br label %48
48: ; preds = %47, %32
br label %49
49: ; preds = %48, %28
%50 = load i32, i32* %12
ret i32 %50
}

LLVM IR (-O2):
%struct.spi_slave = type { i32 }
@.str = [49 x i8] c"SF: Failed to send read command (%zu bytes): %d\0A\00"
@.str.1 = [42 x i8] c"SF: Failed to read %zu bytes of data: %d\0A\00"
define i32 @spi_flash_cmd_write(%struct.spi_slave* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = icmp eq i32 %4, 0
%7 = select i1 %6, i32 3, i32 1
%8 = shl i32 %2, 3
%9 = tail call i32 @spi_xfer(%struct.spi_slave* %0, i32 %8, i8* %1, i32* null, i32 %7)
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %5
%12 = tail call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %9)
br label %20
13: ; preds = %5
br i1 %6, label %20, label %14
14: ; preds = %13
%15 = shl i32 %4, 3
%16 = tail call i32 @spi_xfer(%struct.spi_slave* %0, i32 %15, i8* %3, i32* null, i32 2)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = tail call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 %16)
br label %20
20: ; preds = %13, %18, %14, %11
%21 = phi i32 [ %9, %11 ], [ %16, %18 ], [ 0, %14 ], [ 0, %13 ]
ret i32 %21
}