#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*/ reply;
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);
scalar_t__ memcmp(unsigned char *, int *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read_rx_dep(int);
int /*<<< orphan*/ read_tx_dep(int);
int recv_data(int, unsigned char *, int);
int /*<<< orphan*/ send_data(int, int *, int);
int send_upgrade_cmd(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, 0x50, 0xff, 0xff, 0x7b};
  uint8_t reply[] = {0x51, 0xff, 0xff, 0x3d};
  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;
  }
  if (memcmp(pr, reply, sizeof(reply))) {
    printf("replay isn't match\n");
    printf("%x %x %x %x\n", pr[0], pr[1], pr[2], pr[3]);
    free(buf);
    return -5;
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<send_upgrade_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%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    0xe0c(%rip),%rsi
mov    $0x28,%edx
call   1090 <memcpy@plt>
mov    0xe2b(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0xe28(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <read_tx_dep@plt>
mov    -0x8(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x28,%edx
call   10a0 <send_data@plt>
mov    -0x8(%rbp),%edi
call   10c0 <read_rx_dep@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    125c <send_upgrade_cmd+0x9c>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1393 <send_upgrade_cmd+0x1d3>
mov    -0xc(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    128b <send_upgrade_cmd+0xcb>
lea    0xdda(%rip),%rdi
call   10d0 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1393 <send_upgrade_cmd+0x1d3>
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    12d8 <send_upgrade_cmd+0x118>
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1393 <send_upgrade_cmd+0x1d3>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   10e0 <find_vir_dev_reply@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1316 <send_upgrade_cmd+0x156>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1393 <send_upgrade_cmd+0x1d3>
mov    -0x20(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x10,%edx
call   1080 <memcmp@plt>
cmp    $0x0,%rax
je     1383 <send_upgrade_cmd+0x1c3>
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rax
movzbl (%rax),%esi
mov    -0x20(%rbp),%rax
movzbl 0x1(%rax),%edx
mov    -0x20(%rbp),%rax
movzbl 0x2(%rax),%ecx
mov    -0x20(%rbp),%rax
movzbl 0x3(%rax),%r8d
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1393 <send_upgrade_cmd+0x1d3>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_upgrade_cmd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebp
mov    0xe4f(%rip),%rax
mov    %rax,0x30(%rsp)
movaps 0xe33(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe17(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe3b(%rip),%xmm0
movaps %xmm0,(%rsp)
call   1080 <read_tx_dep@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x28,%edx
call   10a0 <send_data@plt>
mov    %ebp,%edi
call   10c0 <read_rx_dep@plt>
test   %eax,%eax
js     12b7 <send_upgrade_cmd+0xf7>
mov    %eax,%ebx
mov    %eax,%edi
call   10b0 <malloc@plt>
test   %rax,%rax
je     130c <send_upgrade_cmd+0x14c>
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     12ca <send_upgrade_cmd+0x10a>
mov    %r14,%rdi
mov    %ebx,%esi
call   10e0 <find_vir_dev_reply@plt>
test   %rax,%rax
je     12e5 <send_upgrade_cmd+0x125>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
mov    $0x10,%edx
call   1090 <memcmp@plt>
test   %rax,%rax
je     1300 <send_upgrade_cmd+0x140>
lea    0xdd6(%rip),%rdi
call   1040 <puts@plt>
movzbl (%rbx),%esi
movzbl 0x1(%rbx),%edx
movzbl 0x2(%rbx),%ecx
movzbl 0x3(%rbx),%r8d
lea    0xdad(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xfffffffb,%eax
jmp    131d <send_upgrade_cmd+0x15d>
lea    0xde3(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    131d <send_upgrade_cmd+0x15d>
lea    0xdbf(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xfffffffd,%eax
jmp    131d <send_upgrade_cmd+0x15d>
lea    0xd82(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xfffffffc,%eax
jmp    131d <send_upgrade_cmd+0x15d>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    131d <send_upgrade_cmd+0x15d>
lea    0xd2d(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xfffffffe,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.send_upgrade_cmd.cmd = [10 x i32] [i32 251, i32 129, i32 0, i32 0, i32 4, i32 34, i32 80, i32 255, i32 255, i32 123]
@__const.send_upgrade_cmd.reply = [4 x i32] [i32 81, i32 255, i32 255, i32 61]
@.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"replay isn't match\0A\00"
@.str.5 = [13 x i8] c"%x %x %x %x\0A\00"
define i32 @send_upgrade_cmd(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]
%9 = alloca [4 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
%10 = bitcast [10 x i32]* %8 to i8*
%11 = bitcast [4 x i32]* %9 to i8*
%12 = load i32, i32* %3
%13 = call i32 @read_tx_dep(i32 %12)
%14 = load i32, i32* %3
%15 = getelementptr inbounds [10 x i32], [10 x i32]* %8, i64 0, i64 0
%16 = call i32 @send_data(i32 %14, i32* %15, i32 40)
%17 = load i32, i32* %3
%18 = call i32 @read_rx_dep(i32 %17)
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %1
%22 = 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 %83
23: ; preds = %1
%24 = load i32, i32* %4
%25 = call i8* @malloc(i32 %24)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = 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 %83
30: ; preds = %23
%31 = load i8*, i8** %6
%32 = load i32, i32* %4
%33 = call i32 @memset(i8* %31, i32 0, i32 %32)
%34 = load i32, i32* %3
%35 = load i8*, i8** %6
%36 = load i32, i32* %4
%37 = call i32 @recv_data(i32 %34, i8* %35, i32 %36)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %30
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%42 = load i8*, i8** %6
%43 = call i32 @free(i8* %42)
store i32 -3, i32* %2
br label %83
44: ; preds = %30
%45 = load i8*, i8** %6
%46 = load i32, i32* %4
%47 = call i8* @find_vir_dev_reply(i8* %45, i32 %46)
store i8* %47, i8** %7
%48 = load i8*, i8** %7
%49 = icmp eq i8* %48, null
br i1 %49, label %50, label %54
50: ; preds = %44
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
store i32 -4, i32* %2
br label %83
54: ; preds = %44
%55 = load i8*, i8** %7
%56 = getelementptr inbounds [4 x i32], [4 x i32]* %9, i64 0, i64 0
%57 = call i64 @memcmp(i8* %55, i32* %56, i32 16)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %80
59: ; preds = %54
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%61 = load i8*, i8** %7
%62 = getelementptr inbounds i8, i8* %61, i64 0
%63 = load i8, i8* %62
%64 = zext i8 %63 to i32
%65 = load i8*, i8** %7
%66 = getelementptr inbounds i8, i8* %65, i64 1
%67 = load i8, i8* %66
%68 = zext i8 %67 to i32
%69 = load i8*, i8** %7
%70 = getelementptr inbounds i8, i8* %69, i64 2
%71 = load i8, i8* %70
%72 = zext i8 %71 to i32
%73 = load i8*, i8** %7
%74 = getelementptr inbounds i8, i8* %73, i64 3
%75 = load i8, i8* %74
%76 = zext i8 %75 to i32
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 %64, i32 %68, i32 %72, i32 %76)
%78 = load i8*, i8** %6
%79 = call i32 @free(i8* %78)
store i32 -5, i32* %2
br label %83
80: ; preds = %54
%81 = load i8*, i8** %6
%82 = call i32 @free(i8* %81)
store i32 0, i32* %2
br label %83
83: ; preds = %80, %59, %50, %40, %28, %21
%84 = load i32, i32* %2
ret i32 %84
}

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