#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*/ memcpy(char *, void const *, int);
int write_bytes(char **ptr, const char *end, const void *arg, int arg_len) {
  char *p = *ptr, *arg_end;
  arg_end = p + arg_len;
  if (arg_end > end || arg_end < p)
    return -1;
  memcpy(p, arg, arg_len);
  *ptr = arg_end;
  return 0;
}


x86 assembly (clang, -O0):
<write_bytes>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     115d <write_bytes+0x4d>
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    1169 <write_bytes+0x59>
movl   $0xffffffff,-0x4(%rbp)
jmp    118b <write_bytes+0x7b>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_bytes>:
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%eax
test   %ecx,%ecx
js     113d <write_bytes+0x2d>
mov    %rdi,%r14
mov    (%rdi),%rdi
movslq %ecx,%rbx
add    %rdi,%rbx
cmp    %rsi,%rbx
ja     113d <write_bytes+0x2d>
mov    %rdx,%rsi
mov    %ecx,%edx
call   1030 <memcpy@plt>
mov    %rbx,(%r14)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @write_bytes(i8** %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8** %0, i8*** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = load i8**, i8*** %6
%13 = load i8*, i8** %12
store i8* %13, i8** %10
%14 = load i8*, i8** %10
%15 = load i32, i32* %9
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
store i8* %17, i8** %11
%18 = load i8*, i8** %11
%19 = load i8*, i8** %7
%20 = icmp ugt i8* %18, %19
br i1 %20, label %25, label %21
21: ; preds = %4
%22 = load i8*, i8** %11
%23 = load i8*, i8** %10
%24 = icmp ult i8* %22, %23
br i1 %24, label %25, label %26
25: ; preds = %21, %4
store i32 -1, i32* %5
br label %33
26: ; preds = %21
%27 = load i8*, i8** %10
%28 = load i8*, i8** %8
%29 = load i32, i32* %9
%30 = call i32 @memcpy(i8* %27, i8* %28, i32 %29)
%31 = load i8*, i8** %11
%32 = load i8**, i8*** %6
store i8* %31, i8** %32
store i32 0, i32* %5
br label %33
33: ; preds = %26, %25
%34 = load i32, i32* %5
ret i32 %34
}

LLVM IR (-O2):
define i32 @write_bytes(i8** nocapture %0, i8* readnone %1, i8* %2, i32 %3) {
%5 = load i8*, i8** %0
%6 = sext i32 %3 to i64
%7 = getelementptr inbounds i8, i8* %5, i64 %6
%8 = icmp ugt i8* %7, %1
%9 = icmp slt i32 %3, 0
%10 = or i1 %9, %8
br i1 %10, label %13, label %11
11: ; preds = %4
%12 = tail call i32 @memcpy(i8* %5, i8* %2, i32 %3)
store i8* %7, i8** %0
br label %13
13: ; preds = %4, %11
%14 = phi i32 [ 0, %11 ], [ -1, %4 ]
ret i32 %14
}