#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*/ error();
char getopt(int, char **, char *);
char *optarg;
scalar_t__ optind;
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int counter = 0;
  char o;
  while ((o = getopt(argc, argv, "ab:")) != -1) {
    switch (o) {
    case 'a':
      printf("Option -a activated\n");
      break;
    case 'b':
      printf("Option -b activated with argument: %s.\n", optarg);
      break;
    default:
      error();
    }
  }
  argv += optind;
  argc -= optind;
  while (counter < argc)
    printf("Remaining arg:  %s\n", argv[counter++]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xea5(%rip),%rdx
call   1050 <getopt@plt>
mov    %al,-0x15(%rbp)
movsbl %al,%eax
cmp    $0xffffffff,%eax
je     11d1 <main+0xa1>
movsbl -0x15(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x61,%eax
je     1195 <main+0x65>
jmp    1184 <main+0x54>
mov    -0x1c(%rbp),%eax
sub    $0x62,%eax
je     11a8 <main+0x78>
jmp    11c5 <main+0x95>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11cc <main+0x9c>
mov    0x2e49(%rip),%rax
mov    (%rax),%rsi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11cc <main+0x9c>
mov    $0x0,%al
call   1040 <error@plt>
jmp    114d <main+0x1d>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1230 <main+0x100>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fb <main+0xcb>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xea9(%rip),%rdx
call   1060 <getopt@plt>
cmp    $0xff,%al
je     11c0 <main+0x80>
mov    0x2e91(%rip),%rbp
lea    0xe96(%rip),%r15
lea    0xe8b(%rip),%r12
lea    0xec4(%rip),%r13
jmp    1198 <main+0x58>
xchg   %ax,%ax
xor    %eax,%eax
call   1050 <error@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   1060 <getopt@plt>
cmp    $0xff,%al
je     11c0 <main+0x80>
movsbl %al,%eax
cmp    $0x62,%eax
je     11b0 <main+0x70>
cmp    $0x61,%eax
jne    1180 <main+0x40>
mov    %r13,%rdi
call   1030 <puts@plt>
jmp    1187 <main+0x47>
nop
mov    0x0(%rbp),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1187 <main+0x47>
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
sub    %eax,%ebx
test   %ebx,%ebx
jle    11f7 <main+0xb7>
lea    (%r14,%rax,8),%r15
mov    %ebx,%ebx
lea    0xe4f(%rip),%r14
xor    %ebp,%ebp
nop
mov    (%r15,%rbp,8),%rsi
add    $0x1,%rbp
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    %rbp,%rbx
jne    11e0 <main+0xa0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"ab:\00"
@.str.1 = [21 x i8] c"Option -a activated\0A\00"
@.str.2 = [40 x i8] c"Option -b activated with argument: %s.\0A\00"
@optarg = global i8* null
@optind = global i64 0
@.str.3 = [20 x i8] c"Remaining arg: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
br label %8
8: ; preds = %24, %2
%9 = load i32, i32* %4
%10 = load i8**, i8*** %5
%11 = call signext i8 @getopt(i32 %9, i8** %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8 %11, i8* %7
%12 = sext i8 %11 to i32
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %25
14: ; preds = %8
%15 = load i8, i8* %7
%16 = sext i8 %15 to i32
switch i32 %16, label %22 [
i32 97, label %17
i32 98, label %19
]
17: ; preds = %14
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %24
19: ; preds = %14
%20 = load i8*, i8** @optarg
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %20)
br label %24
22: ; preds = %14
%23 = call i32 (...) @error()
br label %24
24: ; preds = %22, %19, %17
br label %8
25: ; preds = %8
%26 = load i64, i64* @optind
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 %26
store i8** %28, i8*** %5
%29 = load i64, i64* @optind
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = sub nsw i64 %31, %29
%33 = trunc i64 %32 to i32
store i32 %33, i32* %4
br label %34
34: ; preds = %38, %25
%35 = load i32, i32* %6
%36 = load i32, i32* %4
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %46
38: ; preds = %34
%39 = load i8**, i8*** %5
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
%42 = sext i32 %40 to i64
%43 = getelementptr inbounds i8*, i8** %39, i64 %42
%44 = load i8*, i8** %43
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %44)
br label %34
46: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"ab:\00"
@.str.2 = [40 x i8] c"Option -b activated with argument: %s.\0A\00"
@optarg = global i8* null
@optind = global i64 0
@.str.3 = [20 x i8] c"Remaining arg: %s\0A\00"
@str = [20 x i8] c"Option -a activated\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call signext i8 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i8 %3, -1
br i1 %4, label %18, label %5
5: ; preds = %2, %15
%6 = phi i8 [ %16, %15 ], [ %3, %2 ]
%7 = sext i8 %6 to i32
switch i32 %7, label %13 [
i32 97, label %8
i32 98, label %10
]
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %15
10: ; preds = %5
%11 = load i8*, i8** @optarg
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %11)
br label %15
13: ; preds = %5
%14 = tail call i32 (...) @error()
br label %15
15: ; preds = %13, %10, %8
%16 = tail call signext i8 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i8 %16, -1
br i1 %17, label %18, label %5
18: ; preds = %15, %2
%19 = load i64, i64* @optind
%20 = getelementptr inbounds i8*, i8** %1, i64 %19
%21 = trunc i64 %19 to i32
%22 = sub i32 %0, %21
%23 = icmp sgt i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %18
%25 = zext i32 %22 to i64
br label %26
26: ; preds = %24, %26
%27 = phi i64 [ 0, %24 ], [ %28, %26 ]
%28 = add nuw nsw i64 %27, 1
%29 = getelementptr inbounds i8*, i8** %20, i64 %27
%30 = load i8*, i8** %29
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = icmp eq i64 %28, %25
br i1 %32, label %33, label %26
33: ; preds = %26, %18
ret i32 0
}