#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, int, char **);
int buf_args(char *buf, int clifd, int (*optfunc)(int, 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)(clifd, argc, argv));
}


x86 assembly (clang, -O0):
<buf_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1d0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xecf(%rip),%rsi
call   1030 <strtok@plt>
cmp    $0x0,%rax
jne    114c <buf_args+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <buf_args+0xe1>
mov    -0x10(%rbp),%rax
movl   $0x0,-0x1c4(%rbp)
mov    %rax,-0x1c0(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xe94(%rip),%rsi
call   1030 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11bb <buf_args+0xab>
mov    -0x1c4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c4(%rbp)
cmp    $0x31,%eax
jl     11a3 <buf_args+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f1 <buf_args+0xe1>
mov    -0x28(%rbp),%rcx
movslq -0x1c4(%rbp),%rax
mov    %rcx,-0x1c0(%rbp,%rax,8)
ff
jmp    1161 <buf_args+0x51>
mov    -0x1c4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c4(%rbp)
cltq
movq   $0x0,-0x1c0(%rbp,%rax,8)
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%edi
mov    -0x1c4(%rbp),%esi
lea    -0x1c0(%rbp),%rdx
call   *%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<buf_args>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x190,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xed1(%rip),%rsi
call   1030 <strtok@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     11ac <buf_args+0x9c>
mov    %rbx,(%rsp)
lea    0xeb6(%rip),%rsi
xor    %edi,%edi
call   1030 <strtok@plt>
test   %rax,%rax
je     118f <buf_args+0x7f>
mov    $0x1,%ebp
lea    0xe9e(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x31,%rbp
je     11ac <buf_args+0x9c>
mov    %rax,(%rsp,%rbp,8)
add    $0x1,%rbp
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <strtok@plt>
test   %rax,%rax
jne    1170 <buf_args+0x60>
jmp    1194 <buf_args+0x84>
mov    $0x1,%ebp
mov    %ebp,%eax
movq   $0x0,(%rsp,%rax,8)
mov    %rsp,%rdx
mov    %r15d,%edi
mov    %ebp,%esi
call   *%r14
mov    %eax,%r12d
mov    %r12d,%eax
add    $0x190,%rsp
pop    %rbx
pop    %r12
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 %1, i32 (i32, i32, i8**)* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32 (i32, i32, i8**)*
%8 = alloca i8*
%9 = alloca [50 x i8*]
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 (i32, i32, i8**)* %2, i32 (i32, i32, i8**)** %7
%11 = load i8*, i8** %5
%12 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %41
15: ; preds = %3
%16 = load i8*, i8** %5
store i32 0, i32* %10
%17 = getelementptr inbounds [50 x i8*], [50 x i8*]* %9, i64 0, i64 0
store i8* %16, i8** %17
br label %18
18: ; preds = %26, %15
%19 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %19, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = load i32, i32* %10
%23 = add nsw i32 %22, 1
store i32 %23, i32* %10
%24 = icmp sge i32 %23, 49
br i1 %24, label %25, label %26
25: ; preds = %21
store i32 -1, i32* %4
br label %41
26: ; preds = %21
%27 = load i8*, i8** %8
%28 = load i32, i32* %10
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [50 x i8*], [50 x i8*]* %9, i64 0, i64 %29
store i8* %27, i8** %30
br label %18
31: ; preds = %18
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [50 x i8*], [50 x i8*]* %9, i64 0, i64 %34
store i8* null, i8** %35
%36 = load i32 (i32, i32, i8**)*, i32 (i32, i32, i8**)** %7
%37 = load i32, i32* %6
%38 = load i32, i32* %10
%39 = getelementptr inbounds [50 x i8*], [50 x i8*]* %9, i64 0, i64 0
%40 = call i32 %36(i32 %37, i32 %38, i8** %39)
store i32 %40, i32* %4
br label %41
41: ; preds = %31, %25, %14
%42 = load i32, i32* %4
ret i32 %42
}

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