#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*/ memset(unsigned char *, int /*<<< orphan*/, int);
int udrw_cmd(int, unsigned char *, int, char *, int, int /*<<< orphan*/);
int udrw_read_vendor_buffer(int fd, void *buf, int len) {
  char tmpbuf[512];
  unsigned char cmd[16];
  memset(cmd, 0, sizeof(cmd));
  cmd[0] = 0x20;
  cmd[2] = 'I';
  cmd[3] = 'S';
  cmd[4] = 'E';
  if (udrw_cmd(fd, cmd, 10, tmpbuf, sizeof(tmpbuf), SG_DXFER_FROM_DEV) == -1)
    return -1;
  memset(cmd, 0, sizeof(cmd));
  cmd[0] = 0x3C;
  cmd[1] = 0x01;
  cmd[2] = 0x05;
  cmd[7] = 0x02;
  if (udrw_cmd(fd, cmd, 10, buf, len, SG_DXFER_FROM_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_read_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   $0x49,-0x22e(%rbp)
movb   $0x53,-0x22d(%rbp)
movb   $0x45,-0x22c(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x230(%rbp),%rsi
lea    -0x220(%rbp),%rcx
mov    0x2e6c(%rip),%rax
mov    (%rax),%r9d
mov    $0xa,%edx
mov    $0x200,%r8d
call   1040 <udrw_cmd@plt>
cmp    $0xffffffff,%eax
jne    11a4 <udrw_read_vendor_buffer+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <udrw_read_vendor_buffer+0x156>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movb   $0x3c,-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    0x2dfc(%rip),%rax
mov    (%rax),%r9d
mov    $0xa,%edx
call   1040 <udrw_cmd@plt>
cmp    $0xffffffff,%eax
jne    120e <udrw_read_vendor_buffer+0xee>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <udrw_read_vendor_buffer+0x156>
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    0x2da1(%rip),%rax
mov    (%rax),%r9d
mov    $0xa,%edx
mov    $0x200,%r8d
call   1040 <udrw_cmd@plt>
cmp    $0xffffffff,%eax
jne    126f <udrw_read_vendor_buffer+0x14f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <udrw_read_vendor_buffer+0x156>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udrw_read_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>
movb   $0x20,(%rsp)
movw   $0x5349,0x2(%rsp)
movb   $0x45,0x4(%rsp)
mov    0x2e86(%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     120f <udrw_read_vendor_buffer+0xef>
mov    %rsp,%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movw   $0x13c,(%rsp)
movb   $0x5,0x2(%rsp)
movb   $0x2,0x7(%rsp)
mov    0x0(%r13),%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     120f <udrw_read_vendor_buffer+0xef>
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
define i32 @udrw_read_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 2
store i8 73, i8* %13
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 3
store i8 83, i8* %14
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 4
store i8 69, i8* %15
%16 = load i32, i32* %5
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%18 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%19 = load i32, i32* @SG_DXFER_FROM_DEV
%20 = call i32 @udrw_cmd(i32 %16, i8* %17, i32 10, i8* %18, i32 512, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %3
store i32 -1, i32* %4
br label %51
23: ; preds = %3
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%25 = call i32 @memset(i8* %24, i32 0, i32 16)
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
store i8 60, i8* %26
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 1
store i8 1, i8* %27
%28 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 2
store i8 5, i8* %28
%29 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 7
store i8 2, i8* %29
%30 = load i32, i32* %5
%31 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%32 = load i8*, i8** %6
%33 = load i32, i32* %7
%34 = load i32, i32* @SG_DXFER_FROM_DEV
%35 = call i32 @udrw_cmd(i32 %30, i8* %31, i32 10, i8* %32, i32 %33, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %23
store i32 -1, i32* %4
br label %51
38: ; preds = %23
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%40 = call i32 @memset(i8* %39, i32 0, i32 16)
%41 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
store i8 32, i8* %41
%42 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 1
store i8 27, i8* %42
%43 = load i32, i32* %5
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%45 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%46 = load i32, i32* @SG_DXFER_FROM_DEV
%47 = call i32 @udrw_cmd(i32 %43, i8* %44, i32 10, i8* %45, i32 512, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %38
store i32 -1, i32* %4
br label %51
50: ; preds = %38
store i32 0, i32* %4
br label %51
51: ; preds = %50, %49, %37, %22
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@SG_DXFER_FROM_DEV = global i32 0
define i32 @udrw_read_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 2
store i8 73, i8* %9
%10 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 3
store i8 83, i8* %10
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 4
store i8 69, i8* %11
%12 = load i32, i32* @SG_DXFER_FROM_DEV
%13 = call i32 @udrw_cmd(i32 %0, i8* %7, i32 10, i8* %6, i32 512, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %28, label %15
15: ; preds = %3
%16 = call i32 @memset(i8* %7, i32 0, i32 16)
store i8 60, i8* %7
%17 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 1
store i8 1, i8* %17
store i8 5, i8* %9
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 7
store i8 2, i8* %18
%19 = load i32, i32* @SG_DXFER_FROM_DEV
%20 = call i32 @udrw_cmd(i32 %0, i8* %7, i32 10, i8* %1, i32 %2, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %28, label %22
22: ; preds = %15
%23 = call i32 @memset(i8* %7, i32 0, i32 16)
store i8 32, i8* %7
store i8 27, i8* %17
%24 = load i32, i32* @SG_DXFER_FROM_DEV
%25 = call i32 @udrw_cmd(i32 %0, i8* %7, i32 10, i8* %6, i32 512, i32 %24)
%26 = icmp eq i32 %25, -1
%27 = sext i1 %26 to i32
br label %28
28: ; preds = %22, %15, %3
%29 = phi i32 [ -1, %3 ], [ -1, %15 ], [ %27, %22 ]
ret i32 %29
}