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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
scalar_t__ realloc(char *, size_t);
error_t argz_append(char **argz, size_t *argz_len, const char *buf,
                    size_t buf_len) {
  if (buf_len) {
    size_t last = *argz_len;
    *argz_len += buf_len;
    if (!(*argz = (char *)realloc(*argz, *argz_len)))
      return ENOMEM;
    memcpy(*argz + last, buf, buf_len);
  }
  return 0;
}


x86 assembly (clang, -O0):
<argz_append>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11a9 <argz_append+0x89>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rsi
call   1040 <realloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    1191 <argz_append+0x71>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b0 <argz_append+0x90>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<argz_append>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %r15d,%r15d
test   %rcx,%rcx
je     1174 <argz_append+0x54>
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %rdi,%rbp
mov    (%rsi),%r12
lea    (%r12,%rcx,1),%rax
mov    %rax,(%rsi)
mov    (%rdi),%rdi
mov    %rax,%rsi
call   1040 <realloc@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
je     116a <argz_append+0x4a>
add    %r12,%rax
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <memcpy@plt>
jmp    1174 <argz_append+0x54>
mov    0x2e6f(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
define i32 @argz_append(i8** %0, i64* %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
store i8** %0, i8*** %6
store i64* %1, i64** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = load i64, i64* %9
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %38
13: ; preds = %4
%14 = load i64*, i64** %7
%15 = load i64, i64* %14
store i64 %15, i64* %10
%16 = load i64, i64* %9
%17 = load i64*, i64** %7
%18 = load i64, i64* %17
%19 = add i64 %18, %16
store i64 %19, i64* %17
%20 = load i8**, i8*** %6
%21 = load i8*, i8** %20
%22 = load i64*, i64** %7
%23 = load i64, i64* %22
%24 = call i64 @realloc(i8* %21, i64 %23)
%25 = inttoptr i64 %24 to i8*
%26 = load i8**, i8*** %6
store i8* %25, i8** %26
%27 = icmp ne i8* %25, null
br i1 %27, label %30, label %28
28: ; preds = %13
%29 = load i32, i32* @ENOMEM
store i32 %29, i32* %5
br label %39
30: ; preds = %13
%31 = load i8**, i8*** %6
%32 = load i8*, i8** %31
%33 = load i64, i64* %10
%34 = getelementptr inbounds i8, i8* %32, i64 %33
%35 = load i8*, i8** %8
%36 = load i64, i64* %9
%37 = call i32 @memcpy(i8* %34, i8* %35, i64 %36)
br label %38
38: ; preds = %30, %4
store i32 0, i32* %5
br label %39
39: ; preds = %38, %28
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
@ENOMEM = global i32 0
define i32 @argz_append(i8** nocapture %0, i64* nocapture %1, i8* %2, i64 %3) {
%5 = icmp eq i64 %3, 0
br i1 %5, label %18, label %6
6: ; preds = %4
%7 = load i64, i64* %1
%8 = add i64 %7, %3
store i64 %8, i64* %1
%9 = load i8*, i8** %0
%10 = tail call i64 @realloc(i8* %9, i64 %8)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %0
%12 = icmp eq i64 %10, 0
br i1 %12, label %16, label %13
13: ; preds = %6
%14 = getelementptr inbounds i8, i8* %11, i64 %7
%15 = tail call i32 @memcpy(i8* %14, i8* %2, i64 %3)
br label %18
16: ; preds = %6
%17 = load i32, i32* @ENOMEM
br label %18
18: ; preds = %4, %13, %16
%19 = phi i32 [ %17, %16 ], [ 0, %13 ], [ 0, %4 ]
ret i32 %19
}