#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;
};
struct spi_flash {
  int dummy;
};
typedef int /*<<< orphan*/ idcode;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int spi_claim_bus(struct spi_slave *);
int spi_flash_cmd(struct spi_slave *, int, unsigned char **, int);
int /*<<< orphan*/ spi_free_slave(struct spi_slave *);
int /*<<< orphan*/ spi_release_bus(struct spi_slave *);
scalar_t__ spi_setup_slave(unsigned int, unsigned int, unsigned int,
                           unsigned int);
struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
                                  unsigned int max_hz, unsigned int spi_mode) {
  struct spi_slave *spi;
  struct spi_flash *flash;
  int ret;
  unsigned char idcode[3];
  spi = (struct spi_slave *)spi_setup_slave(bus, cs, max_hz, spi_mode);
  if (!spi) {
    printf("SF: Failed to set up slave\n");
    return NULL;
  }
  ret = spi_claim_bus(spi);
  if (ret) {
    printf("SF: Failed to claim SPI bus: %d\n", ret);
    goto err_claim_bus;
  }
  ret = spi_flash_cmd(spi, 0x9f, &idcode, sizeof(idcode));
  if (ret)
    goto err_read_id;
  printf("SF: Got idcode %02x %02x %02x\n", idcode[0], idcode[1], idcode[2]);
  switch (idcode[0]) {
  default:
    printf("SF: Unsupported manufacturer %02X\n", idcode[0]);
    flash = NULL;
    break;
  }
  if (!flash)
    goto err_manufacturer_probe;
  spi_release_bus(spi);
  return flash;
err_manufacturer_probe:
err_read_id:
  spi_release_bus(spi);
err_claim_bus:
  spi_free_slave(spi);
  return NULL;
}


x86 assembly (clang, -O0):
<spi_flash_probe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1030 <spi_setup_slave@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11af <spi_flash_probe+0x4f>
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    127d <spi_flash_probe+0x11d>
mov    -0x20(%rbp),%rdi
call   1060 <spi_claim_bus@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11db <spi_flash_probe+0x7b>
mov    -0x2c(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    126c <spi_flash_probe+0x10c>
mov    -0x20(%rbp),%rdi
lea    -0x2f(%rbp),%rdx
mov    $0x9f,%esi
mov    $0x3,%ecx
call   1040 <spi_flash_cmd@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1204 <spi_flash_probe+0xa4>
jmp    1263 <spi_flash_probe+0x103>
movzbl -0x2f(%rbp),%esi
movzbl -0x2e(%rbp),%edx
movzbl -0x2d(%rbp),%ecx
lea    0xe26(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl -0x2f(%rbp),%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1248 <spi_flash_probe+0xe8>
jmp    125e <spi_flash_probe+0xfe>
mov    -0x20(%rbp),%rdi
call   1070 <spi_release_bus@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    127d <spi_flash_probe+0x11d>
jmp    1263 <spi_flash_probe+0x103>
mov    -0x20(%rbp),%rdi
call   1070 <spi_release_bus@plt>
mov    -0x20(%rbp),%rdi
call   1080 <spi_free_slave@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spi_flash_probe>:
push   %rbx
sub    $0x10,%rsp
call   1030 <spi_setup_slave@plt>
test   %rax,%rax
je     11a0 <spi_flash_probe+0x30>
mov    %rax,%rbx
mov    %rax,%rdi
call   1070 <spi_claim_bus@plt>
test   %eax,%eax
je     11ae <spi_flash_probe+0x3e>
lea    0xe6b(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1201 <spi_flash_probe+0x91>
lea    0xebc(%rip),%rdi
call   1050 <puts@plt>
jmp    1209 <spi_flash_probe+0x99>
lea    0xd(%rsp),%rdx
mov    %rbx,%rdi
mov    $0x9f,%esi
mov    $0x3,%ecx
call   1040 <spi_flash_cmd@plt>
test   %eax,%eax
jne    11f9 <spi_flash_probe+0x89>
movzbl 0xd(%rsp),%esi
movzbl 0xe(%rsp),%edx
movzbl 0xf(%rsp),%ecx
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movzbl 0xd(%rsp),%esi
lea    0xe4e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <spi_release_bus@plt>
mov    %rbx,%rdi
call   1090 <spi_free_slave@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.spi_flash = type { i32 }
%struct.spi_slave = type { i32 }
@.str = [28 x i8] c"SF: Failed to set up slave\0A\00"
@.str.1 = [33 x i8] c"SF: Failed to claim SPI bus: %d\0A\00"
@.str.2 = [31 x i8] c"SF: Got idcode %02x %02x %02x\0A\00"
@.str.3 = [35 x i8] c"SF: Unsupported manufacturer %02X\0A\00"
define %struct.spi_flash* @spi_flash_probe(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.spi_flash*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.spi_slave*
%11 = alloca %struct.spi_flash*
%12 = alloca i32
%13 = alloca [3 x i8]
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = load i32, i32* %6
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = load i32, i32* %9
%18 = call i64 @spi_setup_slave(i32 %14, i32 %15, i32 %16, i32 %17)
%19 = inttoptr i64 %18 to %struct.spi_slave*
store %struct.spi_slave* %19, %struct.spi_slave** %10
%20 = load %struct.spi_slave*, %struct.spi_slave** %10
%21 = icmp ne %struct.spi_slave* %20, null
br i1 %21, label %24, label %22
22: ; preds = %4
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store %struct.spi_flash* null, %struct.spi_flash** %5
br label %73
24: ; preds = %4
%25 = load %struct.spi_slave*, %struct.spi_slave** %10
%26 = call i32 @spi_claim_bus(%struct.spi_slave* %25)
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* %12
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %30)
br label %70
32: ; preds = %24
%33 = load %struct.spi_slave*, %struct.spi_slave** %10
%34 = bitcast [3 x i8]* %13 to i8**
%35 = call i32 @spi_flash_cmd(%struct.spi_slave* %33, i32 159, i8** %34, i32 3)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
br label %67
39: ; preds = %32
%40 = getelementptr inbounds [3 x i8], [3 x i8]* %13, i64 0, i64 0
%41 = load i8, i8* %40
%42 = zext i8 %41 to i32
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %13, i64 0, i64 1
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = getelementptr inbounds [3 x i8], [3 x i8]* %13, i64 0, i64 2
%47 = load i8, i8* %46
%48 = zext i8 %47 to i32
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %42, i32 %45, i32 %48)
%50 = getelementptr inbounds [3 x i8], [3 x i8]* %13, i64 0, i64 0
%51 = load i8, i8* %50
%52 = zext i8 %51 to i32
switch i32 %52, label %53 [
]
53: ; preds = %39
%54 = getelementptr inbounds [3 x i8], [3 x i8]* %13, i64 0, i64 0
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %56)
store %struct.spi_flash* null, %struct.spi_flash** %11
br label %58
58: ; preds = %53
%59 = load %struct.spi_flash*, %struct.spi_flash** %11
%60 = icmp ne %struct.spi_flash* %59, null
br i1 %60, label %62, label %61
61: ; preds = %58
br label %66
62: ; preds = %58
%63 = load %struct.spi_slave*, %struct.spi_slave** %10
%64 = call i32 @spi_release_bus(%struct.spi_slave* %63)
%65 = load %struct.spi_flash*, %struct.spi_flash** %11
store %struct.spi_flash* %65, %struct.spi_flash** %5
br label %73
66: ; preds = %61
br label %67
67: ; preds = %66, %38
%68 = load %struct.spi_slave*, %struct.spi_slave** %10
%69 = call i32 @spi_release_bus(%struct.spi_slave* %68)
br label %70
70: ; preds = %67, %29
%71 = load %struct.spi_slave*, %struct.spi_slave** %10
%72 = call i32 @spi_free_slave(%struct.spi_slave* %71)
store %struct.spi_flash* null, %struct.spi_flash** %5
br label %73
73: ; preds = %70, %62, %22
%74 = load %struct.spi_flash*, %struct.spi_flash** %5
ret %struct.spi_flash* %74
}

LLVM IR (-O2):
%struct.spi_flash = type { i32 }
%struct.spi_slave = type { i32 }
@.str.1 = [33 x i8] c"SF: Failed to claim SPI bus: %d\0A\00"
@.str.2 = [31 x i8] c"SF: Got idcode %02x %02x %02x\0A\00"
@.str.3 = [35 x i8] c"SF: Unsupported manufacturer %02X\0A\00"
@str = [27 x i8] c"SF: Failed to set up slave\00"
define noalias %struct.spi_flash* @spi_flash_probe(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca [3 x i8]
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%7 = tail call i64 @spi_setup_slave(i32 %0, i32 %1, i32 %2, i32 %3)
%8 = inttoptr i64 %7 to %struct.spi_slave*
%9 = icmp eq i64 %7, 0
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %38
12: ; preds = %4
%13 = tail call i32 @spi_claim_bus(%struct.spi_slave* %8)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %36
17: ; preds = %12
%18 = bitcast [3 x i8]* %5 to i8**
%19 = call i32 @spi_flash_cmd(%struct.spi_slave* %8, i32 159, i8** %18, i32 3)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = load i8, i8* %6
%23 = zext i8 %22 to i32
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 1
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 2
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i32 %23, i32 %26, i32 %29)
%31 = load i8, i8* %6
%32 = zext i8 %31 to i32
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %32)
br label %34
34: ; preds = %21, %17
%35 = call i32 @spi_release_bus(%struct.spi_slave* %8)
br label %36
36: ; preds = %34, %15
%37 = call i32 @spi_free_slave(%struct.spi_slave* %8)
br label %38
38: ; preds = %36, %10
ret %struct.spi_flash* null
}