#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 */
struct simple_buffer {
  int dummy;
};

/* Variables and functions */
int simple_buffer_append(struct simple_buffer *const, char const *const,
                         size_t const);
int simple_buffer_terminate_string(struct simple_buffer *const);
size_t strlen(char *);
int simple_buffer_append_string(struct simple_buffer *const buf,
                                const char *const str) {
  const size_t len = strlen((char *)str);
  int err = simple_buffer_append(buf, str, len);
  if (err)
    return err;
  err = simple_buffer_terminate_string(buf);
  return err;
}


x86 assembly (clang, -O0):
<simple_buffer_append_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <simple_buffer_append@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1176 <simple_buffer_append_string+0x46>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1188 <simple_buffer_append_string+0x58>
mov    -0x10(%rbp),%rdi
call   1030 <simple_buffer_terminate_string@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simple_buffer_append_string>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <simple_buffer_append@plt>
test   %eax,%eax
je     115c <simple_buffer_append_string+0x2c>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <simple_buffer_terminate_string@plt>

LLVM IR (-O0):
%struct.simple_buffer = type { i32 }
define i32 @simple_buffer_append_string(%struct.simple_buffer* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.simple_buffer*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store %struct.simple_buffer* %0, %struct.simple_buffer** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %6
%10 = load %struct.simple_buffer*, %struct.simple_buffer** %4
%11 = load i8*, i8** %5
%12 = load i64, i64* %6
%13 = call i32 @simple_buffer_append(%struct.simple_buffer* %10, i8* %11, i64 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %7
store i32 %17, i32* %3
br label %22
18: ; preds = %2
%19 = load %struct.simple_buffer*, %struct.simple_buffer** %4
%20 = call i32 @simple_buffer_terminate_string(%struct.simple_buffer* %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
store i32 %21, i32* %3
br label %22
22: ; preds = %18, %16
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.simple_buffer = type { i32 }
define i32 @simple_buffer_append_string(%struct.simple_buffer* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%4 = tail call i32 @simple_buffer_append(%struct.simple_buffer* %0, i8* %1, i64 %3)
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @simple_buffer_terminate_string(%struct.simple_buffer* %0)
br label %8
8: ; preds = %2, %6
%9 = phi i32 [ %7, %6 ], [ %4, %2 ]
ret i32 %9
}