#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 */
scalar_t__ malloc(int);
scalar_t__ realloc(char **, int);
char *strtok(char *, char *);
char *trim_word(char *);
char **get_commands(char *cmd_text, int *num_commands) {
  char **commands = NULL;
  char *cmd, *trimmed_cmd;
  int i = 0;
  cmd = strtok(cmd_text, ";");
  while (cmd != NULL) {
    trimmed_cmd = trim_word(cmd);
    if (trimmed_cmd) {
      if (!commands)
        commands = (char **)malloc(sizeof(char *));
      else
        commands = (char **)realloc(commands, sizeof(char *));
      commands[i++] = trimmed_cmd;
    }
    cmd = strtok(NULL, ";");
  }
  *num_commands = i;
  return commands;
}


x86 assembly (clang, -O0):
<get_commands>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe96(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f9 <get_commands+0xb9>
mov    -0x20(%rbp),%rdi
call   1050 <trim_word@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e0 <get_commands+0xa0>
cmpq   $0x0,-0x18(%rbp)
jne    11b4 <get_commands+0x74>
mov    $0x8,%edi
call   1030 <malloc@plt>
mov    %rax,-0x18(%rbp)
jmp    11c6 <get_commands+0x86>
mov    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1040 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x2c(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe15(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1173 <get_commands+0x33>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_commands>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
lea    0xead(%rip),%rsi
call   1060 <strtok@plt>
test   %rax,%rax
je     11bb <get_commands+0x7b>
xor    %r13d,%r13d
lea    0xe99(%rip),%r15
xor    %r12d,%r12d
jmp    119a <get_commands+0x5a>
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x8,%esi
call   1040 <realloc@plt>
mov    %rax,%r12
movslq %r13d,%rax
add    $0x1,%r13d
mov    %rbx,(%r12,%rax,8)
xor    %edi,%edi
mov    %r15,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
je     11c1 <get_commands+0x81>
mov    %rax,%rdi
call   1050 <trim_word@plt>
test   %rax,%rax
je     118b <get_commands+0x4b>
mov    %rax,%rbx
test   %r12,%r12
jne    1170 <get_commands+0x30>
mov    $0x8,%edi
call   1030 <malloc@plt>
jmp    117d <get_commands+0x3d>
xor    %r12d,%r12d
xor    %r13d,%r13d
mov    %r13d,(%r14)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c";\00"
define i8** @get_commands(i8* %0, i32* nocapture writeonly %1) {
%3 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %28, label %5
5: ; preds = %2, %23
%6 = phi i32 [ %25, %23 ], [ 0, %2 ]
%7 = phi i8** [ %24, %23 ], [ null, %2 ]
%8 = phi i8* [ %26, %23 ], [ %3, %2 ]
%9 = tail call i8* @trim_word(i8* %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %23, label %11
11: ; preds = %5
%12 = icmp eq i8** %7, null
br i1 %12, label %13, label %15
13: ; preds = %11
%14 = tail call i64 @malloc(i32 8)
br label %17
15: ; preds = %11
%16 = tail call i64 @realloc(i8** %7, i32 8)
br label %17
17: ; preds = %15, %13
%18 = phi i64 [ %16, %15 ], [ %14, %13 ]
%19 = inttoptr i64 %18 to i8**
%20 = add nsw i32 %6, 1
%21 = sext i32 %6 to i64
%22 = getelementptr inbounds i8*, i8** %19, i64 %21
store i8* %9, i8** %22
br label %23
23: ; preds = %17, %5
%24 = phi i8** [ %19, %17 ], [ %7, %5 ]
%25 = phi i32 [ %20, %17 ], [ %6, %5 ]
%26 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %5
28: ; preds = %23, %2
%29 = phi i8** [ null, %2 ], [ %24, %23 ]
%30 = phi i32 [ 0, %2 ], [ %25, %23 ]
store i32 %30, i32* %1
ret i8** %29
}