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

/* Variables and functions */
size_t EACCES;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ error_printf(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t truncate_buffer(char **, size_t, size_t);
size_t write_to_buffer(const char *buffer, size_t size, off_t offset,
                       char **target, size_t *bufferlength) {
  ;
  ;
  if ((target == NULL) || (bufferlength == NULL)) {
    ;
    return -EACCES;
  }
  size_t writebuffersize = (*bufferlength);
  if (*target == NULL) {
    ;
    writebuffersize = offset + size + 1;
    *target = (char *)malloc(writebuffersize);
    memset(*target, 0, writebuffersize);
    writebuffersize--;
  } else if ((offset + size) > writebuffersize) {
    writebuffersize = offset + size + 1;
    size_t l = truncate_buffer(target, *bufferlength, writebuffersize);
    if (!l || l < writebuffersize) {
      error_printf("truncating buffer failed!\n");
      return -EACCES;
    }
    writebuffersize--;
  }
  assert(writebuffersize >= offset + size);
  char *dest = *target;
  memcpy(dest + offset, buffer, size);
  *bufferlength = writebuffersize;
  return size;
}


x86 assembly (clang, -O0):
<write_to_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1192 <write_to_buffer+0x32>
cmpq   $0x0,-0x30(%rbp)
jne    11a7 <write_to_buffer+0x47>
xor    %eax,%eax
mov    0x2e4d(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    12da <write_to_buffer+0x17a>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1206 <write_to_buffer+0xa6>
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    128b <write_to_buffer+0x12b>
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
cmp    -0x38(%rbp),%rax
jbe    1286 <write_to_buffer+0x126>
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x38(%rbp),%rdx
call   1080 <truncate_buffer@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1259 <write_to_buffer+0xf9>
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
jae    127a <write_to_buffer+0x11a>
lea    0xda0(%rip),%rdi
call   1060 <error_printf@plt>
xor    %eax,%eax
mov    0x2d7a(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    12da <write_to_buffer+0x17a>
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    128b <write_to_buffer+0x12b>
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    -0x18(%rbp),%rcx
cmp    %rcx,%rax
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
add    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %rcx,%rcx
je     11cb <write_to_buffer+0x6b>
mov    %r8,%r14
test   %r8,%r8
je     11cb <write_to_buffer+0x6b>
mov    %rcx,%r13
mov    %rdx,%r15
mov    %rsi,%rbx
lea    (%rdx,%rsi,1),%r12
cmpq   $0x0,(%rcx)
mov    %rdi,0x10(%rsp)
je     11d9 <write_to_buffer+0x79>
mov    (%r14),%rbp
cmp    %rbp,%r12
jbe    11fa <write_to_buffer+0x9a>
lea    0x1(%r12),%rdx
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %rdx,0x8(%rsp)
call   1080 <truncate_buffer@plt>
test   %rax,%rax
je     11bf <write_to_buffer+0x5f>
mov    %r12,%rbp
cmp    0x8(%rsp),%rax
jae    11fa <write_to_buffer+0x9a>
lea    0xe3a(%rip),%rdi
call   1060 <error_printf@plt>
mov    0x2e16(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%rbx
jmp    121f <write_to_buffer+0xbf>
lea    0x1(%r12),%rbp
mov    %rbp,%rdi
call   1070 <malloc@plt>
mov    %rax,0x0(%r13)
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1030 <memset@plt>
mov    %r12,%rbp
xor    %edi,%edi
cmp    %r12,%rbp
setae  %dil
call   1040 <assert@plt>
add    0x0(%r13),%r15
mov    %r15,%rdi
mov    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
mov    %rbp,(%r14)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EACCES = global i64 0
@.str = [27 x i8] c"truncating buffer failed!\0A\00"
define i64 @write_to_buffer(i8* %0, i64 %1, i64 %2, i8** %3, i64* %4) {
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8**
%11 = alloca i64*
%12 = alloca i64
%13 = alloca i64
%14 = alloca i8*
store i8* %0, i8** %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i8** %3, i8*** %10
store i64* %4, i64** %11
%15 = load i8**, i8*** %10
%16 = icmp eq i8** %15, null
br i1 %16, label %20, label %17
17: ; preds = %5
%18 = load i64*, i64** %11
%19 = icmp eq i64* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17, %5
%21 = load i64, i64* @EACCES
%22 = sub i64 0, %21
store i64 %22, i64* %6
br label %93
23: ; preds = %17
%24 = load i64*, i64** %11
%25 = load i64, i64* %24
store i64 %25, i64* %12
%26 = load i8**, i8*** %10
%27 = load i8*, i8** %26
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %44
29: ; preds = %23
%30 = load i64, i64* %9
%31 = load i64, i64* %8
%32 = add i64 %30, %31
%33 = add i64 %32, 1
store i64 %33, i64* %12
%34 = load i64, i64* %12
%35 = call i64 @malloc(i64 %34)
%36 = inttoptr i64 %35 to i8*
%37 = load i8**, i8*** %10
store i8* %36, i8** %37
%38 = load i8**, i8*** %10
%39 = load i8*, i8** %38
%40 = load i64, i64* %12
%41 = call i32 @memset(i8* %39, i32 0, i64 %40)
%42 = load i64, i64* %12
%43 = add i64 %42, -1
store i64 %43, i64* %12
br label %74
44: ; preds = %23
%45 = load i64, i64* %9
%46 = load i64, i64* %8
%47 = add i64 %45, %46
%48 = load i64, i64* %12
%49 = icmp ugt i64 %47, %48
br i1 %49, label %50, label %73
50: ; preds = %44
%51 = load i64, i64* %9
%52 = load i64, i64* %8
%53 = add i64 %51, %52
%54 = add i64 %53, 1
store i64 %54, i64* %12
%55 = load i8**, i8*** %10
%56 = load i64*, i64** %11
%57 = load i64, i64* %56
%58 = load i64, i64* %12
%59 = call i64 @truncate_buffer(i8** %55, i64 %57, i64 %58)
store i64 %59, i64* %13
%60 = load i64, i64* %13
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %50
%63 = load i64, i64* %13
%64 = load i64, i64* %12
%65 = icmp ult i64 %63, %64
br i1 %65, label %66, label %70
66: ; preds = %62, %50
%67 = call i32 @error_printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%68 = load i64, i64* @EACCES
%69 = sub i64 0, %68
store i64 %69, i64* %6
br label %93
70: ; preds = %62
%71 = load i64, i64* %12
%72 = add i64 %71, -1
store i64 %72, i64* %12
br label %73
73: ; preds = %70, %44
br label %74
74: ; preds = %73, %29
%75 = load i64, i64* %12
%76 = load i64, i64* %9
%77 = load i64, i64* %8
%78 = add i64 %76, %77
%79 = icmp uge i64 %75, %78
%80 = zext i1 %79 to i32
%81 = call i32 @assert(i32 %80)
%82 = load i8**, i8*** %10
%83 = load i8*, i8** %82
store i8* %83, i8** %14
%84 = load i8*, i8** %14
%85 = load i64, i64* %9
%86 = getelementptr inbounds i8, i8* %84, i64 %85
%87 = load i8*, i8** %7
%88 = load i64, i64* %8
%89 = call i32 @memcpy(i8* %86, i8* %87, i64 %88)
%90 = load i64, i64* %12
%91 = load i64*, i64** %11
store i64 %90, i64* %91
%92 = load i64, i64* %8
store i64 %92, i64* %6
br label %93
93: ; preds = %74, %66, %20
%94 = load i64, i64* %6
ret i64 %94
}

LLVM IR (-O2):
@EACCES = global i64 0
@.str = [27 x i8] c"truncating buffer failed!\0A\00"
define i64 @write_to_buffer(i8* %0, i64 %1, i64 %2, i8** %3, i64* %4) {
%6 = icmp eq i8** %3, null
%7 = icmp eq i64* %4, null
%8 = or i1 %6, %7
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = load i64, i64* @EACCES
%11 = sub i64 0, %10
br label %42
12: ; preds = %5
%13 = load i64, i64* %4
%14 = load i8*, i8** %3
%15 = icmp eq i8* %14, null
%16 = add i64 %2, %1
br i1 %15, label %17, label %22
17: ; preds = %12
%18 = add i64 %16, 1
%19 = tail call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %3
%21 = tail call i32 @memset(i8* %20, i32 0, i64 %18)
br label %34
22: ; preds = %12
%23 = icmp ugt i64 %16, %13
br i1 %23, label %24, label %34
24: ; preds = %22
%25 = add i64 %16, 1
%26 = tail call i64 @truncate_buffer(i8** %3, i64 %13, i64 %25)
%27 = icmp eq i64 %26, 0
%28 = icmp ult i64 %26, %25
%29 = or i1 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = tail call i32 @error_printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%32 = load i64, i64* @EACCES
%33 = sub i64 0, %32
br label %42
34: ; preds = %24, %22, %17
%35 = phi i64 [ %16, %24 ], [ %13, %22 ], [ %16, %17 ]
%36 = icmp uge i64 %35, %16
%37 = zext i1 %36 to i32
%38 = tail call i32 @assert(i32 %37)
%39 = load i8*, i8** %3
%40 = getelementptr inbounds i8, i8* %39, i64 %2
%41 = tail call i32 @memcpy(i8* %40, i8* %0, i64 %1)
store i64 %35, i64* %4
br label %42
42: ; preds = %30, %34, %9
%43 = phi i64 [ %11, %9 ], [ %1, %34 ], [ %33, %30 ]
ret i64 %43
}