#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 */
typedef int /*<<< orphan*/ tmp_str;

/* Variables and functions */
int /*<<< orphan*/ str_clearcopy(char *, char const *, int);
char *strtok(char *, char *);
int prompt_count_args(const char *str, size_t len) {
  char tmp_str[len];
  str_clearcopy(tmp_str, str, sizeof(tmp_str));
  int arg_count = 0;
  char *arg;
  char delim[2] = {' ', '\0'};
  arg = strtok(tmp_str, delim);
  while (arg) {
    arg_count++;
    arg = strtok(NULL, delim);
  }
  return arg_count;
}


x86 assembly (clang, -O0):
<prompt_count_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
mov    %eax,%edx
call   1030 <str_clearcopy@plt>
mov    -0x40(%rbp),%rdi
movl   $0x0,-0x24(%rbp)
mov    0xe8c(%rip),%ax
mov    %ax,-0x32(%rbp)
lea    -0x32(%rbp),%rsi
call   1040 <strtok@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11af <prompt_count_args+0x8f>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
lea    -0x32(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    1185 <prompt_count_args+0x65>
mov    -0x24(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prompt_count_args>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rdx
mov    %rdi,%rsi
mov    %rsp,%rbx
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1030 <str_clearcopy@plt>
movw   $0x20,-0x12(%rbp)
lea    -0x12(%rbp),%rsi
mov    %rbx,%rdi
call   1040 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1182 <prompt_count_args+0x62>
lea    -0x12(%rbp),%r14
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
jne    1170 <prompt_count_args+0x50>
mov    %ebx,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@__const.prompt_count_args.delim = [2 x i8] c" \00"
define i32 @prompt_count_args(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [2 x i8]
store i8* %0, i8** %3
store i64 %1, i64* %4
%10 = load i64, i64* %4
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = load i8*, i8** %3
%14 = trunc i64 %10 to i32
%15 = call i32 @str_clearcopy(i8* %12, i8* %13, i32 %14)
store i32 0, i32* %7
%16 = bitcast [2 x i8]* %9 to i8*
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%18 = call i8* @strtok(i8* %12, i8* %17)
store i8* %18, i8** %8
br label %19
19: ; preds = %22, %2
%20 = load i8*, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %9, i64 0, i64 0
%26 = call i8* @strtok(i8* null, i8* %25)
store i8* %26, i8** %8
br label %19
27: ; preds = %19
%28 = load i32, i32* %7
%29 = load i8*, i8** %5
ret i32 %28
}

LLVM IR (-O2):
define i32 @prompt_count_args(i8* %0, i64 %1) {
%3 = alloca i16
%4 = alloca i8, i64 %1
%5 = trunc i64 %1 to i32
%6 = call i32 @str_clearcopy(i8* %4, i8* %0, i32 %5)
%7 = bitcast i16* %3 to i8*
store i16 32, i16* %3
%8 = call i8* @strtok(i8* %4, i8* %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %15, label %10
10: ; preds = %2, %10
%11 = phi i32 [ %12, %10 ], [ 0, %2 ]
%12 = add nuw nsw i32 %11, 1
%13 = call i8* @strtok(i8* null, i8* %7)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %10
15: ; preds = %10, %2
%16 = phi i32 [ 0, %2 ], [ %12, %10 ]
ret i32 %16
}