#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 *, int);
scalar_t__ realloc(char *, size_t);
int strlen(char const *);
error_t argz_add(char **argz, size_t *argz_len, const char *str) {
  int len_to_add = 0;
  size_t last = *argz_len;
  if (str == NULL)
    return 0;
  len_to_add = strlen(str) + 1;
  *argz_len += len_to_add;
  if (!(*argz = (char *)realloc(*argz, *argz_len)))
    return ENOMEM;
  memcpy(*argz + last, str, len_to_add);
  return 0;
}


x86 assembly (clang, -O0):
<argz_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116d <argz_add+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    11dd <argz_add+0xad>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movslq -0x24(%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   1050 <realloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    11bf <argz_add+0x8f>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11dd <argz_add+0xad>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<argz_add>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %rdx,%rdx
je     119d <argz_add+0x6d>
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,%r15
mov    (%rsi),%r13
mov    %rdx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
cltq
mov    0x0(%rbp),%rcx
lea    (%rax,%rcx,1),%rsi
add    $0x1,%rsi
mov    %rsi,0x0(%rbp)
mov    (%r15),%rdi
call   1050 <realloc@plt>
mov    %rax,(%r15)
test   %rax,%rax
je     1193 <argz_add+0x63>
add    $0x1,%r12d
add    %r13,%rax
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
jmp    119d <argz_add+0x6d>
mov    0x2e46(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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