#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*/ block_write(int, char *);
int get_first_empty_block_from(int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int write_starting_from_block(int last, char *buffer, int *n_byte) {
  int next, nbyte = *n_byte;
  char *buff = (char *)malloc(4096);
  int i = 0;
  while (nbyte > 4096 - 4) {
    next = get_first_empty_block_from(last + 1);
    memcpy(buff, buffer + (i * (4096 - 4)), 4096 - 4);
    memcpy(buff + ((i + 1) * 4096 - 4), &next, 4);
    block_write(last, buff);
    last = next;
    nbyte -= (4096 - 4);
    i++;
  }
  next = -1;
  memset(buff, 0, 4096);
  memcpy(buff, buffer + (i * (4096 - 4)), nbyte);
  memcpy(buff + ((i + 1) * 4096 - 4), &next, 4);
  block_write(last, buff);
  return last;
}


x86 assembly (clang, -O0):
<write_starting_from_block>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x1000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0xffc,-0x20(%rbp)
jle    120c <write_starting_from_block+0xbc>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1040 <get_first_empty_block_from@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
imul   $0xffc,-0x2c(%rbp),%eax
cltq
add    %rax,%rsi
xor    %eax,%eax
mov    $0xffc,%edx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
shl    $0xc,%eax
sub    $0x4,%eax
cltq
add    %rax,%rdi
xor    %eax,%eax
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1070 <block_write@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%eax
sub    $0xffc,%eax
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1181 <write_starting_from_block+0x31>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1030 <memset@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
imul   $0xffc,-0x2c(%rbp),%eax
cltq
add    %rax,%rsi
mov    -0x20(%rbp),%edx
xor    %eax,%eax
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
shl    $0xc,%eax
sub    $0x4,%eax
cltq
add    %rax,%rdi
xor    %eax,%eax
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1050 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1070 <block_write@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_starting_from_block>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%r13d
mov    (%rdx),%r12d
mov    $0x1000,%edi
call   1060 <malloc@plt>
xor    %r15d,%r15d
cmp    $0xffd,%r12d
mov    %rbx,0x10(%rsp)
mov    %rax,0x8(%rsp)
jl     1204 <write_starting_from_block+0xb4>
mov    $0xffc,%r14d
xor    %r15d,%r15d
lea    0x1(%r13),%edi
call   1040 <get_first_empty_block_from@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0xffc,%edx
xor    %eax,%eax
call   1050 <memcpy@plt>
movslq %r14d,%r14
lea    (%r14,%rbp,1),%rdi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <memcpy@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
call   1070 <block_write@plt>
mov    0x4(%rsp),%r13d
lea    -0xffc(%r12),%ebp
ff
add    $0xffc,%rbx
add    $0x1,%r15d
add    $0x1000,%r14d
cmp    $0x1ff8,%r12d
mov    %ebp,%r12d
ja     1190 <write_starting_from_block+0x40>
jmp    1207 <write_starting_from_block+0xb7>
mov    %r12d,%ebp
movl   $0xffffffff,0x4(%rsp)
ff
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1030 <memset@plt>
imul   $0xffc,%r15d,%eax
mov    0x10(%rsp),%rsi
add    %rax,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <memcpy@plt>
shl    $0xc,%r15d
or     $0xffc,%r15d
movslq %r15d,%rax
mov    %rbx,%rdi
add    %rax,%rdi
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1050 <memcpy@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1070 <block_write@plt>
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @write_starting_from_block(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%11 = load i32*, i32** %6
%12 = load i32, i32* %11
store i32 %12, i32* %8
%13 = call i64 @malloc(i32 4096)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %9
store i32 0, i32* %10
br label %15
15: ; preds = %18, %3
%16 = load i32, i32* %8
%17 = icmp sgt i32 %16, 4092
br i1 %17, label %18, label %45
18: ; preds = %15
%19 = load i32, i32* %4
%20 = add nsw i32 %19, 1
%21 = call i32 @get_first_empty_block_from(i32 %20)
store i32 %21, i32* %7
%22 = load i8*, i8** %9
%23 = load i8*, i8** %5
%24 = load i32, i32* %10
%25 = mul nsw i32 %24, 4092
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = call i32 (i8*, ...) @memcpy(i8* %22, i8* %27, i32 4092)
%29 = load i8*, i8** %9
%30 = load i32, i32* %10
%31 = add nsw i32 %30, 1
%32 = mul nsw i32 %31, 4096
%33 = sub nsw i32 %32, 4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %29, i64 %34
%36 = call i32 (i8*, ...) @memcpy(i8* %35, i32* %7, i32 4)
%37 = load i32, i32* %4
%38 = load i8*, i8** %9
%39 = call i32 @block_write(i32 %37, i8* %38)
%40 = load i32, i32* %7
store i32 %40, i32* %4
%41 = load i32, i32* %8
%42 = sub nsw i32 %41, 4092
store i32 %42, i32* %8
%43 = load i32, i32* %10
%44 = add nsw i32 %43, 1
store i32 %44, i32* %10
br label %15
45: ; preds = %15
store i32 -1, i32* %7
%46 = load i8*, i8** %9
%47 = call i32 @memset(i8* %46, i32 0, i32 4096)
%48 = load i8*, i8** %9
%49 = load i8*, i8** %5
%50 = load i32, i32* %10
%51 = mul nsw i32 %50, 4092
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %49, i64 %52
%54 = load i32, i32* %8
%55 = call i32 (i8*, ...) @memcpy(i8* %48, i8* %53, i32 %54)
%56 = load i8*, i8** %9
%57 = load i32, i32* %10
%58 = add nsw i32 %57, 1
%59 = mul nsw i32 %58, 4096
%60 = sub nsw i32 %59, 4
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %56, i64 %61
%63 = call i32 (i8*, ...) @memcpy(i8* %62, i32* %7, i32 4)
%64 = load i32, i32* %4
%65 = load i8*, i8** %9
%66 = call i32 @block_write(i32 %64, i8* %65)
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
define i32 @write_starting_from_block(i32 %0, i8* %1, i32* nocapture %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = load i32, i32* %2
%7 = tail call i64 @malloc(i32 4096)
%8 = inttoptr i64 %7 to i8*
%9 = icmp sgt i32 %6, 4092
br i1 %9, label %10, label %32
10: ; preds = %3, %10
%11 = phi i64 [ %19, %10 ], [ 0, %3 ]
%12 = phi i32 [ %27, %10 ], [ %0, %3 ]
%13 = phi i32 [ %28, %10 ], [ %6, %3 ]
%14 = add nsw i32 %12, 1
%15 = call i32 @get_first_empty_block_from(i32 %14)
store i32 %15, i32* %4
%16 = mul nuw nsw i64 %11, 4092
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = call i32 (i8*, ...) @memcpy(i8* %8, i8* %17, i32 4092)
%19 = add nuw nsw i64 %11, 1
%20 = trunc i64 %19 to i32
%21 = shl i32 %20, 12
%22 = add i32 %21, -4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %8, i64 %23
%25 = call i32 (i8*, ...) @memcpy(i8* %24, i32* %4, i32 4)
%26 = call i32 @block_write(i32 %12, i8* %8)
%27 = load i32, i32* %4
%28 = add nsw i32 %13, -4092
%29 = icmp ugt i32 %13, 8184
br i1 %29, label %10, label %30
30: ; preds = %10
%31 = trunc i64 %19 to i32
br label %32
32: ; preds = %30, %3
%33 = phi i32 [ %6, %3 ], [ %28, %30 ]
%34 = phi i32 [ %0, %3 ], [ %27, %30 ]
%35 = phi i32 [ 0, %3 ], [ %31, %30 ]
store i32 -1, i32* %4
%36 = call i32 @memset(i8* %8, i32 0, i32 4096)
%37 = mul nsw i32 %35, 4092
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %1, i64 %38
%40 = call i32 (i8*, ...) @memcpy(i8* %8, i8* %39, i32 %33)
%41 = shl i32 %35, 12
%42 = or i32 %41, 4092
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %8, i64 %43
%45 = call i32 (i8*, ...) @memcpy(i8* %44, i32* %4, i32 4)
%46 = call i32 @block_write(i32 %34, i8* %8)
ret i32 %34
}