#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 uint8_t;
typedef int /*<<< orphan*/ resp;
typedef int /*<<< orphan*/ UP3D_CMD_6C_X_Y;

/* Variables and functions */
int UP3DCOMM_Read(int *, int);
int UP3DCOMM_Write(int *, int);
bool UP3D_UseSDProgramBuf(uint8_t progID, bool enableWrite) {
  if (progID > 9)
    return false;
  uint8_t UP3D_CMD_6C_X_Y[3] = {0x6C, progID, enableWrite ? 0x01 : 0x00};
  uint8_t resp[2048];
  if ((UP3DCOMM_Write(UP3D_CMD_6C_X_Y, sizeof(UP3D_CMD_6C_X_Y)) !=
       sizeof(UP3D_CMD_6C_X_Y)) ||
      (1 != UP3DCOMM_Read(resp, sizeof(resp))) || (6 != resp[0]))
    return false;
  return true;
}


x86 assembly (clang, -O0):
<UP3D_UseSDProgramBuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x9,-0x8(%rbp)
jle    1147 <UP3D_UseSDProgramBuf+0x27>
movl   $0x0,-0x4(%rbp)
jmp    11c5 <UP3D_UseSDProgramBuf+0xa5>
movl   $0x6c,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   1030 <UP3DCOMM_Write@plt>
cltq
cmp    $0xc,%rax
jne    11b2 <UP3D_UseSDProgramBuf+0x92>
lea    -0x2020(%rbp),%rdi
mov    $0x2000,%esi
call   1040 <UP3DCOMM_Read@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    11b2 <UP3D_UseSDProgramBuf+0x92>
mov    $0x6,%eax
cmp    -0x2020(%rbp),%eax
je     11be <UP3D_UseSDProgramBuf+0x9e>
movl   $0x0,-0x4(%rbp)
jmp    11c5 <UP3D_UseSDProgramBuf+0xa5>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UP3D_UseSDProgramBuf>:
xor    %eax,%eax
cmp    $0x9,%edi
jg     1186 <UP3D_UseSDProgramBuf+0x66>
sub    $0x2018,%rsp
movl   $0x6c,0x4(%rsp)
mov    %edi,0x8(%rsp)
xor    %eax,%eax
test   %esi,%esi
setne  %al
mov    %eax,0xc(%rsp)
lea    0x4(%rsp),%rdi
mov    $0xc,%esi
call   1030 <UP3DCOMM_Write@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xc,%ecx
jne    117f <UP3D_UseSDProgramBuf+0x5f>
lea    0x10(%rsp),%rdi
mov    $0x2000,%esi
call   1040 <UP3DCOMM_Read@plt>
mov    %eax,%ecx
xor    $0x1,%ecx
mov    0x10(%rsp),%edx
xor    $0x6,%edx
xor    %eax,%eax
or     %ecx,%edx
sete   %al
add    $0x2018,%rsp
ret

LLVM IR (-O0):
define i32 @UP3D_UseSDProgramBuf(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [3 x i32]
%7 = alloca [2048 x i32]
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 9
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 0, i32* %3
br label %34
11: ; preds = %2
%12 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
store i32 108, i32* %12
%13 = getelementptr inbounds i32, i32* %12, i64 1
%14 = load i32, i32* %4
store i32 %14, i32* %13
%15 = getelementptr inbounds i32, i32* %13, i64 1
%16 = load i32, i32* %5
%17 = icmp ne i32 %16, 0
%18 = zext i1 %17 to i64
%19 = select i1 %17, i32 1, i32 0
store i32 %19, i32* %15
%20 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
%21 = call i32 @UP3DCOMM_Write(i32* %20, i32 12)
%22 = sext i32 %21 to i64
%23 = icmp ne i64 %22, 12
br i1 %23, label %32, label %24
24: ; preds = %11
%25 = getelementptr inbounds [2048 x i32], [2048 x i32]* %7, i64 0, i64 0
%26 = call i32 @UP3DCOMM_Read(i32* %25, i32 8192)
%27 = icmp ne i32 1, %26
br i1 %27, label %32, label %28
28: ; preds = %24
%29 = getelementptr inbounds [2048 x i32], [2048 x i32]* %7, i64 0, i64 0
%30 = load i32, i32* %29
%31 = icmp ne i32 6, %30
br i1 %31, label %32, label %33
32: ; preds = %28, %24, %11
store i32 0, i32* %3
br label %34
33: ; preds = %28
store i32 1, i32* %3
br label %34
34: ; preds = %33, %32, %10
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @UP3D_UseSDProgramBuf(i32 %0, i32 %1) {
%3 = alloca [3 x i32]
%4 = alloca [2048 x i32]
%5 = icmp sgt i32 %0, 9
br i1 %5, label %26, label %6
6: ; preds = %2
%7 = bitcast [3 x i32]* %3 to i8*
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
store i32 108, i32* %8
%9 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
store i32 %0, i32* %9
%10 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
%11 = icmp ne i32 %1, 0
%12 = zext i1 %11 to i32
store i32 %12, i32* %10
%13 = bitcast [2048 x i32]* %4 to i8*
%14 = call i32 @UP3DCOMM_Write(i32* %8, i32 12)
%15 = icmp eq i32 %14, 12
br i1 %15, label %16, label %24
16: ; preds = %6
%17 = getelementptr inbounds [2048 x i32], [2048 x i32]* %4, i64 0, i64 0
%18 = call i32 @UP3DCOMM_Read(i32* %17, i32 8192)
%19 = icmp eq i32 %18, 1
%20 = load i32, i32* %17
%21 = icmp eq i32 %20, 6
%22 = select i1 %19, i1 %21, i1 false
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %16, %6
%25 = phi i32 [ 0, %6 ], [ %23, %16 ]
br label %26
26: ; preds = %2, %24
%27 = phi i32 [ %25, %24 ], [ 0, %2 ]
ret i32 %27
}