#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_4C_X;

/* Variables and functions */
int UP3DCOMM_Read(int *, int);
int UP3DCOMM_Write(int *, int);
bool UP3D_InsertRomProgram(uint8_t prog) {
  uint8_t UP3D_CMD_4C_X[] = {0x4C, '0' + prog};
  uint8_t resp[2048];
  if ((UP3DCOMM_Write(UP3D_CMD_4C_X, sizeof(UP3D_CMD_4C_X)) !=
       sizeof(UP3D_CMD_4C_X)) ||
      (5 != UP3DCOMM_Read(resp, sizeof(resp))) || (6 != resp[4]) ||
      (1 != resp[0]))
    return false;
  return true;
}


x86 assembly (clang, -O0):
<UP3D_InsertRomProgram>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2010,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x4c,-0x10(%rbp)
mov    -0x8(%rbp),%eax
add    $0x30,%eax
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1030 <UP3DCOMM_Write@plt>
cltq
cmp    $0x8,%rax
jne    119a <UP3D_InsertRomProgram+0x7a>
lea    -0x2010(%rbp),%rdi
mov    $0x2000,%esi
call   1040 <UP3DCOMM_Read@plt>
mov    %eax,%ecx
mov    $0x5,%eax
cmp    %ecx,%eax
jne    119a <UP3D_InsertRomProgram+0x7a>
mov    $0x6,%eax
cmp    -0x2000(%rbp),%eax
jne    119a <UP3D_InsertRomProgram+0x7a>
mov    $0x1,%eax
cmp    -0x2010(%rbp),%eax
je     11a6 <UP3D_InsertRomProgram+0x86>
movl   $0x0,-0x4(%rbp)
jmp    11ad <UP3D_InsertRomProgram+0x8d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<UP3D_InsertRomProgram>:
sub    $0x2018,%rsp
movl   $0x4c,0x8(%rsp)
add    $0x30,%edi
mov    %edi,0xc(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
call   1030 <UP3DCOMM_Write@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x8,%ecx
jne    1177 <UP3D_InsertRomProgram+0x57>
lea    0x10(%rsp),%rdi
mov    $0x2000,%esi
call   1040 <UP3DCOMM_Read@plt>
xor    $0x5,%eax
mov    0x10(%rsp),%ecx
mov    0x20(%rsp),%edx
xor    $0x6,%edx
or     %eax,%edx
xor    $0x1,%ecx
xor    %eax,%eax
or     %edx,%ecx
sete   %al
add    $0x2018,%rsp
ret

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

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