int getPreambleLength(unsigned int *const length,
                      int (*spiTransfer)(unsigned char *const, unsigned int,
                                         void *const customData),
                      void *const customData) {
  unsigned char data[] = {0x2C, 0x00, 0x00};
  int res = (*spiTransfer)(data, 3, customData);
  if (res < 0) {
    return res;
  }
  *length = ((unsigned int)data[1] << 8) + data[2];
  return 0;
}


x86 assembly (clang, -O0):
<getPreambleLength>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0xee5(%rip),%ax
mov    %ax,-0x23(%rbp)
mov    0xedd(%rip),%al
mov    %al,-0x21(%rbp)
mov    -0x18(%rbp),%rax
lea    -0x23(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x3,%esi
call   *%rax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    1153 <getPreambleLength+0x53>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    116d <getPreambleLength+0x6d>
movzbl -0x22(%rbp),%ecx
shl    $0x8,%ecx
movzbl -0x21(%rbp),%eax
add    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getPreambleLength>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rax
mov    %rdi,%rbx
movb   $0x0,0xe(%rsp)
movw   $0x2c,0xc(%rsp)
lea    0xc(%rsp),%rdi
mov    $0x3,%esi
call   *%rax
test   %eax,%eax
js     1135 <getPreambleLength+0x35>
movzwl 0xd(%rsp),%eax
shl    $0x10,%eax
bswap  %eax
mov    %eax,(%rbx)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.getPreambleLength.data = [3 x i8] c",\00\00"
define i32 @getPreambleLength(i32* %0, i32 (i8*, i32, i8*)* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32 (i8*, i32, i8*)*
%7 = alloca i8*
%8 = alloca [3 x i8]
%9 = alloca i32
store i32* %0, i32** %5
store i32 (i8*, i32, i8*)* %1, i32 (i8*, i32, i8*)** %6
store i8* %2, i8** %7
%10 = bitcast [3 x i8]* %8 to i8*
%11 = load i32 (i8*, i32, i8*)*, i32 (i8*, i32, i8*)** %6
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%13 = load i8*, i8** %7
%14 = call i32 %11(i8* %12, i32 3, i8* %13)
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = load i32, i32* %9
store i32 %18, i32* %4
br label %29
19: ; preds = %3
%20 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = shl i32 %22, 8
%24 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = add i32 %23, %26
%28 = load i32*, i32** %5
store i32 %27, i32* %28
store i32 0, i32* %4
br label %29
29: ; preds = %19, %17
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
@__const.getPreambleLength.data = [3 x i8] c",\00\00"
define i32 @getPreambleLength(i32* nocapture writeonly %0, i32 (i8*, i32, i8*)* nocapture %1, i8* %2) {
%4 = alloca [3 x i8]
%5 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%6 = call i32 %1(i8* %5, i32 3, i8* %2)
%7 = icmp slt i32 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %3
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
%10 = load i8, i8* %9
%11 = zext i8 %10 to i32
%12 = shl nuw nsw i32 %11, 8
%13 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = or i32 %12, %15
store i32 %16, i32* %0
br label %17
17: ; preds = %3, %8
%18 = phi i32 [ 0, %8 ], [ %6, %3 ]
ret i32 %18
}