#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 */
unsigned char cbm_pp_read(int);
int cbm_upload(int, unsigned char, unsigned int, unsigned char *, int);
int /*<<< orphan*/ pia_to_inputmode(int, unsigned char, unsigned int);
int /*<<< orphan*/ usleep(int);
int output_pia(int HandleDevice, unsigned char Drive, unsigned int PiaAddress,
               unsigned char Value) {
  int ret = -1;
  ;
  do {
    unsigned char byteval = 0xff;
    if (cbm_upload(HandleDevice, Drive, PiaAddress + 2, &byteval, 1) != 1)
      break;
    if (cbm_upload(HandleDevice, Drive, PiaAddress, &Value, 1) != 1)
      break;
    usleep(10000);
    ret = cbm_pp_read(HandleDevice) != Value;
    if (ret) {
      pia_to_inputmode(HandleDevice, Drive, PiaAddress);
    }
  } while (0);
  return ret;
}


x86 assembly (clang, -O0):
<output_pia>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %sil,%cl
mov    %edi,-0x4(%rbp)
mov    %cl,-0x5(%rbp)
mov    %edx,-0xc(%rbp)
mov    %al,-0xd(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movb   $0xff,-0x15(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x5(%rbp),%al
mov    -0xc(%rbp),%edx
add    $0x2,%edx
lea    -0x15(%rbp),%rcx
mov    $0x1,%r8d
movzbl %al,%esi
call   1040 <cbm_upload@plt>
cmp    $0x1,%eax
je     1190 <output_pia+0x50>
jmp    11ff <output_pia+0xbf>
mov    -0x4(%rbp),%edi
mov    -0x5(%rbp),%al
mov    -0xc(%rbp),%edx
lea    -0xd(%rbp),%rcx
mov    $0x1,%r8d
movzbl %al,%esi
call   1040 <cbm_upload@plt>
cmp    $0x1,%eax
je     11b9 <output_pia+0x79>
jmp    11ff <output_pia+0xbf>
mov    $0x2710,%edi
call   1060 <usleep@plt>
mov    -0x4(%rbp),%edi
call   1050 <cbm_pp_read@plt>
movzbl %al,%eax
movzbl -0xd(%rbp),%ecx
cmp    %ecx,%eax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11fa <output_pia+0xba>
mov    -0x4(%rbp),%edi
mov    -0x5(%rbp),%al
mov    -0xc(%rbp),%edx
movzbl %al,%esi
call   1030 <pia_to_inputmode@plt>
jmp    11ff <output_pia+0xbf>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<output_pia>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %esi,%ebp
mov    %edi,%r12d
mov    %cl,0xe(%rsp)
movb   $0xff,0xf(%rsp)
lea    0x2(%r15),%edx
lea    0xf(%rsp),%rcx
mov    $0x1,%r8d
call   1040 <cbm_upload@plt>
mov    $0xffffffff,%ebx
cmp    $0x1,%eax
jne    11ca <output_pia+0x8a>
movzbl %bpl,%r14d
lea    0xe(%rsp),%rcx
mov    %r12d,%edi
mov    %r14d,%esi
mov    %r15d,%edx
mov    $0x1,%r8d
call   1040 <cbm_upload@plt>
cmp    $0x1,%eax
jne    11ca <output_pia+0x8a>
mov    $0x2710,%edi
call   1060 <usleep@plt>
mov    %r12d,%edi
call   1050 <cbm_pp_read@plt>
xor    %ebx,%ebx
cmp    0xe(%rsp),%al
je     11ca <output_pia+0x8a>
mov    %r12d,%edi
mov    %r14d,%esi
mov    %r15d,%edx
call   1030 <pia_to_inputmode@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @output_pia(i32 %0, i8 zeroext %1, i32 %2, i8 zeroext %3) {
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
%8 = alloca i8
%9 = alloca i32
%10 = alloca i8
store i32 %0, i32* %5
store i8 %1, i8* %6
store i32 %2, i32* %7
store i8 %3, i8* %8
store i32 -1, i32* %9
br label %11
11: ; preds = %4
store i8 -1, i8* %10
%12 = load i32, i32* %5
%13 = load i8, i8* %6
%14 = load i32, i32* %7
%15 = add i32 %14, 2
%16 = call i32 @cbm_upload(i32 %12, i8 zeroext %13, i32 %15, i8* %10, i32 1)
%17 = icmp ne i32 %16, 1
br i1 %17, label %18, label %19
18: ; preds = %11
br label %43
19: ; preds = %11
%20 = load i32, i32* %5
%21 = load i8, i8* %6
%22 = load i32, i32* %7
%23 = call i32 @cbm_upload(i32 %20, i8 zeroext %21, i32 %22, i8* %8, i32 1)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %26
25: ; preds = %19
br label %43
26: ; preds = %19
%27 = call i32 @usleep(i32 10000)
%28 = load i32, i32* %5
%29 = call zeroext i8 @cbm_pp_read(i32 %28)
%30 = zext i8 %29 to i32
%31 = load i8, i8* %8
%32 = zext i8 %31 to i32
%33 = icmp ne i32 %30, %32
%34 = zext i1 %33 to i32
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %26
%38 = load i32, i32* %5
%39 = load i8, i8* %6
%40 = load i32, i32* %7
%41 = call i32 @pia_to_inputmode(i32 %38, i8 zeroext %39, i32 %40)
br label %42
42: ; preds = %37, %26
br label %43
43: ; preds = %42, %25, %18
%44 = load i32, i32* %9
ret i32 %44
}

LLVM IR (-O2):
define i32 @output_pia(i32 %0, i8 zeroext %1, i32 %2, i8 zeroext %3) {
%5 = alloca i8
%6 = alloca i8
store i8 %3, i8* %5
store i8 -1, i8* %6
%7 = add i32 %2, 2
%8 = call i32 @cbm_upload(i32 %0, i8 zeroext %1, i32 %7, i8* %6, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %20
10: ; preds = %4
%11 = call i32 @cbm_upload(i32 %0, i8 zeroext %1, i32 %2, i8* %5, i32 1)
%12 = icmp eq i32 %11, 1
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = call i32 @usleep(i32 10000)
%15 = call zeroext i8 @cbm_pp_read(i32 %0)
%16 = load i8, i8* %5
%17 = icmp eq i8 %15, %16
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = call i32 @pia_to_inputmode(i32 %0, i8 zeroext %1, i32 %2)
br label %20
20: ; preds = %13, %18, %10, %4
%21 = phi i32 [ -1, %4 ], [ -1, %10 ], [ 1, %18 ], [ 0, %13 ]
ret i32 %21
}