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

/* Variables and functions */
int /*<<< orphan*/ SG_DXFER_FROM_DEV;
int /*<<< orphan*/ SG_DXFER_TO_DEV;
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int udrw_cmd(int, unsigned char *, int, unsigned char *, int,
             int /*<<< orphan*/);
int udrw_write_vendor_buffer(int fd, void *buf, int len) {
  unsigned char tmpbuf[512];
  unsigned char cmd[16];
  memset(cmd, 0, sizeof(cmd));
  cmd[0] = 0x20;
  cmd[1] = 0x1a;
  if (udrw_cmd(fd, cmd, 10, tmpbuf, sizeof(tmpbuf), SG_DXFER_FROM_DEV) == -1)
    return -1;
  memset(cmd, 0, sizeof(cmd));
  cmd[0] = 0x3b;
  cmd[1] = 0x01;
  cmd[2] = 0x05;
  cmd[7] = 0x02;
  if (udrw_cmd(fd, cmd, 10, buf, len, SG_DXFER_TO_DEV) == -1)
    return -1;
  memset(cmd, 0, sizeof(cmd));
  cmd[0] = 0x20;
  cmd[1] = 0x1b;
  if (udrw_cmd(fd, cmd, 10, tmpbuf, sizeof(tmpbuf), SG_DXFER_FROM_DEV) == -1)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<udrw_write_vendor_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movb   $0x20,-0x230(%rbp)
movb   $0x1a,-0x22f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x220(%rbp),%rcx
mov    0x2e72(%rip),%rax
mov    (%rax),%r9d
mov    $0xa,%edx
mov    $0x200,%r8d
call   1040 <udrw_cmd@plt>
cmp    $0xffffffff,%eax
jne    1196 <udrw_write_vendor_buffer+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <udrw_write_vendor_buffer+0x148>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movb   $0x3b,-0x230(%rbp)
movb   $0x1,-0x22f(%rbp)
movb   $0x5,-0x22e(%rbp)
movb   $0x2,-0x229(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    0x2e1a(%rip),%rax
mov    (%rax),%r9d
mov    $0xa,%edx
call   1040 <udrw_cmd@plt>
cmp    $0xffffffff,%eax
jne    1200 <udrw_write_vendor_buffer+0xe0>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <udrw_write_vendor_buffer+0x148>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movb   $0x20,-0x230(%rbp)
movb   $0x1b,-0x22f(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x220(%rbp),%rcx
mov    0x2da7(%rip),%rax
mov    (%rax),%r9d
mov    $0xa,%edx
mov    $0x200,%r8d
call   1040 <udrw_cmd@plt>
cmp    $0xffffffff,%eax
jne    1261 <udrw_write_vendor_buffer+0x141>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <udrw_write_vendor_buffer+0x148>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udrw_write_vendor_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movw   $0x1a20,(%rsp)
mov    0x2e88(%rip),%r13
mov    0x0(%r13),%r9d
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xa,%edx
mov    $0x200,%r8d
call   1040 <udrw_cmd@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     120b <udrw_write_vendor_buffer+0xeb>
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movw   $0x13b,(%rsp)
movb   $0x5,0x2(%rsp)
movb   $0x2,0x7(%rsp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%r9d
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0xa,%edx
mov    %r15,%rcx
mov    %r14d,%r8d
call   1040 <udrw_cmd@plt>
cmp    $0xffffffff,%eax
je     120b <udrw_write_vendor_buffer+0xeb>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movw   $0x1b20,(%rsp)
mov    0x0(%r13),%r9d
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xa,%edx
mov    $0x200,%r8d
call   1040 <udrw_cmd@plt>
xor    %ebp,%ebp
cmp    $0xffffffff,%eax
sete   %bpl
neg    %ebp
mov    %ebp,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SG_DXFER_FROM_DEV = global i32 0
@SG_DXFER_TO_DEV = global i32 0
define i32 @udrw_write_vendor_buffer(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca [16 x i8]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 0, i32 16)
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
store i8 32, i8* %12
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 1
store i8 26, i8* %13
%14 = load i32, i32* %5
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%17 = load i32, i32* @SG_DXFER_FROM_DEV
%18 = call i32 @udrw_cmd(i32 %14, i8* %15, i32 10, i8* %16, i32 512, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %49
21: ; preds = %3
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i32 0, i32 16)
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
store i8 59, i8* %24
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 1
store i8 1, i8* %25
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 2
store i8 5, i8* %26
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 7
store i8 2, i8* %27
%28 = load i32, i32* %5
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%30 = load i8*, i8** %6
%31 = load i32, i32* %7
%32 = load i32, i32* @SG_DXFER_TO_DEV
%33 = call i32 @udrw_cmd(i32 %28, i8* %29, i32 10, i8* %30, i32 %31, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %21
store i32 -1, i32* %4
br label %49
36: ; preds = %21
%37 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%38 = call i32 @memset(i8* %37, i32 0, i32 16)
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
store i8 32, i8* %39
%40 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 1
store i8 27, i8* %40
%41 = load i32, i32* %5
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%44 = load i32, i32* @SG_DXFER_FROM_DEV
%45 = call i32 @udrw_cmd(i32 %41, i8* %42, i32 10, i8* %43, i32 512, i32 %44)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %36
store i32 -1, i32* %4
br label %49
48: ; preds = %36
store i32 0, i32* %4
br label %49
49: ; preds = %48, %47, %35, %20
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
@SG_DXFER_FROM_DEV = global i32 0
@SG_DXFER_TO_DEV = global i32 0
define i32 @udrw_write_vendor_buffer(i32 %0, i8* %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = alloca [16 x i8]
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%8 = call i32 @memset(i8* %7, i32 0, i32 16)
store i8 32, i8* %7
%9 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 1
store i8 26, i8* %9
%10 = load i32, i32* @SG_DXFER_FROM_DEV
%11 = call i32 @udrw_cmd(i32 %0, i8* %7, i32 10, i8* %6, i32 512, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %26, label %13
13: ; preds = %3
%14 = call i32 @memset(i8* %7, i32 0, i32 16)
store i8 59, i8* %7
store i8 1, i8* %9
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 2
store i8 5, i8* %15
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 7
store i8 2, i8* %16
%17 = load i32, i32* @SG_DXFER_TO_DEV
%18 = call i32 @udrw_cmd(i32 %0, i8* %7, i32 10, i8* %1, i32 %2, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %26, label %20
20: ; preds = %13
%21 = call i32 @memset(i8* %7, i32 0, i32 16)
store i8 32, i8* %7
store i8 27, i8* %9
%22 = load i32, i32* @SG_DXFER_FROM_DEV
%23 = call i32 @udrw_cmd(i32 %0, i8* %7, i32 10, i8* %6, i32 512, i32 %22)
%24 = icmp eq i32 %23, -1
%25 = sext i1 %24 to i32
br label %26
26: ; preds = %20, %13, %3
%27 = phi i32 [ -1, %3 ], [ -1, %13 ], [ %25, %20 ]
ret i32 %27
}