#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*/ esock_free(char *);
void *esock_malloc(int);
char **esock_realloc(char **, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok_quote(char *, char *);
int esock_build_argv(char *cmd, char ***argvp) {
  int argvsize = 10, argc = 0;
  char *args, *tokp, *argp;
  char **argv;
  argv = esock_malloc(argvsize * sizeof(char *));
  args = esock_malloc(strlen(cmd) + 1);
  strcpy(args, cmd);
  tokp = strtok_quote(args, " \t");
  while (tokp != NULL) {
    if (argc + 1 >= argvsize) {
      argvsize += 10;
      argv = esock_realloc(argv, argvsize * sizeof(char *));
    }
    argp = esock_malloc(strlen(tokp) + 1);
    strcpy(argp, tokp);
    argv[argc++] = argp;
    tokp = strtok_quote(NULL, " \t");
  }
  esock_free(args);
  argv[argc] = NULL;
  *argvp = argv;
  return argc;
}


x86 assembly (clang, -O0):
<esock_build_argv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xa,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <esock_malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1080 <esock_malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1070 <strtok_quote@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     125a <esock_build_argv+0xfa>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jl     1203 <esock_build_argv+0xa3>
mov    -0x14(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x14(%rbp)
mov    -0x38(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%esi
call   1060 <esock_realloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1080 <esock_malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdx
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdb4(%rip),%rsi
call   1070 <strtok_quote@plt>
mov    %rax,-0x28(%rbp)
jmp    11c9 <esock_build_argv+0x69>
mov    -0x20(%rbp),%rdi
call   1040 <esock_free@plt>
mov    -0x38(%rbp),%rax
movslq -0x18(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<esock_build_argv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%rbp
mov    $0x50,%edi
call   1080 <esock_malloc@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1080 <esock_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
lea    0xe5a(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strtok_quote@plt>
test   %rax,%rax
je     123f <esock_build_argv+0xdf>
mov    %rax,%rbp
mov    %rbx,0x8(%rsp)
mov    %r14,0x10(%rsp)
mov    $0xa,%r15d
xor    %ebx,%ebx
jmp    120b <esock_build_argv+0xab>
xchg   %ax,%ax
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1080 <esock_malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
mov    %r14,(%r12,%rbx,8)
xor    %edi,%edi
lea    0xe05(%rip),%rsi
call   1070 <strtok_quote@plt>
mov    %rax,%rbp
mov    %r13,%rbx
test   %rax,%rax
je     1233 <esock_build_argv+0xd3>
lea    0x1(%rbx),%r13
cmp    %r13d,%r15d
jg     11d0 <esock_build_argv+0x70>
mov    %r15d,%r14d
add    $0xa,%r14d
lea    0x50(,%r15,8),%esi
mov    %r12,%rdi
call   1060 <esock_realloc@plt>
mov    %rax,%r12
mov    %r14d,%r15d
jmp    11d0 <esock_build_argv+0x70>
mov    0x10(%rsp),%r14
mov    0x8(%rsp),%rbx
jmp    1242 <esock_build_argv+0xe2>
xor    %r13d,%r13d
mov    %rbx,%rdi
call   1040 <esock_free@plt>
mov    %r13d,%eax
movq   $0x0,(%r12,%rax,8)
mov    %r12,(%r14)
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c" \09\00"
define i32 @esock_build_argv(i8* %0, i8*** %1) {
%3 = alloca i8*
%4 = alloca i8***
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8**
store i8* %0, i8** %3
store i8*** %1, i8**** %4
store i32 10, i32* %5
store i32 0, i32* %6
%11 = load i32, i32* %5
%12 = sext i32 %11 to i64
%13 = mul i64 %12, 8
%14 = trunc i64 %13 to i32
%15 = call i8* @esock_malloc(i32 %14)
%16 = bitcast i8* %15 to i8**
store i8** %16, i8*** %10
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 1
%20 = call i8* @esock_malloc(i32 %19)
store i8* %20, i8** %7
%21 = load i8*, i8** %7
%22 = load i8*, i8** %3
%23 = call i32 @strcpy(i8* %21, i8* %22)
%24 = load i8*, i8** %7
%25 = call i8* @strtok_quote(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** %8
br label %26
26: ; preds = %43, %2
%27 = load i8*, i8** %8
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %58
29: ; preds = %26
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
%32 = load i32, i32* %5
%33 = icmp sge i32 %31, %32
br i1 %33, label %34, label %43
34: ; preds = %29
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 10
store i32 %36, i32* %5
%37 = load i8**, i8*** %10
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = mul i64 %39, 8
%41 = trunc i64 %40 to i32
%42 = call i8** @esock_realloc(i8** %37, i32 %41)
store i8** %42, i8*** %10
br label %43
43: ; preds = %34, %29
%44 = load i8*, i8** %8
%45 = call i32 @strlen(i8* %44)
%46 = add nsw i32 %45, 1
%47 = call i8* @esock_malloc(i32 %46)
store i8* %47, i8** %9
%48 = load i8*, i8** %9
%49 = load i8*, i8** %8
%50 = call i32 @strcpy(i8* %48, i8* %49)
%51 = load i8*, i8** %9
%52 = load i8**, i8*** %10
%53 = load i32, i32* %6
%54 = add nsw i32 %53, 1
store i32 %54, i32* %6
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds i8*, i8** %52, i64 %55
store i8* %51, i8** %56
%57 = call i8* @strtok_quote(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %57, i8** %8
br label %26
58: ; preds = %26
%59 = load i8*, i8** %7
%60 = call i32 @esock_free(i8* %59)
%61 = load i8**, i8*** %10
%62 = load i32, i32* %6
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %61, i64 %63
store i8* null, i8** %64
%65 = load i8**, i8*** %10
%66 = load i8***, i8**** %4
store i8** %65, i8*** %66
%67 = load i32, i32* %6
ret i32 %67
}

LLVM IR (-O2):
@.str = [3 x i8] c" \09\00"
define i32 @esock_build_argv(i8* %0, i8*** nocapture writeonly %1) {
%3 = tail call i8* @esock_malloc(i32 80)
%4 = bitcast i8* %3 to i8**
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 1
%7 = tail call i8* @esock_malloc(i32 %6)
%8 = tail call i32 @strcpy(i8* %7, i8* %0)
%9 = tail call i8* @strtok_quote(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %35, label %11
11: ; preds = %2, %23
%12 = phi i64 [ %16, %23 ], [ 0, %2 ]
%13 = phi i8** [ %25, %23 ], [ %4, %2 ]
%14 = phi i32 [ %24, %23 ], [ 10, %2 ]
%15 = phi i8* [ %31, %23 ], [ %9, %2 ]
%16 = add nuw i64 %12, 1
%17 = trunc i64 %16 to i32
%18 = icmp sgt i32 %14, %17
br i1 %18, label %23, label %19
19: ; preds = %11
%20 = add nsw i32 %14, 10
%21 = shl i32 %20, 3
%22 = tail call i8** @esock_realloc(i8** %13, i32 %21)
br label %23
23: ; preds = %19, %11
%24 = phi i32 [ %20, %19 ], [ %14, %11 ]
%25 = phi i8** [ %22, %19 ], [ %13, %11 ]
%26 = tail call i32 @strlen(i8* %15)
%27 = add nsw i32 %26, 1
%28 = tail call i8* @esock_malloc(i32 %27)
%29 = tail call i32 @strcpy(i8* %28, i8* %15)
%30 = getelementptr inbounds i8*, i8** %25, i64 %12
store i8* %28, i8** %30
%31 = tail call i8* @strtok_quote(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %11
33: ; preds = %23
%34 = trunc i64 %16 to i32
br label %35
35: ; preds = %33, %2
%36 = phi i32 [ 0, %2 ], [ %34, %33 ]
%37 = phi i8** [ %4, %2 ], [ %25, %33 ]
%38 = tail call i32 @esock_free(i8* %7)
%39 = zext i32 %36 to i64
%40 = getelementptr inbounds i8*, i8** %37, i64 %39
store i8* null, i8** %40
store i8** %37, i8*** %1
ret i32 %36
}