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

/* Variables and functions */
int /*<<< orphan*/ err_handler(char *);
char *malloc(size_t);
int /*<<< orphan*/ strcpy(char *, char *);
int add_argv(char **dest, char *content) {
  size_t i;
  i = 0;
  while (content[i++] != '\0')
    ;
  if ((*dest = malloc(i)) == NULL) {
    err_handler("Cannot allocate memory");
    return 0;
  }
  strcpy(*dest, content);
  return 1;
}


x86 assembly (clang, -O0):
<add_argv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x20(%rbp)
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     116d <add_argv+0x3d>
jmp    1148 <add_argv+0x18>
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    -0x10(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jne    119f <add_argv+0x6f>
lea    0xe72(%rip),%rdi
call   1030 <err_handler@plt>
movl   $0x0,-0x4(%rbp)
jmp    11b6 <add_argv+0x86>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_argv>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
nopl   0x0(%rax)
cmpb   $0x0,(%rbx,%rdi,1)
lea    0x1(%rdi),%rdi
jne    1140 <add_argv+0x10>
call   1050 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1169 <add_argv+0x39>
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    $0x1,%eax
jmp    1177 <add_argv+0x47>
lea    0xe90(%rip),%rdi
call   1030 <err_handler@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Cannot allocate memory\00"
define i32 @add_argv(i8** %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i64
store i8** %0, i8*** %4
store i8* %1, i8** %5
store i64 0, i64* %6
br label %7
7: ; preds = %15, %2
%8 = load i8*, i8** %5
%9 = load i64, i64* %6
%10 = add i64 %9, 1
store i64 %10, i64* %6
%11 = getelementptr inbounds i8, i8* %8, i64 %9
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %7
br label %7
16: ; preds = %7
%17 = load i64, i64* %6
%18 = call i8* @malloc(i64 %17)
%19 = load i8**, i8*** %4
store i8* %18, i8** %19
%20 = icmp eq i8* %18, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @err_handler(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %28
23: ; preds = %16
%24 = load i8**, i8*** %4
%25 = load i8*, i8** %24
%26 = load i8*, i8** %5
%27 = call i32 @strcpy(i8* %25, i8* %26)
store i32 1, i32* %3
br label %28
28: ; preds = %23, %21
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [23 x i8] c"Cannot allocate memory\00"
define i32 @add_argv(i8** nocapture writeonly %0, i8* %1) {
br label %3
3: ; preds = %3, %2
%4 = phi i64 [ 0, %2 ], [ %5, %3 ]
%5 = add i64 %4, 1
%6 = getelementptr inbounds i8, i8* %1, i64 %4
%7 = load i8, i8* %6
%8 = icmp eq i8 %7, 0
br i1 %8, label %9, label %3
9: ; preds = %3
%10 = tail call i8* @malloc(i64 %5)
store i8* %10, i8** %0
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @err_handler(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %16
14: ; preds = %9
%15 = tail call i32 @strcpy(i8* %10, i8* %1)
br label %16
16: ; preds = %14, %12
%17 = phi i32 [ 0, %12 ], [ 1, %14 ]
ret i32 %17
}