#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, unsigned char *, void *, int);
int spi_flash_cmd(struct spi_slave *spi, unsigned char cmd, void *response,
                  unsigned int len) {
  unsigned long flags = 0x01;
  int ret;
  if (len == 0)
    flags |= 0x02;
  ret = spi_xfer(spi, 8, &cmd, NULL, flags);
  if (ret) {
    printf("SF: Failed to send command %02x: %d\n", cmd, ret);
    return ret;
  }
  if (len) {
    ret = spi_xfer(spi, len * 8, NULL, response, 0x02);
    if (ret)
      printf("SF: Failed to read response (%zu bytes): %d\n", len, ret);
  }
  return ret;
}


x86 assembly (clang, -O0):
<spi_flash_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
movq   $0x1,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1157 <spi_flash_cmd+0x37>
mov    -0x30(%rbp),%rax
or     $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    %eax,%r8d
mov    $0x8,%esi
lea    -0x11(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1040 <spi_xfer@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     119f <spi_flash_cmd+0x7f>
movzbl -0x11(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f0 <spi_flash_cmd+0xd0>
cmpl   $0x0,-0x24(%rbp)
je     11ea <spi_flash_cmd+0xca>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
shl    $0x3,%esi
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edx
mov    $0x2,%r8d
call   1040 <spi_xfer@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11e5 <spi_flash_cmd+0xc5>
mov    -0x24(%rbp),%esi
mov    -0x34(%rbp),%edx
lea    0xe45(%rip),%rdi
call   1030 <printf@plt>
jmp    11ea <spi_flash_cmd+0xca>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_flash_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rdi,%r12
mov    %sil,0xf(%rsp)
xor    %eax,%eax
test   %ecx,%ecx
sete   %al
lea    (%rax,%rax,1),%r8d
add    $0x1,%r8d
xor    %ebx,%ebx
lea    0xf(%rsp),%rdx
mov    $0x8,%esi
xor    %ecx,%ecx
call   1040 <spi_xfer@plt>
test   %eax,%eax
je     1170 <spi_flash_cmd+0x50>
mov    %eax,%ebp
movzbl 0xf(%rsp),%esi
lea    0xe92(%rip),%rdi
jmp    11a2 <spi_flash_cmd+0x82>
test   %r15d,%r15d
je     11ab <spi_flash_cmd+0x8b>
lea    0x0(,%r15,8),%esi
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %edx,%edx
mov    %r14,%rcx
mov    $0x2,%r8d
call   1040 <spi_xfer@plt>
test   %eax,%eax
je     11ab <spi_flash_cmd+0x8b>
mov    %eax,%ebp
lea    0xe86(%rip),%rdi
mov    %r15d,%esi
mov    %ebp,%edx
call   1030 <printf@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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