#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 uint32_t;
struct matlab_buffer {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ensure_buffer(struct matlab_buffer *, size_t);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, size_t);
size_t strlen(char const *);
int /*<<< orphan*/ write_buffer_uint32(struct matlab_buffer *, int);
void write_matlab_string(struct matlab_buffer *buffer, const char *string) {
  size_t l = strlen(string);
  write_buffer_uint32(buffer, 1);
  write_buffer_uint32(buffer, (uint32_t)l);
  memcpy(ensure_buffer(buffer, (l + 7) & ~7U), string, l);
}


x86 assembly (clang, -O0):
<write_matlab_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1030 <write_buffer_uint32@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
call   1030 <write_buffer_uint32@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x7,%rsi
movabs $0xfffffff8,%rax
and    %rax,%rsi
call   1060 <ensure_buffer@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_matlab_string>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <write_buffer_uint32@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1030 <write_buffer_uint32@plt>
lea    0x7(%r15),%esi
and    $0xfffffff8,%esi
mov    %rbx,%rdi
call   1060 <ensure_buffer@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <memcpy@plt>

LLVM IR (-O0):
%struct.matlab_buffer = type { i32 }
define void @write_matlab_string(%struct.matlab_buffer* %0, i8* %1) {
%3 = alloca %struct.matlab_buffer*
%4 = alloca i8*
%5 = alloca i64
store %struct.matlab_buffer* %0, %struct.matlab_buffer** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = call i64 @strlen(i8* %6)
store i64 %7, i64* %5
%8 = load %struct.matlab_buffer*, %struct.matlab_buffer** %3
%9 = call i32 @write_buffer_uint32(%struct.matlab_buffer* %8, i32 1)
%10 = load %struct.matlab_buffer*, %struct.matlab_buffer** %3
%11 = load i64, i64* %5
%12 = trunc i64 %11 to i32
%13 = call i32 @write_buffer_uint32(%struct.matlab_buffer* %10, i32 %12)
%14 = load %struct.matlab_buffer*, %struct.matlab_buffer** %3
%15 = load i64, i64* %5
%16 = add i64 %15, 7
%17 = and i64 %16, 4294967288
%18 = call i32 @ensure_buffer(%struct.matlab_buffer* %14, i64 %17)
%19 = load i8*, i8** %4
%20 = load i64, i64* %5
%21 = call i32 @memcpy(i32 %18, i8* %19, i64 %20)
ret void
}

LLVM IR (-O2):
%struct.matlab_buffer = type { i32 }
define void @write_matlab_string(%struct.matlab_buffer* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i32 @write_buffer_uint32(%struct.matlab_buffer* %0, i32 1)
%5 = trunc i64 %3 to i32
%6 = tail call i32 @write_buffer_uint32(%struct.matlab_buffer* %0, i32 %5)
%7 = add i64 %3, 7
%8 = and i64 %7, 4294967288
%9 = tail call i32 @ensure_buffer(%struct.matlab_buffer* %0, i64 %8)
%10 = tail call i32 @memcpy(i32 %9, i8* %1, i64 %3)
ret void
}