#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 *, char const *, size_t);
size_t strlen(char const *);
bool write_string(char **string, size_t *length, const char *value) {
  size_t value_len = strlen(value);
  if (value_len + 1 < *length) {
    memcpy(*string, value, value_len + 1);
    *string += value_len;
    *length -= value_len;
    return true;
  } else
    return false;
}


x86 assembly (clang, -O0):
<write_string>:
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>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jae    1199 <write_string+0x79>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11a0 <write_string+0x80>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_string>:
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>
mov    %rax,%rbx
lea    0x1(%rax),%rdx
xor    %eax,%eax
cmp    (%r15),%rdx
jae    115d <write_string+0x3d>
mov    (%r14),%rdi
mov    %r12,%rsi
call   1040 <memcpy@plt>
add    %rbx,(%r14)
sub    %rbx,(%r15)
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @write_string(i8** %0, i64* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64*
%7 = alloca i8*
%8 = alloca i64
store i8** %0, i8*** %5
store i64* %1, i64** %6
store i8* %2, i8** %7
%9 = load i8*, i8** %7
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %8
%11 = load i64, i64* %8
%12 = add i64 %11, 1
%13 = load i64*, i64** %6
%14 = load i64, i64* %13
%15 = icmp ult i64 %12, %14
br i1 %15, label %16, label %31
16: ; preds = %3
%17 = load i8**, i8*** %5
%18 = load i8*, i8** %17
%19 = load i8*, i8** %7
%20 = load i64, i64* %8
%21 = add i64 %20, 1
%22 = call i32 @memcpy(i8* %18, i8* %19, i64 %21)
%23 = load i64, i64* %8
%24 = load i8**, i8*** %5
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 %23
store i8* %26, i8** %24
%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
store i32 1, i32* %4
br label %32
31: ; preds = %3
store i32 0, i32* %4
br label %32
32: ; preds = %31, %16
%33 = load i32, i32* %4
ret i32 %33
}

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