char *filepath_str(struct filepath *self) {
  int length = filepath_length(self);
  char *alloc = (char *)malloc(length + 1);
  assert(alloc);
  int pos = filepath_write(self, alloc);
  assert(pos == length);
  return alloc;
}


x86 assembly (clang, -O0):
<filepath_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x0,%al
call   1030 <filepath_length@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1060 <filepath_write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filepath_str>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <filepath_length@plt>
mov    %eax,%r15d
lea    0x1(%r15),%eax
movslq %eax,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %eax,%eax
call   1040 <assert@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <filepath_write@plt>
xor    %edi,%edi
cmp    %r15d,%eax
sete   %dil
xor    %eax,%eax
call   1040 <assert@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.filepath = type opaque
define i8* @filepath_str(%struct.filepath* %0) {
%2 = alloca %struct.filepath*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store %struct.filepath* %0, %struct.filepath** %2
%6 = load %struct.filepath*, %struct.filepath** %2
%7 = call i32 (%struct.filepath*, ...) bitcast (i32 (...)* @filepath_length to i32 (%struct.filepath*, ...)*)(%struct.filepath* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = add nsw i32 %8, 1
%10 = sext i32 %9 to i64
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %12)
%14 = load %struct.filepath*, %struct.filepath** %2
%15 = load i8*, i8** %4
%16 = call i32 (%struct.filepath*, i8*, ...) bitcast (i32 (...)* @filepath_write to i32 (%struct.filepath*, i8*, ...)*)(%struct.filepath* %14, i8* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = load i32, i32* %3
%19 = icmp eq i32 %17, %18
%20 = zext i1 %19 to i32
%21 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %20)
%22 = load i8*, i8** %4
ret i8* %22
}

LLVM IR (-O2):
%struct.filepath = type opaque
define i8* @filepath_str(%struct.filepath* %0) {
%2 = tail call i32 (%struct.filepath*, ...) bitcast (i32 (...)* @filepath_length to i32 (%struct.filepath*, ...)*)(%struct.filepath* %0)
%3 = add nsw i32 %2, 1
%4 = sext i32 %3 to i64
%5 = tail call i8* @malloc(i64 %4)
%6 = tail call i32 (i8*, ...) bitcast (i32 (...)* @assert to i32 (i8*, ...)*)(i8* %5)
%7 = tail call i32 (%struct.filepath*, i8*, ...) bitcast (i32 (...)* @filepath_write to i32 (%struct.filepath*, i8*, ...)*)(%struct.filepath* %0, i8* %5)
%8 = icmp eq i32 %7, %2
%9 = zext i1 %8 to i32
%10 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %9)
ret i8* %5
}