#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 *);
char *strtok(char *, char *);
int getdims(int *dims, char *arg) {
  char *s;
  int ndims = 0;
  while ((s = strtok(arg, ","))) {
    dims[ndims++] = atoi(s);
    arg = NULL;
  }
  return ndims;
}


x86 assembly (clang, -O0):
<getdims>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xebe(%rip),%rsi
call   1030 <strtok@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1182 <getdims+0x62>
mov    -0x18(%rbp),%rdi
call   1040 <atoi@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
movq   $0x0,-0x10(%rbp)
jmp    1137 <getdims+0x17>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdims>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xece(%rip),%rsi
mov    %rax,%rdi
call   1030 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     116f <getdims+0x4f>
lea    0xeb8(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
call   1040 <atoi@plt>
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r15,%rsi
call   1030 <strtok@plt>
test   %rax,%rax
jne    1150 <getdims+0x30>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c",\00"
define i32 @getdims(i32* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %3
store i8* %1, i8** %4
store i32 0, i32* %6
br label %7
7: ; preds = %11, %2
%8 = load i8*, i8** %4
%9 = call i8* @strtok(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %19
11: ; preds = %7
%12 = load i8*, i8** %5
%13 = call i32 @atoi(i8* %12)
%14 = load i32*, i32** %3
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
store i32 %16, i32* %6
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i32, i32* %14, i64 %17
store i32 %13, i32* %18
store i8* null, i8** %4
br label %7
19: ; preds = %7
%20 = load i32, i32* %6
ret i32 %20
}

LLVM IR (-O2):
@.str = [2 x i8] c",\00"
define i32 @getdims(i32* nocapture writeonly %0, i8* %1) {
%3 = tail call i8* @strtok(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8* %3, null
br i1 %4, label %15, label %5
5: ; preds = %2, %5
%6 = phi i64 [ %9, %5 ], [ 0, %2 ]
%7 = phi i8* [ %11, %5 ], [ %3, %2 ]
%8 = tail call i32 @atoi(i8* %7)
%9 = add nuw i64 %6, 1
%10 = getelementptr inbounds i32, i32* %0, i64 %6
store i32 %8, i32* %10
%11 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %5
13: ; preds = %5
%14 = trunc i64 %9 to i32
br label %15
15: ; preds = %13, %2
%16 = phi i32 [ 0, %2 ], [ %14, %13 ]
ret i32 %16
}