#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 */
char *strtok(char *, char *);
int stub1(int, char **);
int buf_args(char *buf, int (*optfunc)(int, char **)) {
  char *ptr, *argv[50];
  int argc;
  if (strtok(buf, " \t\n") == NULL)
    return -1;
  argv[argc = 0] = buf;
  while ((ptr = strtok(NULL, " \t\n")) != NULL) {
    if (++argc >= 50 - 1)
      return -1;
    argv[argc] = ptr;
  }
  argv[++argc] = NULL;
  return (*optfunc)(argc, argv);
}


x86 assembly (clang, -O0):
<buf_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xed2(%rip),%rsi
call   1030 <strtok@plt>
cmp    $0x0,%rax
jne    1149 <buf_args+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <buf_args+0xdb>
mov    -0x10(%rbp),%rax
movl   $0x0,-0x1b4(%rbp)
mov    %rax,-0x1b0(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe97(%rip),%rsi
call   1030 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11b8 <buf_args+0xa8>
mov    -0x1b4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b4(%rbp)
cmp    $0x31,%eax
jl     11a0 <buf_args+0x90>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <buf_args+0xdb>
mov    -0x20(%rbp),%rcx
movslq -0x1b4(%rbp),%rax
mov    %rcx,-0x1b0(%rbp,%rax,8)
ff
jmp    115e <buf_args+0x4e>
mov    -0x1b4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1b4(%rbp)
cltq
movq   $0x0,-0x1b0(%rbp,%rax,8)
mov    -0x18(%rbp),%rax
mov    -0x1b4(%rbp),%edi
lea    -0x1b0(%rbp),%rsi
call   *%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_args>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x198,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xed6(%rip),%rsi
call   1030 <strtok@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1199 <buf_args+0x89>
mov    %rbx,(%rsp)
lea    0xebb(%rip),%rsi
xor    %edi,%edi
call   1030 <strtok@plt>
test   %rax,%rax
je     117f <buf_args+0x6f>
mov    $0x1,%ebx
lea    0xea3(%rip),%rbp
nopl   (%rax)
cmp    $0x31,%rbx
je     1199 <buf_args+0x89>
mov    %rax,(%rsp,%rbx,8)
add    $0x1,%rbx
xor    %edi,%edi
mov    %rbp,%rsi
call   1030 <strtok@plt>
test   %rax,%rax
jne    1160 <buf_args+0x50>
jmp    1184 <buf_args+0x74>
mov    $0x1,%ebx
mov    %ebx,%eax
movq   $0x0,(%rsp,%rax,8)
mov    %rsp,%rsi
mov    %ebx,%edi
call   *%r14
mov    %eax,%r15d
mov    %r15d,%eax
add    $0x198,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c" \09\0A\00"
define i32 @buf_args(i8* %0, i32 (i32, i8**)* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32 (i32, i8**)*
%6 = alloca i8*
%7 = alloca [50 x i8*]
%8 = alloca i32
store i8* %0, i8** %4
store i32 (i32, i8**)* %1, i32 (i32, i8**)** %5
%9 = load i8*, i8** %4
%10 = call i8* @strtok(i8* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %38
13: ; preds = %2
%14 = load i8*, i8** %4
store i32 0, i32* %8
%15 = getelementptr inbounds [50 x i8*], [50 x i8*]* %7, i64 0, i64 0
store i8* %14, i8** %15
br label %16
16: ; preds = %24, %13
%17 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = load i32, i32* %8
%21 = add nsw i32 %20, 1
store i32 %21, i32* %8
%22 = icmp sge i32 %21, 49
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 -1, i32* %3
br label %38
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [50 x i8*], [50 x i8*]* %7, i64 0, i64 %27
store i8* %25, i8** %28
br label %16
29: ; preds = %16
%30 = load i32, i32* %8
%31 = add nsw i32 %30, 1
store i32 %31, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [50 x i8*], [50 x i8*]* %7, i64 0, i64 %32
store i8* null, i8** %33
%34 = load i32 (i32, i8**)*, i32 (i32, i8**)** %5
%35 = load i32, i32* %8
%36 = getelementptr inbounds [50 x i8*], [50 x i8*]* %7, i64 0, i64 0
%37 = call i32 %34(i32 %35, i8** %36)
store i32 %37, i32* %3
br label %38
38: ; preds = %29, %23, %12
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
@.str = [4 x i8] c" \09\0A\00"
define i32 @buf_args(i8* %0, i32 (i32, i8**)* nocapture %1) {
%3 = alloca [50 x i8*]
%4 = bitcast [50 x i8*]* %3 to i8*
%5 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %27, label %7
7: ; preds = %2
%8 = getelementptr inbounds [50 x i8*], [50 x i8*]* %3, i64 0, i64 0
store i8* %0, i8** %8
%9 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %22, label %11
11: ; preds = %7, %15
%12 = phi i64 [ %17, %15 ], [ 1, %7 ]
%13 = phi i8* [ %18, %15 ], [ %9, %7 ]
%14 = icmp eq i64 %12, 49
br i1 %14, label %27, label %15
15: ; preds = %11
%16 = getelementptr inbounds [50 x i8*], [50 x i8*]* %3, i64 0, i64 %12
store i8* %13, i8** %16
%17 = add nuw nsw i64 %12, 1
%18 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %11
20: ; preds = %15
%21 = trunc i64 %17 to i32
br label %22
22: ; preds = %20, %7
%23 = phi i32 [ 1, %7 ], [ %21, %20 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds [50 x i8*], [50 x i8*]* %3, i64 0, i64 %24
store i8* null, i8** %25
%26 = call i32 %1(i32 %23, i8** %8)
br label %27
27: ; preds = %11, %2, %22
%28 = phi i32 [ %26, %22 ], [ -1, %2 ], [ -1, %11 ]
ret i32 %28
}