#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 uint16_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fm_wait_for_cmd_complete(int, int, int);
int /*<<< orphan*/ mrvl_hex_dump(char *, int, int *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdout;
int strtol(char *, int /*<<< orphan*/ *, int);
int write(int, int *, int);
int fm_send_cmd(int fd, int argc, char **argv) {
  uint16_t opcode;
  uint8_t cmd[512];
  int len = 0, i = 0, j = 0;
  uint16_t ogf, ocf;
  if (argc < 1) {
    printf("Did not provide cmd content\n");
    printf("Usage: drvtest <device>  [content]\n");
    exit(0);
  }
  ogf = strtol(argv[0], NULL, 16);
  ocf = strtol(argv[1], NULL, 16);
  opcode = (uint16_t)((ocf & 0x03ff) | (ogf << 10));
  cmd[0] = 0x01;
  cmd[1] = (uint8_t)opcode;
  cmd[2] = (uint8_t)(opcode >> 8);
  for (i = 2; i < argc; i++, len++) {
    cmd[j + 4] = (uint8_t)strtol(argv[i], NULL, 16);
    j++;
  }
  cmd[3] = len;
  do {
    printf("< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\n", (ogf), (ocf),
           (len + 4));
    mrvl_hex_dump("  ", 20, (cmd), (len + 4));
    fflush(stdout);
  } while (0);
  if (write(fd, cmd, len + 4) != (len + 4)) {
    perror("Can't write raw command");
    return -1;
  }
  fm_wait_for_cmd_complete(fd, ogf, ocf);
  return 0;
}


x86 assembly (clang, -O0):
<fm_send_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x824(%rbp)
movl   $0x0,-0x828(%rbp)
movl   $0x0,-0x82c(%rbp)
cmpl   $0x1,-0xc(%rbp)
jge    11e0 <fm_send_cmd+0x60>
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,-0x830(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,-0x834(%rbp)
mov    -0x834(%rbp),%eax
and    $0x3ff,%eax
mov    -0x830(%rbp),%ecx
shl    $0xa,%ecx
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x820(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x81c(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x818(%rbp)
movl   $0x2,-0x828(%rbp)
mov    -0x828(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    12cb <fm_send_cmd+0x14b>
mov    -0x18(%rbp),%rax
movslq -0x828(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1060 <strtol@plt>
mov    %eax,%ecx
mov    -0x82c(%rbp),%eax
add    $0x4,%eax
cltq
mov    %ecx,-0x820(%rbp,%rax,4)
mov    -0x82c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x82c(%rbp)
mov    -0x828(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x828(%rbp)
mov    -0x824(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x824(%rbp)
jmp    1259 <fm_send_cmd+0xd9>
mov    -0x824(%rbp),%eax
mov    %eax,-0x814(%rbp)
mov    -0x830(%rbp),%esi
mov    -0x834(%rbp),%edx
mov    -0x824(%rbp),%ecx
add    $0x4,%ecx
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x820(%rbp),%rdx
mov    -0x824(%rbp),%ecx
add    $0x4,%ecx
lea    0xd60(%rip),%rdi
mov    $0x14,%esi
call   1080 <mrvl_hex_dump@plt>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x8(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    -0x824(%rbp),%edx
add    $0x4,%edx
call   1030 <write@plt>
mov    -0x824(%rbp),%ecx
add    $0x4,%ecx
cmp    %ecx,%eax
je     136a <fm_send_cmd+0x1ea>
lea    0xd1b(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <fm_send_cmd+0x205>
mov    -0x8(%rbp),%edi
mov    -0x830(%rbp),%esi
mov    -0x834(%rbp),%edx
call   1050 <fm_wait_for_cmd_complete@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fm_send_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %edi,0xc(%rsp)
test   %esi,%esi
jle    12ca <fm_send_cmd+0x13a>
mov    %rdx,%r14
mov    %esi,%ebp
mov    (%rdx),%rdi
xor    %r13d,%r13d
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,%r15d
mov    0x8(%r14),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,%r12d
and    $0x3ff,%eax
mov    %r15d,%ecx
shl    $0xa,%ecx
or     %eax,%ecx
movl   $0x1,0x10(%rsp)
mov    %ecx,0x14(%rsp)
sar    $0x8,%ecx
mov    %ecx,0x18(%rsp)
cmp    $0x3,%ebp
jb     1231 <fm_send_cmd+0xa1>
add    $0xfffffffe,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x10(%r14,%rbx,8),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,0x20(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1210 <fm_send_cmd+0x80>
mov    %ebp,%r13d
mov    %r13d,0x1c(%rsp)
add    $0x4,%r13d
lea    0xdbf(%rip),%rdi
xor    %r14d,%r14d
mov    %r15d,%esi
mov    %r12d,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdd5(%rip),%rdi
lea    0x10(%rsp),%rbp
mov    $0x14,%esi
mov    %rbp,%rdx
mov    %r13d,%ecx
call   1090 <mrvl_hex_dump@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r13d,%edx
call   1040 <write@plt>
cmp    %r13d,%eax
jne    12b6 <fm_send_cmd+0x126>
mov    %ebx,%edi
mov    %r15d,%esi
mov    %r12d,%edx
call   1060 <fm_wait_for_cmd_complete@plt>
mov    %r14d,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd76(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    12a1 <fm_send_cmd+0x111>
lea    0xd7a(%rip),%rdi
call   1030 <puts@plt>
lea    0xd8a(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [29 x i8] c"Did not provide cmd content\0A\00"
@.str.1 = [36 x i8] c"Usage: drvtest <device> [content]\0A\00"
@.str.2 = [48 x i8] c"< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\0A\00"
@.str.3 = [3 x i8] c" \00"
@stdout = global i32 0
@.str.4 = [24 x i8] c"Can't write raw command\00"
define i32 @fm_send_cmd(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [512 x i32]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %12
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 1
br i1 %16, label %17, label %21
17: ; preds = %3
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %3
%22 = load i8**, i8*** %7
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 @strtol(i8* %24, i32* null, i32 16)
store i32 %25, i32* %13
%26 = load i8**, i8*** %7
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i32 @strtol(i8* %28, i32* null, i32 16)
store i32 %29, i32* %14
%30 = load i32, i32* %14
%31 = and i32 %30, 1023
%32 = load i32, i32* %13
%33 = shl i32 %32, 10
%34 = or i32 %31, %33
store i32 %34, i32* %8
%35 = getelementptr inbounds [512 x i32], [512 x i32]* %9, i64 0, i64 0
store i32 1, i32* %35
%36 = load i32, i32* %8
%37 = getelementptr inbounds [512 x i32], [512 x i32]* %9, i64 0, i64 1
store i32 %36, i32* %37
%38 = load i32, i32* %8
%39 = ashr i32 %38, 8
%40 = getelementptr inbounds [512 x i32], [512 x i32]* %9, i64 0, i64 2
store i32 %39, i32* %40
store i32 2, i32* %11
br label %41
41: ; preds = %58, %21
%42 = load i32, i32* %11
%43 = load i32, i32* %6
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %63
45: ; preds = %41
%46 = load i8**, i8*** %7
%47 = load i32, i32* %11
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %46, i64 %48
%50 = load i8*, i8** %49
%51 = call i32 @strtol(i8* %50, i32* null, i32 16)
%52 = load i32, i32* %12
%53 = add nsw i32 %52, 4
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [512 x i32], [512 x i32]* %9, i64 0, i64 %54
store i32 %51, i32* %55
%56 = load i32, i32* %12
%57 = add nsw i32 %56, 1
store i32 %57, i32* %12
br label %58
58: ; preds = %45
%59 = load i32, i32* %11
%60 = add nsw i32 %59, 1
store i32 %60, i32* %11
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
br label %41
63: ; preds = %41
%64 = load i32, i32* %10
%65 = getelementptr inbounds [512 x i32], [512 x i32]* %9, i64 0, i64 3
store i32 %64, i32* %65
br label %66
66: ; preds = %63
%67 = load i32, i32* %13
%68 = load i32, i32* %14
%69 = load i32, i32* %10
%70 = add nsw i32 %69, 4
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %67, i32 %68, i32 %70)
%72 = getelementptr inbounds [512 x i32], [512 x i32]* %9, i64 0, i64 0
%73 = load i32, i32* %10
%74 = add nsw i32 %73, 4
%75 = call i32 @mrvl_hex_dump(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 20, i32* %72, i32 %74)
%76 = load i32, i32* @stdout
%77 = call i32 @fflush(i32 %76)
br label %78
78: ; preds = %66
%79 = load i32, i32* %5
%80 = getelementptr inbounds [512 x i32], [512 x i32]* %9, i64 0, i64 0
%81 = load i32, i32* %10
%82 = add nsw i32 %81, 4
%83 = call i32 @write(i32 %79, i32* %80, i32 %82)
%84 = load i32, i32* %10
%85 = add nsw i32 %84, 4
%86 = icmp ne i32 %83, %85
br i1 %86, label %87, label %89
87: ; preds = %78
%88 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %4
br label %94
89: ; preds = %78
%90 = load i32, i32* %5
%91 = load i32, i32* %13
%92 = load i32, i32* %14
%93 = call i32 @fm_wait_for_cmd_complete(i32 %90, i32 %91, i32 %92)
store i32 0, i32* %4
br label %94
94: ; preds = %89, %87
%95 = load i32, i32* %4
ret i32 %95
}

LLVM IR (-O2):
@.str.2 = [48 x i8] c"< HCI Command: ogf 0x%02x, ocf 0x%04x, plen %d\0A\00"
@.str.3 = [3 x i8] c" \00"
@stdout = global i32 0
@.str.4 = [24 x i8] c"Can't write raw command\00"
@str = [28 x i8] c"Did not provide cmd content\00"
@str.5 = [35 x i8] c"Usage: drvtest <device> [content]\00"
define i32 @fm_send_cmd(i32 %0, i32 %1, i8** nocapture %2) {
%4 = alloca [512 x i32]
%5 = bitcast [512 x i32]* %4 to i8*
%6 = icmp slt i32 %1, 1
br i1 %6, label %7, label %11
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.5, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %3
%12 = load i8*, i8** %2
%13 = tail call i32 @strtol(i8* nocapture %12, i32* null, i32 16)
%14 = getelementptr inbounds i8*, i8** %2, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @strtol(i8* nocapture %15, i32* null, i32 16)
%17 = and i32 %16, 1023
%18 = shl i32 %13, 10
%19 = or i32 %17, %18
%20 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 0
store i32 1, i32* %20
%21 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 1
store i32 %19, i32* %21
%22 = ashr i32 %19, 8
%23 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 2
store i32 %22, i32* %23
%24 = icmp ugt i32 %1, 2
br i1 %24, label %25, label %39
25: ; preds = %11
%26 = add nsw i32 %1, -2
%27 = zext i32 %26 to i64
br label %28
28: ; preds = %25, %28
%29 = phi i64 [ 2, %25 ], [ %37, %28 ]
%30 = phi i64 [ 0, %25 ], [ %36, %28 ]
%31 = getelementptr inbounds i8*, i8** %2, i64 %29
%32 = load i8*, i8** %31
%33 = tail call i32 @strtol(i8* nocapture %32, i32* null, i32 16)
%34 = add nuw nsw i64 %30, 4
%35 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 %34
store i32 %33, i32* %35
%36 = add nuw nsw i64 %30, 1
%37 = add nuw nsw i64 %29, 1
%38 = icmp eq i64 %36, %27
br i1 %38, label %39, label %28
39: ; preds = %28, %11
%40 = phi i32 [ 0, %11 ], [ %26, %28 ]
%41 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 3
store i32 %40, i32* %41
%42 = add nuw nsw i32 %40, 4
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %16, i32 %42)
%44 = call i32 @mrvl_hex_dump(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 20, i32* %20, i32 %42)
%45 = load i32, i32* @stdout
%46 = call i32 @fflush(i32 %45)
%47 = call i32 @write(i32 %0, i32* %20, i32 %42)
%48 = icmp eq i32 %47, %42
br i1 %48, label %51, label %49
49: ; preds = %39
%50 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %53
51: ; preds = %39
%52 = call i32 @fm_wait_for_cmd_complete(i32 %0, i32 %13, i32 %16)
br label %53
53: ; preds = %51, %49
%54 = phi i32 [ -1, %49 ], [ 0, %51 ]
ret i32 %54
}