#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 usbdevfs_ctrltransfer {
  int bRequestType;
  int bRequest;
  int wValue;
  int wLength;
  int timeout;
  void *data;
  scalar_t__ wIndex;
};

/* Variables and functions */
int /*<<< orphan*/ USBDEVFS_CONTROL;
int /*<<< orphan*/ error_and_exit(char *);
int ioctl(int, int /*<<< orphan*/, struct usbdevfs_ctrltransfer *);
void do_firmload(int fd, int addr, int len, void *data) {
  struct usbdevfs_ctrltransfer ctrl;
  int status;
  ctrl.bRequestType = 0x40;
  ctrl.bRequest = 0xA0;
  ctrl.wValue = addr;
  ctrl.wIndex = 0;
  ctrl.wLength = len;
  ctrl.timeout = 1000;
  ctrl.data = data;
  status = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
  if (status != len) {
    error_and_exit("cannot issue control transfer");
  }
}


x86 assembly (clang, -O0):
<do_firmload>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x40,-0x40(%rbp)
movl   $0xa0,-0x3c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
movl   $0x3e8,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e68(%rip),%rax
mov    (%rax),%esi
lea    -0x40(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1196 <do_firmload+0x76>
lea    0xe6f(%rip),%rdi
call   1040 <error_and_exit@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_firmload>:
push   %rbx
sub    $0x30,%rsp
mov    %edx,%ebx
movabs $0xa000000040,%rax
mov    %rax,0x8(%rsp)
mov    %esi,0x10(%rsp)
movq   $0x0,0x28(%rsp)
mov    %edx,0x14(%rsp)
movl   $0x3e8,0x18(%rsp)
mov    %rcx,0x20(%rsp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <ioctl@plt>
cmp    %ebx,%eax
je     1177 <do_firmload+0x57>
lea    0xe8e(%rip),%rdi
call   1040 <error_and_exit@plt>
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.usbdevfs_ctrltransfer = type { i32, i32, i32, i32, i32, i8*, i64 }
@USBDEVFS_CONTROL = global i32 0
@.str = [30 x i8] c"cannot issue control transfer\00"
define void @do_firmload(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.usbdevfs_ctrltransfer
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%11 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %9, i32 0, i32 0
store i32 64, i32* %11
%12 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %9, i32 0, i32 1
store i32 160, i32* %12
%13 = load i32, i32* %6
%14 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %9, i32 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %9, i32 0, i32 6
store i64 0, i64* %15
%16 = load i32, i32* %7
%17 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %9, i32 0, i32 3
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %9, i32 0, i32 4
store i32 1000, i32* %18
%19 = load i8*, i8** %8
%20 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %9, i32 0, i32 5
store i8* %19, i8** %20
%21 = load i32, i32* %5
%22 = load i32, i32* @USBDEVFS_CONTROL
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.usbdevfs_ctrltransfer* %9)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = load i32, i32* %7
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %4
%28 = call i32 @error_and_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %4
ret void
}

LLVM IR (-O2):
%struct.usbdevfs_ctrltransfer = type { i32, i32, i32, i32, i32, i8*, i64 }
@USBDEVFS_CONTROL = global i32 0
@.str = [30 x i8] c"cannot issue control transfer\00"
define void @do_firmload(i32 %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca %struct.usbdevfs_ctrltransfer
%6 = bitcast %struct.usbdevfs_ctrltransfer* %5 to i8*
%7 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %5, i64 0, i32 0
store i32 64, i32* %7
%8 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %5, i64 0, i32 1
store i32 160, i32* %8
%9 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %5, i64 0, i32 2
store i32 %1, i32* %9
%10 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %5, i64 0, i32 6
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %5, i64 0, i32 3
store i32 %2, i32* %11
%12 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %5, i64 0, i32 4
store i32 1000, i32* %12
%13 = getelementptr inbounds %struct.usbdevfs_ctrltransfer, %struct.usbdevfs_ctrltransfer* %5, i64 0, i32 5
store i8* %3, i8** %13
%14 = load i32, i32* @USBDEVFS_CONTROL
%15 = call i32 @ioctl(i32 %0, i32 %14, %struct.usbdevfs_ctrltransfer* %5)
%16 = icmp eq i32 %15, %2
br i1 %16, label %19, label %17
17: ; preds = %4
%18 = call i32 @error_and_exit(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %4
ret void
}