#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*/ free(char *);
char **malloc(int);
char *strdup(char *);
char *strtok(char *, char *);
char **array_of_segments(char *buf, char *dl, int i) {
  char *sg = NULL;
  char **a;
  int j = 0;
  char *cbuf = strdup(buf);
  a = malloc(i * sizeof(char *));
  sg = strtok(cbuf, dl);
  do {
    if (j < i) {
      a[j] = strdup(sg);
    }
    ++j;
  } while (sg = strtok(NULL, dl));
  if (cbuf)
    free(cbuf);
  return a;
}


x86 assembly (clang, -O0):
<array_of_segments>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x38(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11b7 <array_of_segments+0x77>
mov    -0x20(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1193 <array_of_segments+0x53>
cmpq   $0x0,-0x38(%rbp)
je     11ef <array_of_segments+0xaf>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<array_of_segments>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r13
call   1060 <strdup@plt>
mov    %rax,%r14
lea    0x0(,%r12,8),%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %r13,%rsi
call   1050 <strtok@plt>
movslq %r12d,%r12
xor    %ebx,%ebx
jmp    1193 <array_of_segments+0x53>
nopl   0x0(%rax)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r13,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
je     11a6 <array_of_segments+0x66>
cmp    %r12,%rbx
jge    1180 <array_of_segments+0x40>
mov    %rax,%rdi
call   1060 <strdup@plt>
mov    %rax,(%r15,%rbx,8)
jmp    1180 <array_of_segments+0x40>
test   %r14,%r14
je     11b3 <array_of_segments+0x73>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8** @array_of_segments(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i8* null, i8** %7
store i32 0, i32* %9
%11 = load i8*, i8** %4
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %10
%13 = load i32, i32* %6
%14 = sext i32 %13 to i64
%15 = mul i64 %14, 8
%16 = trunc i64 %15 to i32
%17 = call i8** @malloc(i32 %16)
store i8** %17, i8*** %8
%18 = load i8*, i8** %10
%19 = load i8*, i8** %5
%20 = call i8* @strtok(i8* %18, i8* %19)
store i8* %20, i8** %7
br label %21
21: ; preds = %35, %3
%22 = load i32, i32* %9
%23 = load i32, i32* %6
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %32
25: ; preds = %21
%26 = load i8*, i8** %7
%27 = call i8* @strdup(i8* %26)
%28 = load i8**, i8*** %8
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
store i8* %27, i8** %31
br label %32
32: ; preds = %25, %21
%33 = load i32, i32* %9
%34 = add nsw i32 %33, 1
store i32 %34, i32* %9
br label %35
35: ; preds = %32
%36 = load i8*, i8** %5
%37 = call i8* @strtok(i8* null, i8* %36)
store i8* %37, i8** %7
%38 = icmp ne i8* %37, null
br i1 %38, label %21, label %39
39: ; preds = %35
%40 = load i8*, i8** %10
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %39
%43 = load i8*, i8** %10
%44 = call i32 @free(i8* %43)
br label %45
45: ; preds = %42, %39
%46 = load i8**, i8*** %8
ret i8** %46
}

LLVM IR (-O2):
define noalias i8** @array_of_segments(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i8* @strdup(i8* %0)
%5 = shl i32 %2, 3
%6 = tail call i8** @malloc(i32 %5)
%7 = tail call i8* @strtok(i8* %4, i8* %1)
%8 = sext i32 %2 to i64
br label %9
9: ; preds = %16, %3
%10 = phi i64 [ %17, %16 ], [ 0, %3 ]
%11 = phi i8* [ %18, %16 ], [ %7, %3 ]
%12 = icmp slt i64 %10, %8
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = tail call i8* @strdup(i8* %11)
%15 = getelementptr inbounds i8*, i8** %6, i64 %10
store i8* %14, i8** %15
br label %16
16: ; preds = %13, %9
%17 = add nuw i64 %10, 1
%18 = tail call i8* @strtok(i8* null, i8* %1)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %9
20: ; preds = %16
%21 = icmp eq i8* %4, null
br i1 %21, label %24, label %22
22: ; preds = %20
%23 = tail call i32 @free(i8* %4)
br label %24
24: ; preds = %22, %20
ret i8** %6
}