#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*/ exit(int);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ printf(char *, int, char *);
char **realloc(char **, int);
char *strtok(char *, char *);
void split_str_to_array() {
  char str[] = "Hello world";
  char **res = NULL;
  char *p = strtok(str, " ");
  int n_spaces = 0, i;
  while (p) {
    res = realloc(res, sizeof(char *) * ++n_spaces);
    if (res == NULL)
      exit(-1);
    res[n_spaces - 1] = p;
    p = strtok(NULL, " ");
  }
  res = realloc(res, sizeof(char *) * (n_spaces + 1));
  res[n_spaces] = 0;
  for (i = 0; i < (n_spaces + 1); ++i)
    printf("res[%d] = %s\n", i, res[i]);
  free(res);
}


x86 assembly (clang, -O0):
<split_str_to_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0xea1(%rip),%rax
mov    %rax,-0xc(%rbp)
mov    0xe9f(%rip),%eax
mov    %eax,-0x4(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0xc(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11fb <split_str_to_array+0xab>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <split_str_to_array+0x7d>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe1f(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    118f <split_str_to_array+0x3f>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%esi
call   1050 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jge    1267 <split_str_to_array+0x117>
mov    -0x28(%rbp),%esi
mov    -0x18(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xdba(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    122d <split_str_to_array+0xdd>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<split_str_to_array>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
movabs $0x6f77206f6c6c6548,%rax
20 77 6f
mov    %rax,(%rsp)
movl   $0x646c72,0x8(%rsp)
lea    0xe93(%rip),%rsi
mov    %rsp,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     11d5 <split_str_to_array+0x85>
mov    %rax,%r14
mov    $0x8,%ebp
xor    %r12d,%r12d
lea    0xe74(%rip),%r15
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %ebp,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     123c <split_str_to_array+0xec>
mov    %rax,%rbx
mov    %r14,(%rax,%r12,8)
xor    %edi,%edi
mov    %r15,%rsi
call   1060 <strtok@plt>
mov    %rax,%r14
add    $0x8,%ebp
add    $0x1,%r12
test   %rax,%rax
jne    11a0 <split_str_to_array+0x50>
jmp    11da <split_str_to_array+0x8a>
xor    %ebx,%ebx
xor    %r12d,%r12d
lea    0x8(,%r12,8),%esi
mov    %rbx,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbx
mov    %r12d,%eax
movq   $0x0,(%rbx,%rax,8)
add    $0x1,%r12d
xor    %ebp,%ebp
lea    0xe09(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%rbx,%rbp,8),%rdx
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1210 <split_str_to_array+0xc0>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@__const.split_str_to_array.str = [12 x i8] c"Hello world\00"
@.str = [2 x i8] c" \00"
@.str.1 = [14 x i8] c"res[%d] = %s\0A\00"
define void @split_str_to_array() {
%1 = alloca [12 x i8]
%2 = alloca i8**
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = bitcast [12 x i8]* %1 to i8*
store i8** null, i8*** %2
%7 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%8 = call i8* @strtok(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %8, i8** %3
store i32 0, i32* %4
br label %9
9: ; preds = %24, %0
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %32
12: ; preds = %9
%13 = load i8**, i8*** %2
%14 = load i32, i32* %4
%15 = add nsw i32 %14, 1
store i32 %15, i32* %4
%16 = sext i32 %15 to i64
%17 = mul i64 8, %16
%18 = trunc i64 %17 to i32
%19 = call i8** @realloc(i8** %13, i32 %18)
store i8** %19, i8*** %2
%20 = load i8**, i8*** %2
%21 = icmp eq i8** %20, null
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %12
%25 = load i8*, i8** %3
%26 = load i8**, i8*** %2
%27 = load i32, i32* %4
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8*, i8** %26, i64 %29
store i8* %25, i8** %30
%31 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %31, i8** %3
br label %9
32: ; preds = %9
%33 = load i8**, i8*** %2
%34 = load i32, i32* %4
%35 = add nsw i32 %34, 1
%36 = sext i32 %35 to i64
%37 = mul i64 8, %36
%38 = trunc i64 %37 to i32
%39 = call i8** @realloc(i8** %33, i32 %38)
store i8** %39, i8*** %2
%40 = load i8**, i8*** %2
%41 = load i32, i32* %4
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %40, i64 %42
store i8* null, i8** %43
store i32 0, i32* %5
br label %44
44: ; preds = %57, %32
%45 = load i32, i32* %5
%46 = load i32, i32* %4
%47 = add nsw i32 %46, 1
%48 = icmp slt i32 %45, %47
br i1 %48, label %49, label %60
49: ; preds = %44
%50 = load i32, i32* %5
%51 = load i8**, i8*** %2
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8*, i8** %51, i64 %53
%55 = load i8*, i8** %54
%56 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %50, i8* %55)
br label %57
57: ; preds = %49
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
store i32 %59, i32* %5
br label %44
60: ; preds = %44
%61 = load i8**, i8*** %2
%62 = call i32 @free(i8** %61)
ret void
}

LLVM IR (-O2):
@__const.split_str_to_array.str = [12 x i8] c"Hello world\00"
@.str = [2 x i8] c" \00"
@.str.1 = [14 x i8] c"res[%d] = %s\0A\00"
define void @split_str_to_array() {
%1 = alloca [12 x i8]
%2 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%3 = call i8* @strtok(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %22, label %5
5: ; preds = %0, %16
%6 = phi i64 [ %9, %16 ], [ 0, %0 ]
%7 = phi i8* [ %18, %16 ], [ %3, %0 ]
%8 = phi i8** [ %12, %16 ], [ null, %0 ]
%9 = add nuw i64 %6, 1
%10 = trunc i64 %9 to i32
%11 = shl i32 %10, 3
%12 = call i8** @realloc(i8** %8, i32 %11)
%13 = icmp eq i8** %12, null
br i1 %13, label %14, label %16
14: ; preds = %5
%15 = call i32 @exit(i32 -1)
unreachable
16: ; preds = %5
%17 = getelementptr inbounds i8*, i8** %12, i64 %6
store i8* %7, i8** %17
%18 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %5
20: ; preds = %16
%21 = trunc i64 %9 to i32
br label %22
22: ; preds = %20, %0
%23 = phi i8** [ null, %0 ], [ %12, %20 ]
%24 = phi i32 [ 0, %0 ], [ %21, %20 ]
%25 = shl i32 %24, 3
%26 = add i32 %25, 8
%27 = call i8** @realloc(i8** %23, i32 %26)
%28 = zext i32 %24 to i64
%29 = getelementptr inbounds i8*, i8** %27, i64 %28
store i8* null, i8** %29
%30 = add i32 %24, 1
%31 = zext i32 %30 to i64
br label %32
32: ; preds = %22, %32
%33 = phi i64 [ 0, %22 ], [ %38, %32 ]
%34 = getelementptr inbounds i8*, i8** %27, i64 %33
%35 = load i8*, i8** %34
%36 = trunc i64 %33 to i32
%37 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %36, i8* %35)
%38 = add nuw nsw i64 %33, 1
%39 = icmp eq i64 %38, %31
br i1 %39, label %40, label %32
40: ; preds = %32
%41 = call i32 @free(i8** %27)
ret void
}