#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 /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ memset(char **, int /*<<< orphan*/, int);
size_t strlen(char *);
char *strsep(char **, char *);
int parse_arg(char *arg, char *buf, char *argv[]) {
  int res = 0;
  size_t argl;
  char *tok;
  char **ap = &buf;
  int i;
  if ((arg == NULL)) {
    return -1;
  }
  argl = strlen(arg);
  if (argl == 0) {
    return res;
  }
  if (argl >= 1024) {
    argl = 1024 - 1;
  }
  memset(argv, 0, 32 * sizeof(void *));
  memcpy(buf, arg, argl);
  buf[argl] = '\0';
  for (i = 0; i < 32; i++) {
    tok = strsep(ap, ":");
    if (tok == NULL) {
      break;
    }
    argv[i] = tok;
    res++;
  }
  return res;
}


x86 assembly (clang, -O0):
<parse_arg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <parse_arg+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <parse_arg+0x102>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119d <parse_arg+0x5d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1242 <parse_arg+0x102>
cmpq   $0x400,-0x30(%rbp)
jb     11b3 <parse_arg+0x73>
movq   $0x3ff,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x44(%rbp)
cmpl   $0x20,-0x44(%rbp)
jge    123c <parse_arg+0xfc>
mov    -0x40(%rbp),%rdi
lea    0xe04(%rip),%rsi
call   1060 <strsep@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1215 <parse_arg+0xd5>
jmp    123c <parse_arg+0xfc>
mov    -0x38(%rbp),%rdx
mov    -0x20(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    11e7 <parse_arg+0xa7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_arg>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,0x8(%rsp)
test   %rdi,%rdi
je     11d5 <parse_arg+0x95>
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1030 <strlen@plt>
test   %rax,%rax
je     11dc <parse_arg+0x9c>
cmp    $0x3ff,%rax
mov    $0x3ff,%r13d
cmovb  %rax,%r13
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1040 <memset@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1050 <memcpy@plt>
movb   $0x0,(%r15,%r13,1)
lea    0xe5b(%rip),%r15
lea    0x8(%rsp),%r12
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <strsep@plt>
test   %rax,%rax
je     11de <parse_arg+0x9e>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    $0x20,%rbx
jne    11b0 <parse_arg+0x70>
mov    $0x20,%ebx
jmp    11de <parse_arg+0x9e>
mov    $0xffffffff,%ebx
jmp    11de <parse_arg+0x9e>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c":\00"
define i32 @parse_arg(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8**
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i32 0, i32* %8
store i8** %6, i8*** %11
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %59
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %59
23: ; preds = %16
%24 = load i64, i64* %9
%25 = icmp uge i64 %24, 1024
br i1 %25, label %26, label %27
26: ; preds = %23
store i64 1023, i64* %9
br label %27
27: ; preds = %26, %23
%28 = load i8**, i8*** %7
%29 = call i32 @memset(i8** %28, i32 0, i32 256)
%30 = load i8*, i8** %6
%31 = load i8*, i8** %5
%32 = load i64, i64* %9
%33 = call i32 @memcpy(i8* %30, i8* %31, i64 %32)
%34 = load i8*, i8** %6
%35 = load i64, i64* %9
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8 0, i8* %36
store i32 0, i32* %12
br label %37
37: ; preds = %54, %27
%38 = load i32, i32* %12
%39 = icmp slt i32 %38, 32
br i1 %39, label %40, label %57
40: ; preds = %37
%41 = load i8**, i8*** %11
%42 = call i8* @strsep(i8** %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %42, i8** %10
%43 = load i8*, i8** %10
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %46
45: ; preds = %40
br label %57
46: ; preds = %40
%47 = load i8*, i8** %10
%48 = load i8**, i8*** %7
%49 = load i32, i32* %12
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8*, i8** %48, i64 %50
store i8* %47, i8** %51
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %54
54: ; preds = %46
%55 = load i32, i32* %12
%56 = add nsw i32 %55, 1
store i32 %56, i32* %12
br label %37
57: ; preds = %45, %37
%58 = load i32, i32* %8
store i32 %58, i32* %4
br label %59
59: ; preds = %57, %21, %15
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
define i32 @parse_arg(i8* %0, i8* %1, i8** %2) {
%4 = alloca i8*
store i8* %1, i8** %4
%5 = icmp eq i8* %0, null
br i1 %5, label %25, label %6
6: ; preds = %3
%7 = tail call i64 @strlen(i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = icmp ult i64 %7, 1023
%11 = select i1 %10, i64 %7, i64 1023
%12 = tail call i32 @memset(i8** %2, i32 0, i32 256)
%13 = tail call i32 @memcpy(i8* %1, i8* %0, i64 %11)
%14 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 0, i8* %14
br label %15
15: ; preds = %9, %19
%16 = phi i64 [ 0, %9 ], [ %21, %19 ]
%17 = call i8* @strsep(i8** %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %23, label %19
19: ; preds = %15
%20 = getelementptr inbounds i8*, i8** %2, i64 %16
store i8* %17, i8** %20
%21 = add nuw nsw i64 %16, 1
%22 = icmp eq i64 %21, 32
br i1 %22, label %25, label %15
23: ; preds = %15
%24 = trunc i64 %16 to i32
br label %25
25: ; preds = %19, %23, %6, %3
%26 = phi i32 [ -1, %3 ], [ 0, %6 ], [ %24, %23 ], [ 32, %19 ]
ret i32 %26
}