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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ free(unsigned char *);
int lseek(int, int, int /*<<< orphan*/);
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, unsigned int);
int write(int, unsigned char *, unsigned int);
int writeToSlave(int cpuf, int fd, off_t DestinationAddress,
                 unsigned int SourceAddress, unsigned int Size) {
  unsigned char *BUFFER = malloc(Size);
  int err;
  err = lseek(fd, SourceAddress, SEEK_SET);
  if (err < 0) {
    printf("error seeking SourceAddress\n");
    return 1;
  }
  err = read(fd, BUFFER, Size);
  if (err != Size) {
    printf("error read fd\n");
    return 1;
  }
  err = lseek(cpuf, DestinationAddress, SEEK_SET);
  printf("seeking to %x\n", DestinationAddress);
  if (err < 0) {
    printf("error seeking copo addi (addi = %x)\n", DestinationAddress);
    return 1;
  }
  err = write(cpuf, BUFFER, Size);
  if (err != Size) {
    printf("error write cpuf\n");
    return 1;
  }
  free(BUFFER);
  return 0;
}


x86 assembly (clang, -O0):
<writeToSlave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11bf <writeToSlave+0x5f>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128e <writeToSlave+0x12e>
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     11f7 <writeToSlave+0x97>
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128e <writeToSlave+0x12e>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
jge    1246 <writeToSlave+0xe6>
mov    -0x10(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128e <writeToSlave+0x12e>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     127e <writeToSlave+0x11e>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    128e <writeToSlave+0x12e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeToSlave>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r13d
mov    %ecx,%ebx
mov    %edx,0x4(%rsp)
mov    %esi,%ebp
mov    %edi,%r12d
mov    %r8d,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
mov    0x2e45(%rip),%r15
mov    (%r15),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   1070 <lseek@plt>
test   %eax,%eax
js     1203 <writeToSlave+0x93>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r13d,%edx
call   1080 <read@plt>
cmp    %r13d,%eax
jne    120c <writeToSlave+0x9c>
mov    (%r15),%edx
mov    %r12d,%edi
mov    0x4(%rsp),%ebp
mov    %ebp,%esi
call   1070 <lseek@plt>
mov    %eax,%ebx
lea    0xe29(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebx,%ebx
js     1215 <writeToSlave+0xa5>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r13d,%edx
call   1050 <write@plt>
cmp    %r13d,%eax
jne    1227 <writeToSlave+0xb7>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1238 <writeToSlave+0xc8>
lea    0xe49(%rip),%rdi
jmp    122e <writeToSlave+0xbe>
lea    0xe32(%rip),%rdi
jmp    122e <writeToSlave+0xbe>
lea    0xdf3(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1233 <writeToSlave+0xc3>
lea    0xe06(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@.str = [29 x i8] c"error seeking SourceAddress\0A\00"
@.str.1 = [15 x i8] c"error read fd\0A\00"
@.str.2 = [15 x i8] c"seeking to %x\0A\00"
@.str.3 = [37 x i8] c"error seeking copo addi (addi = %x)\0A\00"
@.str.4 = [18 x i8] c"error write cpuf\0A\00"
define i32 @writeToSlave(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = load i32, i32* %11
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %12
%16 = load i32, i32* %8
%17 = load i32, i32* %10
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @lseek(i32 %16, i32 %17, i32 %18)
store i32 %19, i32* %13
%20 = load i32, i32* %13
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %5
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %6
br label %59
24: ; preds = %5
%25 = load i32, i32* %8
%26 = load i8*, i8** %12
%27 = load i32, i32* %11
%28 = call i32 @read(i32 %25, i8* %26, i32 %27)
store i32 %28, i32* %13
%29 = load i32, i32* %13
%30 = load i32, i32* %11
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %6
br label %59
34: ; preds = %24
%35 = load i32, i32* %7
%36 = load i32, i32* %9
%37 = load i32, i32* @SEEK_SET
%38 = call i32 @lseek(i32 %35, i32 %36, i32 %37)
store i32 %38, i32* %13
%39 = load i32, i32* %9
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %13
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %34
%44 = load i32, i32* %9
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %44)
store i32 1, i32* %6
br label %59
46: ; preds = %34
%47 = load i32, i32* %7
%48 = load i8*, i8** %12
%49 = load i32, i32* %11
%50 = call i32 @write(i32 %47, i8* %48, i32 %49)
store i32 %50, i32* %13
%51 = load i32, i32* %13
%52 = load i32, i32* %11
%53 = icmp ne i32 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %46
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %6
br label %59
56: ; preds = %46
%57 = load i8*, i8** %12
%58 = call i32 @free(i8* %57)
store i32 0, i32* %6
br label %59
59: ; preds = %56, %54, %43, %32, %22
%60 = load i32, i32* %6
ret i32 %60
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@.str.2 = [15 x i8] c"seeking to %x\0A\00"
@.str.3 = [37 x i8] c"error seeking copo addi (addi = %x)\0A\00"
@str = [17 x i8] c"error write cpuf\00"
@str.5 = [14 x i8] c"error read fd\00"
@str.6 = [28 x i8] c"error seeking SourceAddress\00"
define i32 @writeToSlave(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i8* @malloc(i32 %4)
%7 = load i32, i32* @SEEK_SET
%8 = tail call i32 @lseek(i32 %1, i32 %3, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.6, i64 0, i64 0))
br label %31
12: ; preds = %5
%13 = tail call i32 @read(i32 %1, i8* %6, i32 %4)
%14 = icmp eq i32 %13, %4
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
br label %31
17: ; preds = %12
%18 = load i32, i32* @SEEK_SET
%19 = tail call i32 @lseek(i32 %0, i32 %2, i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %2)
%21 = icmp slt i32 %19, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %2)
br label %31
24: ; preds = %17
%25 = tail call i32 @write(i32 %0, i8* %6, i32 %4)
%26 = icmp eq i32 %25, %4
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %31
29: ; preds = %24
%30 = tail call i32 @free(i8* %6)
br label %31
31: ; preds = %29, %27, %22, %15, %10
%32 = phi i32 [ 1, %10 ], [ 1, %15 ], [ 1, %22 ], [ 1, %27 ], [ 0, %29 ]
ret i32 %32
}