#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*/ windowbytes;
typedef int uint8_t;
typedef int /*<<< orphan*/ uint16_t;
struct kvss905c_window {
  int dummy;
};
typedef int /*<<< orphan*/ length;
typedef int /*<<< orphan*/ command;

/* Variables and functions */
int /*<<< orphan*/ SG_DXFER_TO_DEV;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ htons(int);
int kvss905c_window_serialise(int *, struct kvss905c_window const *);
int /*<<< orphan*/ memcpy(int *, int /*<<< orphan*/ const *, int);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int scsi_command(int, int /*<<< orphan*/, int const *, int, int *, int const,
                 int *, int /*<<< orphan*/);
int kvss905c_set_windows(int fd, const struct kvss905c_window *window,
                         char duplex, uint8_t *requestsense) {
  uint8_t windowbytes[6 + 2 + 64];
  memset(windowbytes, 0, sizeof(windowbytes));
  const int bytes_written = kvss905c_window_serialise(windowbytes + 8, window);
  if (bytes_written != 64)
    abort();
  const uint16_t length = htons(64);
  memcpy(windowbytes + 6, &length, sizeof(length));
  static const int transfer_length = sizeof(windowbytes);
  const uint8_t command[] = {0x24,
                             0,
                             0,
                             0,
                             0,
                             0,
                             transfer_length >> 16,
                             transfer_length >> 8,
                             transfer_length,
                             0};
  const int r = scsi_command(fd, SG_DXFER_TO_DEV, command, sizeof(command),
                             windowbytes, transfer_length, requestsense, 0);
  if (r)
    return r;
  if (duplex) {
    windowbytes[8] = 0x80;
    return scsi_command(fd, SG_DXFER_TO_DEV, command, sizeof(command),
                        windowbytes, transfer_length, requestsense, 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<kvss905c_set_windows>:
push   %rbp
mov    %rsp,%rbp
sub    $0x190,%rsp
mov    %dl,%al
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0x120,%edx
call   1060 <memset@plt>
lea    -0x140(%rbp),%rdi
add    $0x20,%rdi
mov    -0x10(%rbp),%rsi
call   1050 <kvss905c_window_serialise@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x40,-0x144(%rbp)
je     11bc <kvss905c_set_windows+0x5c>
mov    $0x0,%al
call   1030 <abort@plt>
mov    $0x40,%edi
call   1040 <htons@plt>
mov    %eax,-0x148(%rbp)
lea    -0x140(%rbp),%rdi
add    $0x18,%rdi
lea    -0x148(%rbp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
lea    -0x170(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
movl   $0x24,-0x170(%rbp)
movl   $0x1,-0x154(%rbp)
movl   $0x120,-0x150(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
lea    -0x170(%rbp),%rdx
lea    -0x140(%rbp),%r8
mov    -0x20(%rbp),%rax
mov    $0x28,%ecx
mov    $0x120,%r9d
xor    %r10d,%r10d
mov    %rax,(%rsp)
movl   $0x0,0x8(%rsp)
call   1080 <scsi_command@plt>
mov    %eax,-0x174(%rbp)
cmpl   $0x0,-0x174(%rbp)
je     1277 <kvss905c_set_windows+0x117>
mov    -0x174(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d7 <kvss905c_set_windows+0x177>
cmpb   $0x0,-0x11(%rbp)
je     12d0 <kvss905c_set_windows+0x170>
movl   $0x80,-0x120(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
lea    -0x170(%rbp),%rdx
lea    -0x140(%rbp),%r8
mov    -0x20(%rbp),%rax
mov    $0x28,%ecx
mov    $0x120,%r9d
xor    %r10d,%r10d
mov    %rax,(%rsp)
movl   $0x0,0x8(%rsp)
call   1080 <scsi_command@plt>
mov    %eax,-0x4(%rbp)
jmp    12d7 <kvss905c_set_windows+0x177>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x190,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvss905c_set_windows>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x168,%rsp
mov    %rcx,%r15
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x40(%rsp),%rdi
xor    %esi,%esi
mov    $0x120,%edx
call   1060 <memset@plt>
lea    0x60(%rsp),%rdi
mov    %rbx,%rsi
call   1050 <kvss905c_window_serialise@plt>
cmp    $0x40,%eax
jne    1265 <kvss905c_set_windows+0x105>
mov    $0x40,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
lea    0x58(%rsp),%rdi
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1070 <memcpy@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
movq   $0x0,0x30(%rsp)
movl   $0x24,0x10(%rsp)
movl   $0x1,0x2c(%rsp)
movl   $0x120,0x30(%rsp)
mov    0x2e02(%rip),%rbx
mov    (%rbx),%esi
lea    0x10(%rsp),%rdx
lea    0x40(%rsp),%r8
mov    %ebp,%edi
mov    $0x28,%ecx
mov    $0x120,%r9d
push   $0x0
push   %r15
call   1080 <scsi_command@plt>
add    $0x10,%rsp
test   %eax,%eax
jne    1257 <kvss905c_set_windows+0xf7>
test   %r14b,%r14b
je     1255 <kvss905c_set_windows+0xf5>
movl   $0x80,0x60(%rsp)
mov    (%rbx),%esi
lea    0x10(%rsp),%rdx
lea    0x40(%rsp),%r8
mov    %ebp,%edi
mov    $0x28,%ecx
mov    $0x120,%r9d
push   $0x0
push   %r15
call   1080 <scsi_command@plt>
add    $0x10,%rsp
jmp    1257 <kvss905c_set_windows+0xf7>
xor    %eax,%eax
add    $0x168,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.kvss905c_window = type { i32 }
@kvss905c_set_windows.transfer_length = internal constant i32 288
@SG_DXFER_TO_DEV = global i32 0
define i32 @kvss905c_set_windows(i32 %0, %struct.kvss905c_window* %1, i8 signext %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.kvss905c_window*
%8 = alloca i8
%9 = alloca i32*
%10 = alloca [72 x i32]
%11 = alloca i32
%12 = alloca i32
%13 = alloca [10 x i32]
%14 = alloca i32
store i32 %0, i32* %6
store %struct.kvss905c_window* %1, %struct.kvss905c_window** %7
store i8 %2, i8* %8
store i32* %3, i32** %9
%15 = getelementptr inbounds [72 x i32], [72 x i32]* %10, i64 0, i64 0
%16 = call i32 @memset(i32* %15, i32 0, i32 288)
%17 = getelementptr inbounds [72 x i32], [72 x i32]* %10, i64 0, i64 0
%18 = getelementptr inbounds i32, i32* %17, i64 8
%19 = load %struct.kvss905c_window*, %struct.kvss905c_window** %7
%20 = call i32 @kvss905c_window_serialise(i32* %18, %struct.kvss905c_window* %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp ne i32 %21, 64
br i1 %22, label %23, label %25
23: ; preds = %4
%24 = call i32 (...) @abort()
unreachable
25: ; preds = %4
%26 = call i32 @htons(i32 64)
store i32 %26, i32* %12
%27 = getelementptr inbounds [72 x i32], [72 x i32]* %10, i64 0, i64 0
%28 = getelementptr inbounds i32, i32* %27, i64 6
%29 = call i32 @memcpy(i32* %28, i32* %12, i32 4)
%30 = bitcast [10 x i32]* %13 to i8*
%31 = bitcast i8* %30 to [10 x i32]*
%32 = getelementptr inbounds [10 x i32], [10 x i32]* %31, i32 0, i32 0
store i32 36, i32* %32
%33 = getelementptr inbounds [10 x i32], [10 x i32]* %31, i32 0, i32 7
store i32 1, i32* %33
%34 = getelementptr inbounds [10 x i32], [10 x i32]* %31, i32 0, i32 8
store i32 288, i32* %34
%35 = load i32, i32* %6
%36 = load i32, i32* @SG_DXFER_TO_DEV
%37 = getelementptr inbounds [10 x i32], [10 x i32]* %13, i64 0, i64 0
%38 = getelementptr inbounds [72 x i32], [72 x i32]* %10, i64 0, i64 0
%39 = load i32*, i32** %9
%40 = call i32 @scsi_command(i32 %35, i32 %36, i32* %37, i32 40, i32* %38, i32 288, i32* %39, i32 0)
store i32 %40, i32* %14
%41 = load i32, i32* %14
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %25
%44 = load i32, i32* %14
store i32 %44, i32* %5
br label %57
45: ; preds = %25
%46 = load i8, i8* %8
%47 = icmp ne i8 %46, 0
br i1 %47, label %48, label %56
48: ; preds = %45
%49 = getelementptr inbounds [72 x i32], [72 x i32]* %10, i64 0, i64 8
store i32 128, i32* %49
%50 = load i32, i32* %6
%51 = load i32, i32* @SG_DXFER_TO_DEV
%52 = getelementptr inbounds [10 x i32], [10 x i32]* %13, i64 0, i64 0
%53 = getelementptr inbounds [72 x i32], [72 x i32]* %10, i64 0, i64 0
%54 = load i32*, i32** %9
%55 = call i32 @scsi_command(i32 %50, i32 %51, i32* %52, i32 40, i32* %53, i32 288, i32* %54, i32 0)
store i32 %55, i32* %5
br label %57
56: ; preds = %45
store i32 0, i32* %5
br label %57
57: ; preds = %56, %48, %43
%58 = load i32, i32* %5
ret i32 %58
}

LLVM IR (-O2):
%struct.kvss905c_window = type { i32 }
@SG_DXFER_TO_DEV = global i32 0
define i32 @kvss905c_set_windows(i32 %0, %struct.kvss905c_window* %1, i8 signext %2, i32* %3) {
%5 = alloca [72 x i32]
%6 = alloca i32
%7 = alloca [10 x i32]
%8 = bitcast [72 x i32]* %5 to i8*
%9 = getelementptr inbounds [72 x i32], [72 x i32]* %5, i64 0, i64 0
%10 = call i32 @memset(i32* %9, i32 0, i32 288)
%11 = getelementptr inbounds [72 x i32], [72 x i32]* %5, i64 0, i64 8
%12 = call i32 @kvss905c_window_serialise(i32* %11, %struct.kvss905c_window* %1)
%13 = icmp eq i32 %12, 64
br i1 %13, label %16, label %14
14: ; preds = %4
%15 = call i32 (...) @abort()
unreachable
16: ; preds = %4
%17 = bitcast i32* %6 to i8*
%18 = call i32 @htons(i32 64)
store i32 %18, i32* %6
%19 = getelementptr inbounds [72 x i32], [72 x i32]* %5, i64 0, i64 6
%20 = call i32 @memcpy(i32* %19, i32* %6, i32 4)
%21 = bitcast [10 x i32]* %7 to i8*
%22 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 0
store i32 36, i32* %22
%23 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 7
store i32 1, i32* %23
%24 = getelementptr inbounds [10 x i32], [10 x i32]* %7, i64 0, i64 8
store i32 288, i32* %24
%25 = load i32, i32* @SG_DXFER_TO_DEV
%26 = call i32 @scsi_command(i32 %0, i32 %25, i32* %22, i32 40, i32* %9, i32 288, i32* %3, i32 0)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %16
%29 = icmp eq i8 %2, 0
br i1 %29, label %33, label %30
30: ; preds = %28
store i32 128, i32* %11
%31 = load i32, i32* @SG_DXFER_TO_DEV
%32 = call i32 @scsi_command(i32 %0, i32 %31, i32* %22, i32 40, i32* %9, i32 288, i32* %3, i32 0)
br label %33
33: ; preds = %28, %16, %30
%34 = phi i32 [ %32, %30 ], [ %26, %16 ], [ 0, %28 ]
ret i32 %34
}