#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 atoi(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
char *strtok(char *, char *);
int *string2array(char *input, int *dim) {
  int *result;
  char *s;
  char buffer[1024];
  int n;
  strcpy(buffer, input);
  s = strtok(buffer, " ");
  n = 0;
  do {
    n++;
  } while ((s = strtok(NULL, " ")) != NULL);
  *dim = n;
  result = (int *)malloc(n * sizeof(int));
  strcpy(buffer, input);
  s = strtok(buffer, " ");
  n = 0;
  do {
    result[n] = atoi(s);
    n++;
  } while ((s = strtok(NULL, " ")) != NULL);
  return result;
}


x86 assembly (clang, -O0):
<string2array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x424(%rbp)
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe62(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1184 <string2array+0x44>
mov    -0x424(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movslq -0x424(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x424(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,%edx
mov    -0x18(%rbp),%rax
movslq -0x424(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xdc9(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1204 <string2array+0xc4>
mov    -0x18(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string2array>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    0xe96(%rip),%r12
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <strtok@plt>
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
add    $0x1,%ebp
xor    %edi,%edi
mov    %r12,%rsi
call   1050 <strtok@plt>
add    $0x4,%ebx
test   %rax,%rax
jne    1180 <string2array+0x40>
mov    %ebp,(%r15)
mov    %ebx,%edi
call   1040 <malloc@plt>
mov    %rax,%r15
mov    %rsp,%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
lea    0xe49(%rip),%r14
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <strtok@plt>
mov    %r15,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rdi
call   1060 <atoi@plt>
mov    %eax,(%rbx)
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <strtok@plt>
add    $0x4,%rbx
test   %rax,%rax
jne    11d0 <string2array+0x90>
mov    %r15,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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