#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 */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ hp5400_bulk_command_write(int, int, char *, int, int, int,
                                             void *);
char *malloc(int);
void WriteGammaCalibTable(int iHandle, const int *pabGammaR,
                          const int *pabGammaG, const int *pabGammaB) {
  char cmd[3];
  char *buffer;
  int i, j;
  buffer = malloc(2 * 65536);
  cmd[0] = 2;
  cmd[1] = 0;
  cmd[2] = 0;
  for (i = 0; i < 3; i++) {
    const int *ptr = (i == 0) ? pabGammaR : (i == 1) ? pabGammaG : pabGammaB;
    for (j = 0; j < 65536; j++) {
      buffer[2 * j] = ptr[j];
      buffer[2 * j + 1] = ptr[j] >> 8;
    }
    hp5400_bulk_command_write(iHandle, 0x2A01 + i, cmd, 3, 2 * 65536, 65536,
                              (void *)buffer);
  }
  free(buffer);
  return;
}


x86 assembly (clang, -O0):
<WriteGammaCalibTable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    $0x20000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
movb   $0x2,-0x23(%rbp)
movb   $0x0,-0x22(%rbp)
movb   $0x0,-0x21(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x3,-0x34(%rbp)
jge    1256 <WriteGammaCalibTable+0x126>
cmpl   $0x0,-0x34(%rbp)
jne    1189 <WriteGammaCalibTable+0x59>
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11b0 <WriteGammaCalibTable+0x80>
cmpl   $0x1,-0x34(%rbp)
jne    11a0 <WriteGammaCalibTable+0x70>
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    11a8 <WriteGammaCalibTable+0x78>
mov    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x10000,-0x38(%rbp)
jge    121a <WriteGammaCalibTable+0xea>
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
sar    $0x8,%eax
mov    %al,%dl
mov    -0x30(%rbp),%rax
mov    -0x38(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11bf <WriteGammaCalibTable+0x8f>
mov    -0x4(%rbp),%edi
mov    -0x34(%rbp),%esi
add    $0x2a01,%esi
lea    -0x23(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    $0x3,%ecx
mov    $0x20000,%r8d
mov    $0x10000,%r9d
mov    %rax,(%rsp)
call   1040 <hp5400_bulk_command_write@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1168 <WriteGammaCalibTable+0x38>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<WriteGammaCalibTable>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %edi,%r14d
mov    $0x20000,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
movw   $0x2,0xd(%rsp)
movb   $0x0,0xf(%rsp)
mov    $0x4,%eax
movdqa 0xe92(%rip),%xmm4
xchg   %ax,%ax
movdqu -0x10(%rbp,%rax,4),%xmm0
movdqu 0x0(%rbp,%rax,4),%xmm1
movdqa %xmm0,%xmm2
psrld  $0x8,%xmm2
movdqa %xmm1,%xmm3
psrld  $0x8,%xmm3
pand   %xmm4,%xmm0
packuswb %xmm0,%xmm0
packuswb %xmm0,%xmm0
pand   %xmm4,%xmm2
packuswb %xmm2,%xmm2
packuswb %xmm2,%xmm2
punpcklbw %xmm2,%xmm0
pand   %xmm4,%xmm1
packuswb %xmm1,%xmm1
packuswb %xmm1,%xmm1
pand   %xmm4,%xmm3
packuswb %xmm3,%xmm3
packuswb %xmm3,%xmm3
punpcklbw %xmm3,%xmm1
punpcklqdq %xmm1,%xmm0
movdqu %xmm0,-0x8(%rbx,%rax,2)
add    $0x8,%rax
cmp    $0x10004,%rax
jne    1170 <WriteGammaCalibTable+0x40>
mov    %rbx,(%rsp)
lea    0xd(%rsp),%rdx
mov    %r14d,%edi
mov    $0x2a01,%esi
mov    $0x3,%ecx
mov    $0x20000,%r8d
mov    $0x10000,%r9d
call   1040 <hp5400_bulk_command_write@plt>
movdqa 0xdf5(%rip),%xmm4
mov    $0x4,%eax
movdqu -0x10(%r12,%rax,4),%xmm0
movdqu (%r12,%rax,4),%xmm1
movdqa %xmm0,%xmm2
psrld  $0x8,%xmm2
movdqa %xmm1,%xmm3
psrld  $0x8,%xmm3
pand   %xmm4,%xmm0
packuswb %xmm0,%xmm0
packuswb %xmm0,%xmm0
pand   %xmm4,%xmm2
packuswb %xmm2,%xmm2
packuswb %xmm2,%xmm2
punpcklbw %xmm2,%xmm0
pand   %xmm4,%xmm1
packuswb %xmm1,%xmm1
packuswb %xmm1,%xmm1
pand   %xmm4,%xmm3
packuswb %xmm3,%xmm3
packuswb %xmm3,%xmm3
punpcklbw %xmm3,%xmm1
punpcklqdq %xmm1,%xmm0
movdqu %xmm0,-0x8(%rbx,%rax,2)
add    $0x8,%rax
cmp    $0x10004,%rax
jne    1210 <WriteGammaCalibTable+0xe0>
mov    %rbx,(%rsp)
lea    0xd(%rsp),%rdx
mov    %r14d,%edi
mov    $0x2a02,%esi
mov    $0x3,%ecx
mov    $0x20000,%r8d
mov    $0x10000,%r9d
call   1040 <hp5400_bulk_command_write@plt>
movdqa 0xd54(%rip),%xmm4
mov    $0x4,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movdqu -0x10(%r15,%rax,4),%xmm0
movdqu (%r15,%rax,4),%xmm1
movdqa %xmm0,%xmm2
psrld  $0x8,%xmm2
movdqa %xmm1,%xmm3
psrld  $0x8,%xmm3
pand   %xmm4,%xmm0
packuswb %xmm0,%xmm0
packuswb %xmm0,%xmm0
pand   %xmm4,%xmm2
packuswb %xmm2,%xmm2
packuswb %xmm2,%xmm2
punpcklbw %xmm2,%xmm0
pand   %xmm4,%xmm1
packuswb %xmm1,%xmm1
packuswb %xmm1,%xmm1
pand   %xmm4,%xmm3
packuswb %xmm3,%xmm3
packuswb %xmm3,%xmm3
punpcklbw %xmm3,%xmm1
punpcklqdq %xmm1,%xmm0
movdqu %xmm0,-0x8(%rbx,%rax,2)
add    $0x8,%rax
cmp    $0x10004,%rax
jne    12c0 <WriteGammaCalibTable+0x190>
mov    %rbx,(%rsp)
lea    0xd(%rsp),%rdx
mov    %r14d,%edi
mov    $0x2a03,%esi
mov    $0x3,%ecx
mov    $0x20000,%r8d
mov    $0x10000,%r9d
call   1040 <hp5400_bulk_command_write@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @WriteGammaCalibTable(i32 %0, i32* %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca [3 x i8]
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32*
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
%14 = call i8* @malloc(i32 131072)
store i8* %14, i8** %10
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
store i8 2, i8* %15
%16 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
store i8 0, i8* %16
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 2
store i8 0, i8* %17
store i32 0, i32* %11
br label %18
18: ; preds = %75, %4
%19 = load i32, i32* %11
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %78
21: ; preds = %18
%22 = load i32, i32* %11
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = load i32*, i32** %6
br label %35
26: ; preds = %21
%27 = load i32, i32* %11
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = load i32*, i32** %7
br label %33
31: ; preds = %26
%32 = load i32*, i32** %8
br label %33
33: ; preds = %31, %29
%34 = phi i32* [ %30, %29 ], [ %32, %31 ]
br label %35
35: ; preds = %33, %24
%36 = phi i32* [ %25, %24 ], [ %34, %33 ]
store i32* %36, i32** %13
store i32 0, i32* %12
br label %37
37: ; preds = %65, %35
%38 = load i32, i32* %12
%39 = icmp slt i32 %38, 65536
br i1 %39, label %40, label %68
40: ; preds = %37
%41 = load i32*, i32** %13
%42 = load i32, i32* %12
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %44
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %10
%48 = load i32, i32* %12
%49 = mul nsw i32 2, %48
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %47, i64 %50
store i8 %46, i8* %51
%52 = load i32*, i32** %13
%53 = load i32, i32* %12
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i32, i32* %52, i64 %54
%56 = load i32, i32* %55
%57 = ashr i32 %56, 8
%58 = trunc i32 %57 to i8
%59 = load i8*, i8** %10
%60 = load i32, i32* %12
%61 = mul nsw i32 2, %60
%62 = add nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %59, i64 %63
store i8 %58, i8* %64
br label %65
65: ; preds = %40
%66 = load i32, i32* %12
%67 = add nsw i32 %66, 1
store i32 %67, i32* %12
br label %37
68: ; preds = %37
%69 = load i32, i32* %5
%70 = load i32, i32* %11
%71 = add nsw i32 10753, %70
%72 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%73 = load i8*, i8** %10
%74 = call i32 @hp5400_bulk_command_write(i32 %69, i32 %71, i8* %72, i32 3, i32 131072, i32 65536, i8* %73)
br label %75
75: ; preds = %68
%76 = load i32, i32* %11
%77 = add nsw i32 %76, 1
store i32 %77, i32* %11
br label %18
78: ; preds = %18
%79 = load i8*, i8** %10
%80 = call i32 @free(i8* %79)
ret void
}

LLVM IR (-O2):
define void @WriteGammaCalibTable(i32 %0, i32* nocapture %1, i32* nocapture %2, i32* nocapture %3) {
%5 = alloca [3 x i8]
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 0
%7 = tail call dereferenceable_or_null(131072) i8* @malloc(i32 131072)
store i8 2, i8* %6
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 1
store i8 0, i8* %8
%9 = getelementptr inbounds [3 x i8], [3 x i8]* %5, i64 0, i64 2
store i8 0, i8* %9
%10 = getelementptr inbounds i8, i8* %7, i64 -1
%11 = getelementptr inbounds i8, i8* %7, i64 -1
br label %12
12: ; preds = %12, %4
%13 = phi i64 [ 0, %4 ], [ %36, %12 ]
%14 = getelementptr inbounds i32, i32* %1, i64 %13
%15 = bitcast i32* %14 to <4 x i32>*
%16 = load <4 x i32>, <4 x i32>* %15
%17 = getelementptr inbounds i32, i32* %14, i64 4
%18 = bitcast i32* %17 to <4 x i32>*
%19 = load <4 x i32>, <4 x i32>* %18
%20 = trunc <4 x i32> %16 to <4 x i8>
%21 = trunc <4 x i32> %19 to <4 x i8>
%22 = shl nuw nsw i64 %13, 1
%23 = shl i64 %13, 1
%24 = lshr <4 x i32> %16, <i32 8, i32 8, i32 8, i32 8>
%25 = lshr <4 x i32> %19, <i32 8, i32 8, i32 8, i32 8>
%26 = trunc <4 x i32> %24 to <4 x i8>
%27 = trunc <4 x i32> %25 to <4 x i8>
%28 = or i64 %22, 1
%29 = or i64 %23, 9
%30 = getelementptr inbounds i8, i8* %10, i64 %28
%31 = bitcast i8* %30 to <8 x i8>*
%32 = getelementptr inbounds i8, i8* %11, i64 %29
%33 = bitcast i8* %32 to <8 x i8>*
%34 = shufflevector <4 x i8> %20, <4 x i8> %26, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
store <8 x i8> %34, <8 x i8>* %31
%35 = shufflevector <4 x i8> %21, <4 x i8> %27, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
store <8 x i8> %35, <8 x i8>* %33
%36 = add nuw i64 %13, 8
%37 = icmp eq i64 %36, 65536
br i1 %37, label %38, label %12
38: ; preds = %12
%39 = call i32 @hp5400_bulk_command_write(i32 %0, i32 10753, i8* %6, i32 3, i32 131072, i32 65536, i8* %7)
%40 = getelementptr inbounds i8, i8* %7, i64 -1
%41 = getelementptr inbounds i8, i8* %7, i64 -1
br label %42
42: ; preds = %42, %38
%43 = phi i64 [ 0, %38 ], [ %66, %42 ]
%44 = getelementptr inbounds i32, i32* %2, i64 %43
%45 = bitcast i32* %44 to <4 x i32>*
%46 = load <4 x i32>, <4 x i32>* %45
%47 = getelementptr inbounds i32, i32* %44, i64 4
%48 = bitcast i32* %47 to <4 x i32>*
%49 = load <4 x i32>, <4 x i32>* %48
%50 = trunc <4 x i32> %46 to <4 x i8>
%51 = trunc <4 x i32> %49 to <4 x i8>
%52 = shl nuw nsw i64 %43, 1
%53 = shl i64 %43, 1
%54 = lshr <4 x i32> %46, <i32 8, i32 8, i32 8, i32 8>
%55 = lshr <4 x i32> %49, <i32 8, i32 8, i32 8, i32 8>
%56 = trunc <4 x i32> %54 to <4 x i8>
%57 = trunc <4 x i32> %55 to <4 x i8>
%58 = or i64 %52, 1
%59 = or i64 %53, 9
%60 = getelementptr inbounds i8, i8* %40, i64 %58
%61 = bitcast i8* %60 to <8 x i8>*
%62 = getelementptr inbounds i8, i8* %41, i64 %59
%63 = bitcast i8* %62 to <8 x i8>*
%64 = shufflevector <4 x i8> %50, <4 x i8> %56, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
store <8 x i8> %64, <8 x i8>* %61
%65 = shufflevector <4 x i8> %51, <4 x i8> %57, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
store <8 x i8> %65, <8 x i8>* %63
%66 = add nuw i64 %43, 8
%67 = icmp eq i64 %66, 65536
br i1 %67, label %68, label %42
68: ; preds = %42
%69 = call i32 @hp5400_bulk_command_write(i32 %0, i32 10754, i8* %6, i32 3, i32 131072, i32 65536, i8* %7)
%70 = getelementptr inbounds i8, i8* %7, i64 -1
%71 = getelementptr inbounds i8, i8* %7, i64 -1
br label %72
72: ; preds = %72, %68
%73 = phi i64 [ 0, %68 ], [ %96, %72 ]
%74 = getelementptr inbounds i32, i32* %3, i64 %73
%75 = bitcast i32* %74 to <4 x i32>*
%76 = load <4 x i32>, <4 x i32>* %75
%77 = getelementptr inbounds i32, i32* %74, i64 4
%78 = bitcast i32* %77 to <4 x i32>*
%79 = load <4 x i32>, <4 x i32>* %78
%80 = trunc <4 x i32> %76 to <4 x i8>
%81 = trunc <4 x i32> %79 to <4 x i8>
%82 = shl nuw nsw i64 %73, 1
%83 = shl i64 %73, 1
%84 = lshr <4 x i32> %76, <i32 8, i32 8, i32 8, i32 8>
%85 = lshr <4 x i32> %79, <i32 8, i32 8, i32 8, i32 8>
%86 = trunc <4 x i32> %84 to <4 x i8>
%87 = trunc <4 x i32> %85 to <4 x i8>
%88 = or i64 %82, 1
%89 = or i64 %83, 9
%90 = getelementptr inbounds i8, i8* %70, i64 %88
%91 = bitcast i8* %90 to <8 x i8>*
%92 = getelementptr inbounds i8, i8* %71, i64 %89
%93 = bitcast i8* %92 to <8 x i8>*
%94 = shufflevector <4 x i8> %80, <4 x i8> %86, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
store <8 x i8> %94, <8 x i8>* %91
%95 = shufflevector <4 x i8> %81, <4 x i8> %87, <8 x i32> <i32 0, i32 4, i32 1, i32 5, i32 2, i32 6, i32 3, i32 7>
store <8 x i8> %95, <8 x i8>* %93
%96 = add nuw i64 %73, 8
%97 = icmp eq i64 %96, 65536
br i1 %97, label %98, label %72
98: ; preds = %72
%99 = call i32 @hp5400_bulk_command_write(i32 %0, i32 10755, i8* %6, i32 3, i32 131072, i32 65536, i8* %7)
%100 = call i32 @free(i8* %7)
ret void
}