#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*/ perror(char *);
char **realloc(char **, int);
int insert_argv(char ***argv, int *argc, char *arg) {
  char **argv_realloc = realloc(*argv, (*argc + 1) * sizeof(char *));
  if (!argv_realloc) {
    perror("realloc");
    return 0;
  }
  argv_realloc[(*argc)++] = arg;
  *argv = argv_realloc;
  return 1;
}


x86 assembly (clang, -O0):
<insert_argv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1030 <realloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1178 <insert_argv+0x58>
lea    0xe99(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a6 <insert_argv+0x86>
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rsi
mov    (%rsi),%ecx
mov    %ecx,%edi
add    $0x1,%edi
mov    %edi,(%rsi)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert_argv>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    (%rdi),%rdi
mov    (%rsi),%eax
lea    0x8(,%rax,8),%esi
call   1030 <realloc@plt>
test   %rax,%rax
je     115e <insert_argv+0x3e>
movslq (%rbx),%rcx
lea    0x1(%rcx),%edx
mov    %edx,(%rbx)
mov    %r14,(%rax,%rcx,8)
mov    %rax,(%r15)
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe9b(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
jmp    1158 <insert_argv+0x38>

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

LLVM IR (-O2):
@.str = [8 x i8] c"realloc\00"
define i32 @insert_argv(i8*** nocapture %0, i32* nocapture %1, i8* %2) {
%4 = load i8**, i8*** %0
%5 = load i32, i32* %1
%6 = shl i32 %5, 3
%7 = add i32 %6, 8
%8 = tail call i8** @realloc(i8** %4, i32 %7)
%9 = icmp eq i8** %8, null
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %17
12: ; preds = %3
%13 = load i32, i32* %1
%14 = add nsw i32 %13, 1
store i32 %14, i32* %1
%15 = sext i32 %13 to i64
%16 = getelementptr inbounds i8*, i8** %8, i64 %15
store i8* %2, i8** %16
store i8** %8, i8*** %0
br label %17
17: ; preds = %12, %10
%18 = phi i32 [ 1, %12 ], [ 0, %10 ]
ret i32 %18
}