#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 *calloc(int, int);
int count_flags(char *);
int count_options(char *);
int p_strlen(char *);
int /*<<< orphan*/ printf(char *, int, int);
void parse_getopt(char *optstring, char **options, char **flags) {
  int option_count;
  int flag_count;
  char *ptr = optstring;
  option_count = count_options(optstring);
  flag_count = count_flags(optstring);
  printf("%d options, %d flags\n", option_count, flag_count);
  *options = calloc(1, option_count + 1);
  *flags = calloc(1, flag_count + 1);
  while (*ptr++) {
    if (*ptr == ':') {
      *(*options + p_strlen(*options)) = *(ptr - 1);
      ;
    } else if (*(ptr - 1) != ':' && *(ptr - 1) != '?' && *(ptr - 1) != 'h') {
      *(*flags + p_strlen(*flags)) = *(ptr - 1);
      ;
    }
  }
  return;
}


x86 assembly (clang, -O0):
<parse_getopt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <count_options@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <count_flags@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe6f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%esi
add    $0x1,%esi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
cmpb   $0x0,(%rax)
je     1294 <parse_getopt+0x144>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3a,%eax
jne    1227 <parse_getopt+0xd7>
mov    -0x28(%rbp),%rax
mov    -0x1(%rax),%al
mov    %al,-0x31(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <p_strlen@plt>
mov    -0x31(%rbp),%dl
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    128f <parse_getopt+0x13f>
mov    -0x28(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x3a,%eax
je     128a <parse_getopt+0x13a>
mov    -0x28(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x3f,%eax
je     128a <parse_getopt+0x13a>
mov    -0x28(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x68,%eax
je     128a <parse_getopt+0x13a>
mov    -0x28(%rbp),%rax
mov    -0x1(%rax),%al
mov    %al,-0x41(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <p_strlen@plt>
mov    -0x41(%rbp),%dl
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    128f <parse_getopt+0x13f>
jmp    11ca <parse_getopt+0x7a>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_getopt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1070 <count_options@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1060 <count_flags@plt>
mov    %eax,%r12d
lea    0xe83(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <printf@plt>
add    $0x1,%ebp
mov    $0x1,%edi
mov    %ebp,%esi
call   1050 <calloc@plt>
mov    %rax,(%r15)
add    $0x1,%r12d
mov    $0x1,%edi
mov    %r12d,%esi
call   1050 <calloc@plt>
mov    %rax,(%r14)
mov    (%rbx),%bpl
test   %bpl,%bpl
je     1212 <parse_getopt+0xc2>
add    $0x1,%rbx
movabs $0x400000000021,%r12
jmp    11ef <parse_getopt+0x9f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rcx),%r13
mov    %r13,%rdi
call   1040 <p_strlen@plt>
cltq
mov    %bpl,0x0(%r13,%rax,1)
movzbl (%rbx),%eax
add    $0x1,%rbx
mov    %eax,%ebp
test   %al,%al
je     1212 <parse_getopt+0xc2>
movzbl (%rbx),%eax
mov    %r15,%rcx
cmp    $0x3a,%al
je     11d0 <parse_getopt+0x80>
lea    -0x3a(%rbp),%edx
mov    %r14,%rcx
cmp    $0x2e,%dl
ja     11d0 <parse_getopt+0x80>
movzbl %dl,%ecx
bt     %rcx,%r12
mov    %r14,%rcx
jb     11e5 <parse_getopt+0x95>
jmp    11d0 <parse_getopt+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"%d options, %d flags\0A\00"
define void @parse_getopt(i8* %0, i8** %1, i8** %2) {
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i8**
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i8** %2, i8*** %6
%10 = load i8*, i8** %4
store i8* %10, i8** %9
%11 = load i8*, i8** %4
%12 = call i32 @count_options(i8* %11)
store i32 %12, i32* %7
%13 = load i8*, i8** %4
%14 = call i32 @count_flags(i8* %13)
store i32 %14, i32* %8
%15 = load i32, i32* %7
%16 = load i32, i32* %8
%17 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
%20 = call i8* @calloc(i32 1, i32 %19)
%21 = load i8**, i8*** %5
store i8* %20, i8** %21
%22 = load i32, i32* %8
%23 = add nsw i32 %22, 1
%24 = call i8* @calloc(i32 1, i32 %23)
%25 = load i8**, i8*** %6
store i8* %24, i8** %25
br label %26
26: ; preds = %77, %3
%27 = load i8*, i8** %9
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %9
%29 = load i8, i8* %27
%30 = icmp ne i8 %29, 0
br i1 %30, label %31, label %78
31: ; preds = %26
%32 = load i8*, i8** %9
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 58
br i1 %35, label %36, label %47
36: ; preds = %31
%37 = load i8*, i8** %9
%38 = getelementptr inbounds i8, i8* %37, i64 -1
%39 = load i8, i8* %38
%40 = load i8**, i8*** %5
%41 = load i8*, i8** %40
%42 = load i8**, i8*** %5
%43 = load i8*, i8** %42
%44 = call i32 @p_strlen(i8* %43)
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
store i8 %39, i8* %46
br label %77
47: ; preds = %31
%48 = load i8*, i8** %9
%49 = getelementptr inbounds i8, i8* %48, i64 -1
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = icmp ne i32 %51, 58
br i1 %52, label %53, label %76
53: ; preds = %47
%54 = load i8*, i8** %9
%55 = getelementptr inbounds i8, i8* %54, i64 -1
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp ne i32 %57, 63
br i1 %58, label %59, label %76
59: ; preds = %53
%60 = load i8*, i8** %9
%61 = getelementptr inbounds i8, i8* %60, i64 -1
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp ne i32 %63, 104
br i1 %64, label %65, label %76
65: ; preds = %59
%66 = load i8*, i8** %9
%67 = getelementptr inbounds i8, i8* %66, i64 -1
%68 = load i8, i8* %67
%69 = load i8**, i8*** %6
%70 = load i8*, i8** %69
%71 = load i8**, i8*** %6
%72 = load i8*, i8** %71
%73 = call i32 @p_strlen(i8* %72)
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %70, i64 %74
store i8 %68, i8* %75
br label %76
76: ; preds = %65, %59, %53, %47
br label %77
77: ; preds = %76, %36
br label %26
78: ; preds = %26
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"%d options, %d flags\0A\00"
define void @parse_getopt(i8* %0, i8** nocapture %1, i8** nocapture %2) {
%4 = tail call i32 @count_options(i8* %0)
%5 = tail call i32 @count_flags(i8* %0)
%6 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %5)
%7 = add nsw i32 %4, 1
%8 = tail call i8* @calloc(i32 1, i32 %7)
store i8* %8, i8** %1
%9 = add nsw i32 %5, 1
%10 = tail call i8* @calloc(i32 1, i32 %9)
store i8* %10, i8** %2
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 0
br i1 %12, label %30, label %13
13: ; preds = %3, %27
%14 = phi i8 [ %28, %27 ], [ %11, %3 ]
%15 = phi i8* [ %16, %27 ], [ %0, %3 ]
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 58
br i1 %18, label %20, label %19
19: ; preds = %13
switch i8 %14, label %20 [
i8 58, label %27
i8 63, label %27
i8 104, label %27
]
20: ; preds = %19, %13
%21 = phi i8** [ %1, %13 ], [ %2, %19 ]
%22 = load i8*, i8** %21
%23 = tail call i32 @p_strlen(i8* %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 %14, i8* %25
%26 = load i8, i8* %16
br label %27
27: ; preds = %20, %19, %19, %19
%28 = phi i8 [ %26, %20 ], [ %17, %19 ], [ %17, %19 ], [ %17, %19 ]
%29 = icmp eq i8 %28, 0
br i1 %29, label %30, label %13
30: ; preds = %27, %3
ret void
}