#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
void *malloc(int);
int read(int, void *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int write(int, void *, int);
int writeFBData(int output_fd, int fb_fd, int fb_bytes) {
  void *bytes = malloc(fb_bytes);
  int bytes_read = read(fb_fd, bytes, fb_bytes);
  if (bytes_read < fb_bytes) {
    fprintf(stderr, "External: Only read %d bytes from framebuffer!",
            bytes_read);
    fprintf(stdout, "\n");
    return 1;
  }
  fprintf(stdout, "External: read %u bytes from framebuffer.", bytes_read);
  fprintf(stdout, "\n");
  int bytes_written = write(output_fd, bytes, fb_bytes);
  if (bytes_written < fb_bytes) {
    fprintf(stderr, "External: Only wrote %d bytes!", bytes_written);
    fprintf(stdout, "\n");
    return 1;
  }
  fprintf(stdout, "External: wrote %u bytes.", bytes_written);
  fprintf(stdout, "\n");
  return 0;
}


x86 assembly (clang, -O0):
<writeFBData>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11b8 <writeFBData+0x78>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe8a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127c <writeFBData+0x13c>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1244 <writeFBData+0x104>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdfe(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127c <writeFBData+0x13c>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFBData>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %edx,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
mov    %ebx,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
cmp    %ebp,%eax
jge    117b <writeFBData+0x3b>
mov    0x2e70(%rip),%rcx
mov    (%rcx),%edi
lea    0xe87(%rip),%rsi
jmp    11c8 <writeFBData+0x88>
mov    0x2e56(%rip),%rbx
mov    (%rbx),%edi
lea    0xea6(%rip),%rsi
xor    %r15d,%r15d
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe8f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
cmp    %ebp,%eax
jge    11d9 <writeFBData+0x99>
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edi
lea    0xe93(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%r15d
jmp    11eb <writeFBData+0xab>
mov    (%rbx),%edi
lea    0xe98(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [47 x i8] c"External: Only read %d bytes from framebuffer!\00"
@stdout = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [42 x i8] c"External: read %u bytes from framebuffer.\00"
@.str.3 = [31 x i8] c"External: Only wrote %d bytes!\00"
@.str.4 = [26 x i8] c"External: wrote %u bytes.\00"
define i32 @writeFBData(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i32, i32* %7
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %8
%13 = load i32, i32* %6
%14 = load i8*, i8** %8
%15 = load i32, i32* %7
%16 = call i32 @read(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = load i32, i32* %7
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %26
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = load i32, i32* %9
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @stdout
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %51
26: ; preds = %3
%27 = load i32, i32* @stdout
%28 = load i32, i32* %9
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @stdout
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* %5
%33 = load i8*, i8** %8
%34 = load i32, i32* %7
%35 = call i32 @write(i32 %32, i8* %33, i32 %34)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = load i32, i32* %7
%38 = icmp slt i32 %36, %37
br i1 %38, label %39, label %45
39: ; preds = %26
%40 = load i32, i32* @stderr
%41 = load i32, i32* %10
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %41)
%43 = load i32, i32* @stdout
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %51
45: ; preds = %26
%46 = load i32, i32* @stdout
%47 = load i32, i32* %10
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %47)
%49 = load i32, i32* @stdout
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %51
51: ; preds = %45, %39, %20
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [47 x i8] c"External: Only read %d bytes from framebuffer!\00"
@stdout = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [42 x i8] c"External: read %u bytes from framebuffer.\00"
@.str.3 = [31 x i8] c"External: Only wrote %d bytes!\00"
@.str.4 = [26 x i8] c"External: wrote %u bytes.\00"
define i32 @writeFBData(i32 %0, i32 %1, i32 %2) {
%4 = tail call i8* @malloc(i32 %2)
%5 = tail call i32 @read(i32 %1, i8* %4, i32 %2)
%6 = icmp slt i32 %5, %2
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %5)
br label %23
10: ; preds = %3
%11 = load i32, i32* @stdout
%12 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%13 = load i32, i32* @stdout
%14 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @write(i32 %0, i8* %4, i32 %2)
%16 = icmp slt i32 %15, %2
br i1 %16, label %17, label %20
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %15)
br label %23
20: ; preds = %10
%21 = load i32, i32* @stdout
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %15)
br label %23
23: ; preds = %17, %20, %7
%24 = phi i32 [ 1, %7 ], [ 1, %17 ], [ 0, %20 ]
%25 = load i32, i32* @stdout
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
ret i32 %24
}