#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 */
scalar_t__ block_read(int, char *);
scalar_t__ block_write(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memcpy(char *, short *, int);
int /*<<< orphan*/ stderr;
int set_block_ptr(int block, short ptr) {
  char buffer[4096];
  if (block_read(block, buffer)) {
    fprintf(stderr, "set_block_ptr: Error reading block %d.\n", block);
    return -1;
  }
  memcpy(buffer, &ptr, 2);
  if (block_write(block, buffer)) {
    fprintf(stderr, "set_block_ptr: Error writing block %d.\n", block);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_block_ptr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
mov    %si,%ax
mov    %edi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
call   1060 <block_read@plt>
cmp    $0x0,%rax
je     1192 <set_block_ptr+0x52>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe7f(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <set_block_ptr+0xab>
lea    -0x1010(%rbp),%rdi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%edi
lea    -0x1010(%rbp),%rsi
call   1050 <block_write@plt>
cmp    $0x0,%rax
je     11e4 <set_block_ptr+0xa4>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe55(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <set_block_ptr+0xab>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_block_ptr>:
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%ebx
mov    %si,0xe(%rsp)
lea    0x10(%rsp),%rsi
call   1060 <block_read@plt>
test   %rax,%rax
je     1169 <set_block_ptr+0x29>
lea    0xe99(%rip),%rsi
jmp    1196 <set_block_ptr+0x56>
lea    0x10(%rsp),%r14
lea    0xe(%rsp),%rsi
mov    %r14,%rdi
mov    $0x2,%edx
call   1040 <memcpy@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1050 <block_write@plt>
test   %rax,%rax
je     11b6 <set_block_ptr+0x76>
lea    0xe92(%rip),%rsi
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    %ebx,%edx
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
ret
xor    %eax,%eax
jmp    11ab <set_block_ptr+0x6b>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"set_block_ptr: Error reading block %d.\0A\00"
@.str.1 = [40 x i8] c"set_block_ptr: Error writing block %d.\0A\00"
define i32 @set_block_ptr(i32 %0, i16 signext %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i16
%6 = alloca [4096 x i8]
store i32 %0, i32* %4
store i16 %1, i16* %5
%7 = load i32, i32* %4
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%9 = call i64 @block_read(i32 %7, i8* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* %4
%14 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 -1, i32* %3
br label %27
15: ; preds = %2
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%17 = call i32 @memcpy(i8* %16, i16* %5, i32 2)
%18 = load i32, i32* %4
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%20 = call i64 @block_write(i32 %18, i8* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i32, i32* %4
%25 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 -1, i32* %3
br label %27
26: ; preds = %15
store i32 0, i32* %3
br label %27
27: ; preds = %26, %22, %11
%28 = load i32, i32* %3
ret i32 %28
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"set_block_ptr: Error reading block %d.\0A\00"
@.str.1 = [40 x i8] c"set_block_ptr: Error writing block %d.\0A\00"
define i32 @set_block_ptr(i32 %0, i16 signext %1) {
%3 = alloca i16
%4 = alloca [4096 x i8]
store i16 %1, i16* %3
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = call i64 @block_read(i32 %0, i8* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = call i32 @memcpy(i8* %5, i16* %3, i32 2)
%10 = call i64 @block_write(i32 %0, i8* %5)
%11 = icmp eq i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %8, %2
%13 = phi i8* [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), %8 ]
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* %13, i32 %0)
br label %16
16: ; preds = %12, %8
%17 = phi i32 [ 0, %8 ], [ -1, %12 ]
ret i32 %17
}