#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 */
typedef int uint8_t;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
unsigned char *find_vir_dev_reply(unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read_rx_dep(int);
int read_tx_dep(int);
int recv_data(int, unsigned char *, int);
int /*<<< orphan*/ send_data(int, int *, int);
int read_spi_version(int fd) {
  int dep = 0;
  int ret = 0;
  unsigned char *buf = NULL;
  unsigned char *pr = NULL;
  uint8_t cmd[] = {0xfb, 0x81, 0x00, 0x00, 0x04, 0x22, 0x00, 0xff, 0xff, 0x2d};
  dep = read_tx_dep(fd);
  send_data(fd, cmd, sizeof(cmd));
  dep = read_rx_dep(fd);
  if (dep < 0) {
    printf("read rx dep failed\n");
    return -1;
  }
  buf = malloc(dep);
  if (buf == NULL) {
    perror("malloc failed");
    return -2;
  }
  memset(buf, 0, dep);
  ret = recv_data(fd, buf, dep);
  if (ret < 0) {
    printf("recv data failed\n");
    free(buf);
    return -3;
  }
  pr = find_vir_dev_reply(buf, dep);
  if (pr == NULL) {
    printf("can't find vritual device's reply\n");
    free(buf);
    return -4;
  }
  printf("version: %02x %02x\n", pr[1], pr[2]);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<read_spi_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe1c(%rip),%rsi
mov    $0x28,%edx
call   1080 <memcpy@plt>
mov    -0x8(%rbp),%edi
call   1070 <read_tx_dep@plt>
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x28,%edx
call   1090 <send_data@plt>
mov    -0x8(%rbp),%edi
call   10b0 <read_rx_dep@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1239 <read_spi_version+0x89>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <read_spi_version+0x171>
mov    -0xc(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1268 <read_spi_version+0xb8>
lea    0xde5(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1321 <read_spi_version+0x171>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1040 <recv_data@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    12b5 <read_spi_version+0x105>
lea    0xdb1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1321 <read_spi_version+0x171>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   10d0 <find_vir_dev_reply@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12f3 <read_spi_version+0x143>
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1321 <read_spi_version+0x171>
mov    -0x20(%rbp),%rax
movzbl 0x1(%rax),%esi
mov    -0x20(%rbp),%rax
movzbl 0x2(%rax),%edx
lea    0xd75(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_spi_version>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebp
mov    0xe5f(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0xe43(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe27(%rip),%xmm0
movaps %xmm0,(%rsp)
call   1080 <read_tx_dep@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x28,%edx
call   1090 <send_data@plt>
mov    %ebp,%edi
call   10b0 <read_rx_dep@plt>
test   %eax,%eax
js     125e <read_spi_version+0xae>
mov    %eax,%ebx
mov    %eax,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12a7 <read_spi_version+0xf7>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <recv_data@plt>
test   %eax,%eax
js     1271 <read_spi_version+0xc1>
mov    %r14,%rdi
mov    %ebx,%esi
call   10d0 <find_vir_dev_reply@plt>
test   %rax,%rax
je     128c <read_spi_version+0xdc>
movzbl 0x1(%rax),%esi
movzbl 0x2(%rax),%edx
lea    0xdeb(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    12b8 <read_spi_version+0x108>
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
jmp    12b8 <read_spi_version+0x108>
lea    0xdf4(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xfffffffd,%ebx
jmp    12b8 <read_spi_version+0x108>
lea    0xdb7(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xfffffffc,%ebx
jmp    12b8 <read_spi_version+0x108>
lea    0xd7a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.read_spi_version.cmd = [10 x i32] [i32 251, i32 129, i32 0, i32 0, i32 4, i32 34, i32 0, i32 255, i32 255, i32 45]
@.str = [20 x i8] c"read rx dep failed\0A\00"
@.str.1 = [14 x i8] c"malloc failed\00"
@.str.2 = [18 x i8] c"recv data failed\0A\00"
@.str.3 = [35 x i8] c"can't find vritual device's reply\0A\00"
@.str.4 = [20 x i8] c"version: %02x %02x\0A\00"
define i32 @read_spi_version(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca [10 x i32]
store i32 %0, i32* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i8* null, i8** %6
store i8* null, i8** %7
%9 = bitcast [10 x i32]* %8 to i8*
%10 = load i32, i32* %3
%11 = call i32 @read_tx_dep(i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %3
%13 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 0
%14 = call i32 @send_data(i32 %12, i32* %13, i32 40)
%15 = load i32, i32* %3
%16 = call i32 @read_rx_dep(i32 %15)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %64
21: ; preds = %1
%22 = load i32, i32* %4
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %2
br label %64
28: ; preds = %21
%29 = load i8*, i8** %6
%30 = load i32, i32* %4
%31 = call i32 @memset(i8* %29, i32 0, i32 %30)
%32 = load i32, i32* %3
%33 = load i8*, i8** %6
%34 = load i32, i32* %4
%35 = call i32 @recv_data(i32 %32, i8* %33, i32 %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %28
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%40 = load i8*, i8** %6
%41 = call i32 @free(i8* %40)
store i32 -3, i32* %2
br label %64
42: ; preds = %28
%43 = load i8*, i8** %6
%44 = load i32, i32* %4
%45 = call i8* @find_vir_dev_reply(i8* %43, i32 %44)
store i8* %45, i8** %7
%46 = load i8*, i8** %7
%47 = icmp eq i8* %46, null
br i1 %47, label %48, label %52
48: ; preds = %42
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%50 = load i8*, i8** %6
%51 = call i32 @free(i8* %50)
store i32 -4, i32* %2
br label %64
52: ; preds = %42
%53 = load i8*, i8** %7
%54 = getelementptr inbounds i8, i8* %53, i64 1
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = load i8*, i8** %7
%58 = getelementptr inbounds i8, i8* %57, i64 2
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %56, i32 %60)
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
store i32 0, i32* %2
br label %64
64: ; preds = %52, %48, %38, %26, %19
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
@__const.read_spi_version.cmd = [10 x i32] [i32 251, i32 129, i32 0, i32 0, i32 4, i32 34, i32 0, i32 255, i32 255, i32 45]
@.str.1 = [14 x i8] c"malloc failed\00"
@.str.4 = [20 x i8] c"version: %02x %02x\0A\00"
@str = [34 x i8] c"can't find vritual device's reply\00"
@str.5 = [17 x i8] c"recv data failed\00"
@str.6 = [19 x i8] c"read rx dep failed\00"
define i32 @read_spi_version(i32 %0) {
%2 = alloca [10 x i32]
%3 = bitcast [10 x i32]* %2 to i8*
%4 = tail call i32 @read_tx_dep(i32 %0)
%5 = getelementptr inbounds [10 x i32], [10 x i32]* %2, i64 0, i64 0
%6 = call i32 @send_data(i32 %0, i32* %5, i32 40)
%7 = call i32 @read_rx_dep(i32 %0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %38
11: ; preds = %1
%12 = call i8* @malloc(i32 %7)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %38
16: ; preds = %11
%17 = call i32 @memset(i8* %12, i32 0, i32 %7)
%18 = call i32 @recv_data(i32 %0, i8* %12, i32 %7)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.5, i64 0, i64 0))
%22 = call i32 @free(i8* %12)
br label %38
23: ; preds = %16
%24 = call i8* @find_vir_dev_reply(i8* %12, i32 %7)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%28 = call i32 @free(i8* %12)
br label %38
29: ; preds = %23
%30 = getelementptr inbounds i8, i8* %24, i64 1
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = getelementptr inbounds i8, i8* %24, i64 2
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %32, i32 %35)
%37 = call i32 @free(i8* %12)
br label %38
38: ; preds = %29, %26, %20, %14, %9
%39 = phi i32 [ -1, %9 ], [ -2, %14 ], [ -3, %20 ], [ -4, %26 ], [ 0, %29 ]
ret i32 %39
}