#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 SMBReadBlock(int, int, unsigned char *);
int SMBWriteBlock(int, int, unsigned char *, int);
int readProgramBlock(int blockNr, unsigned char *buf) {
  int status, i;
  unsigned char setAddr[3];
  setAddr[0] = blockNr & 0xFF;
  setAddr[1] = (blockNr >> 8) & 0xFF;
  setAddr[2] = (blockNr >> 16) & 0xFF;
  status = SMBWriteBlock(0x16, 0x0, setAddr, 3);
  if (status != 3)
    return -1;
  status = SMBReadBlock(0x16, 0x2, buf);
  return status;
}


x86 assembly (clang, -O0):
<readProgramBlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x1b(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x1a(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x19(%rbp)
lea    -0x1b(%rbp),%rdx
mov    $0x16,%edi
xor    %esi,%esi
mov    $0x3,%ecx
call   1030 <SMBWriteBlock@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
je     1184 <readProgramBlock+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <readProgramBlock+0x80>
mov    -0x10(%rbp),%rdx
mov    $0x16,%edi
mov    $0x2,%esi
call   1040 <SMBReadBlock@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readProgramBlock>:
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%eax
mov    %al,0xd(%rsp)
mov    %ah,0xe(%rsp)
shr    $0x10,%eax
mov    %al,0xf(%rsp)
lea    0xd(%rsp),%rdx
mov    $0x16,%edi
xor    %esi,%esi
mov    $0x3,%ecx
call   1030 <SMBWriteBlock@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0x3,%ecx
jne    116d <readProgramBlock+0x4d>
mov    $0x16,%edi
mov    $0x2,%esi
mov    %rbx,%rdx
call   1040 <SMBReadBlock@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @readProgramBlock(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [3 x i8]
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = and i32 %9, 255
%11 = trunc i32 %10 to i8
%12 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
store i8 %11, i8* %12
%13 = load i32, i32* %4
%14 = ashr i32 %13, 8
%15 = and i32 %14, 255
%16 = trunc i32 %15 to i8
%17 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 1
store i8 %16, i8* %17
%18 = load i32, i32* %4
%19 = ashr i32 %18, 16
%20 = and i32 %19, 255
%21 = trunc i32 %20 to i8
%22 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 2
store i8 %21, i8* %22
%23 = getelementptr inbounds [3 x i8], [3 x i8]* %8, i64 0, i64 0
%24 = call i32 @SMBWriteBlock(i32 22, i32 0, i8* %23, i32 3)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 3
br i1 %26, label %27, label %28
27: ; preds = %2
store i32 -1, i32* %3
br label %32
28: ; preds = %2
%29 = load i8*, i8** %5
%30 = call i32 @SMBReadBlock(i32 22, i32 2, i8* %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
store i32 %31, i32* %3
br label %32
32: ; preds = %28, %27
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
define i32 @readProgramBlock(i32 %0, i8* %1) {
%3 = alloca [3 x i8]
%4 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 0
%5 = trunc i32 %0 to i8
store i8 %5, i8* %4
%6 = lshr i32 %0, 8
%7 = trunc i32 %6 to i8
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 1
store i8 %7, i8* %8
%9 = lshr i32 %0, 16
%10 = trunc i32 %9 to i8
%11 = getelementptr inbounds [3 x i8], [3 x i8]* %3, i64 0, i64 2
store i8 %10, i8* %11
%12 = call i32 @SMBWriteBlock(i32 22, i32 0, i8* %4, i32 3)
%13 = icmp eq i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @SMBReadBlock(i32 22, i32 2, i8* %1)
br label %16
16: ; preds = %2, %14
%17 = phi i32 [ %15, %14 ], [ -1, %2 ]
ret i32 %17
}