#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 /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ const *,
                          size_t);
int strlen(char const *);
uint8_t *write_raw(uint8_t *p, size_t *s, const uint8_t *v) {
  size_t len;
  len = strlen((const char *)v) + 1;
  if (*s < len)
    return (NULL);
  memcpy(p, v, len);
  p += len;
  *s -= len;
  return (p);
}


x86 assembly (clang, -O0):
<write_raw>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x28(%rbp),%rax
jae    1164 <write_raw+0x44>
movq   $0x0,-0x8(%rbp)
jmp    119e <write_raw+0x7e>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_raw>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rdx,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
cmp    %rbx,(%r15)
jae    1148 <write_raw+0x28>
xor    %eax,%eax
jmp    115d <write_raw+0x3d>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <memcpy@plt>
lea    (%r14,%rbx,4),%rax
sub    %rbx,(%r15)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32* @write_raw(i32* %0, i64* nocapture %1, i32* %2) {
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @strlen(i8* %4)
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = load i64, i64* %1
%9 = icmp ult i64 %8, %7
br i1 %9, label %15, label %10
10: ; preds = %3
%11 = tail call i32 @memcpy(i32* %0, i32* %2, i64 %7)
%12 = getelementptr inbounds i32, i32* %0, i64 %7
%13 = load i64, i64* %1
%14 = sub i64 %13, %7
store i64 %14, i64* %1
br label %15
15: ; preds = %3, %10
%16 = phi i32* [ %12, %10 ], [ null, %3 ]
ret i32* %16
}