#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 tokens {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ execv(char *, char **);
size_t tokens_get_length(struct tokens *);
char *tokens_get_token(struct tokens *, int);
void cmd_exec_helper(char *str, struct tokens *tokens) {
  char *argv[tokens_get_length(tokens) + 1];
  int i = 0;
  for (; i < tokens_get_length(tokens); i++) {
    argv[i] = tokens_get_token(tokens, i);
  }
  argv[tokens_get_length(tokens)] = NULL;
  execv(str, argv);
}


x86 assembly (clang, -O0):
<cmd_exec_helper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <tokens_get_length@plt>
mov    %rax,%rcx
mov    %rcx,%rax
add    $0x1,%rax
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
lea    0x17(,%rcx,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <tokens_get_length@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
jae    11c8 <cmd_exec_helper+0x98>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <tokens_get_token@plt>
mov    %rax,%rdx
mov    -0x30(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    117e <cmd_exec_helper+0x4e>
mov    -0x10(%rbp),%rdi
call   1040 <tokens_get_length@plt>
mov    -0x30(%rbp),%rsi
movq   $0x0,(%rsi,%rax,8)
mov    -0x8(%rbp),%rdi
call   1050 <execv@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_exec_helper>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <tokens_get_length@plt>
mov    %rsp,%r15
lea    0x17(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r12,%rdi
call   1040 <tokens_get_length@plt>
test   %rax,%rax
je     118f <cmd_exec_helper+0x5f>
xor    %ebx,%ebx
nopl   (%rax)
mov    %r12,%rdi
mov    %ebx,%esi
call   1030 <tokens_get_token@plt>
mov    %rax,(%r15,%rbx,8)
add    $0x1,%rbx
mov    %r12,%rdi
call   1040 <tokens_get_length@plt>
cmp    %rbx,%rax
ja     1170 <cmd_exec_helper+0x40>
mov    %r12,%rdi
call   1040 <tokens_get_length@plt>
movq   $0x0,(%r15,%rax,8)
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <execv@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tokens = type { i32 }
define void @cmd_exec_helper(i8* %0, %struct.tokens* %1) {
%3 = alloca i8*
%4 = alloca %struct.tokens*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
store %struct.tokens* %1, %struct.tokens** %4
%8 = load %struct.tokens*, %struct.tokens** %4
%9 = call i64 @tokens_get_length(%struct.tokens* %8)
%10 = add i64 %9, 1
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8*, i64 %10
store i64 %10, i64* %6
store i32 0, i32* %7
br label %13
13: ; preds = %26, %2
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = load %struct.tokens*, %struct.tokens** %4
%17 = call i64 @tokens_get_length(%struct.tokens* %16)
%18 = icmp ult i64 %15, %17
br i1 %18, label %19, label %29
19: ; preds = %13
%20 = load %struct.tokens*, %struct.tokens** %4
%21 = load i32, i32* %7
%22 = call i8* @tokens_get_token(%struct.tokens* %20, i32 %21)
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %12, i64 %24
store i8* %22, i8** %25
br label %26
26: ; preds = %19
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %13
29: ; preds = %13
%30 = load %struct.tokens*, %struct.tokens** %4
%31 = call i64 @tokens_get_length(%struct.tokens* %30)
%32 = getelementptr inbounds i8*, i8** %12, i64 %31
store i8* null, i8** %32
%33 = load i8*, i8** %3
%34 = call i32 @execv(i8* %33, i8** %12)
%35 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
%struct.tokens = type { i32 }
define void @cmd_exec_helper(i8* %0, %struct.tokens* %1) {
%3 = tail call i64 @tokens_get_length(%struct.tokens* %1)
%4 = add i64 %3, 1
%5 = alloca i8*, i64 %4
%6 = tail call i64 @tokens_get_length(%struct.tokens* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %16, label %8
8: ; preds = %2, %8
%9 = phi i64 [ %13, %8 ], [ 0, %2 ]
%10 = trunc i64 %9 to i32
%11 = tail call i8* @tokens_get_token(%struct.tokens* %1, i32 %10)
%12 = getelementptr inbounds i8*, i8** %5, i64 %9
store i8* %11, i8** %12
%13 = add nuw i64 %9, 1
%14 = tail call i64 @tokens_get_length(%struct.tokens* %1)
%15 = icmp ugt i64 %14, %13
br i1 %15, label %8, label %16
16: ; preds = %8, %2
%17 = tail call i64 @tokens_get_length(%struct.tokens* %1)
%18 = getelementptr inbounds i8*, i8** %5, i64 %17
store i8* null, i8** %18
%19 = call i32 @execv(i8* %0, i8** %5)
ret void
}