#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 EXIT_SUCCESS;
int atoi(char *);
int /*<<< orphan*/ free(char *);
int getopt(int, char **, char *);
scalar_t__ malloc(int);
char *optarg;
scalar_t__ optind;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ usage(char *);
int main(int argc, char *argv[]) {
  int opt;
  int dividend;
  char buf[512];
  char *heap;
  if (argc == 1)
    usage(argv[0]);
  printf("This is a buggy program!\n");
  while ((opt = getopt(argc, argv, "b:f:d:h")) != -1) {
    switch (opt) {
    case 'b':
      sprintf(buf, "%s", optarg);
      break;
    case 'f':
      printf(optarg);
      break;
    case 'd':
      printf("1 / %d = %d\n", atoi(optarg), 1 / atoi(optarg));
      break;
    default:
    case 'h':
      usage(argv[0]);
    }
  }
  argc -= optind;
  argv += optind;
  if (argc > 0) {
    heap = (char *)malloc(32);
    strcpy(heap, argv[0]);
    printf("You entered: %s\n", heap);
    free(heap);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    11af <main+0x2f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe6b(%rip),%rdx
call   1080 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     12a2 <main+0x122>
mov    -0x14(%rbp),%eax
add    $0xffffff9e,%eax
mov    %eax,%ecx
mov    %rcx,-0x230(%rbp)
sub    $0x6,%eax
ja     128c <main+0x10c>
mov    -0x230(%rbp),%rax
lea    0xdfe(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    -0x220(%rbp),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%rdx
lea    0xe1b(%rip),%rsi
call   10a0 <sprintf@plt>
jmp    129d <main+0x11d>
mov    0x2dc4(%rip),%rax
mov    (%rax),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    129d <main+0x11d>
mov    0x2dae(%rip),%rax
mov    (%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x234(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%rdi
call   1090 <atoi@plt>
mov    -0x234(%rbp),%esi
mov    %eax,%ecx
mov    $0x1,%eax
cltd
idiv   %ecx
mov    %eax,%edx
lea    0xdc1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    129d <main+0x11d>
jmp    1291 <main+0x111>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <usage@plt>
jmp    11bd <main+0x3d>
mov    0x2d37(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    131b <main+0x19b>
mov    $0x20,%edi
call   1060 <malloc@plt>
mov    %rax,-0x228(%rbp)
mov    -0x228(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
call   1040 <strcpy@plt>
mov    -0x228(%rbp),%rsi
lea    0xd46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x228(%rbp),%rdi
call   1030 <free@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x1,%edi
jne    11a3 <main+0x23>
mov    (%rbx),%rdi
call   1080 <usage@plt>
lea    0xe7c(%rip),%rdi
call   1050 <puts@plt>
lea    0xe4a(%rip),%r14
mov    0x2e3b(%rip),%r13
mov    %rsp,%r15
lea    0xe41(%rip),%r12
jmp    11f3 <main+0x73>
nopl   0x0(%rax)
mov    0x0(%r13),%rdi
call   10a0 <atoi@plt>
lea    0x1(%rax),%ecx
cmp    $0x3,%ecx
mov    $0x0,%edx
cmovb  %eax,%edx
mov    %r12,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getopt@plt>
cmp    $0x63,%eax
jle    1220 <main+0xa0>
cmp    $0x64,%eax
je     11d0 <main+0x50>
cmp    $0x66,%eax
jne    1238 <main+0xb8>
mov    0x0(%r13),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11f3 <main+0x73>
nopl   0x0(%rax)
cmp    $0x62,%eax
jne    1233 <main+0xb3>
mov    0x0(%r13),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
jmp    11f3 <main+0x73>
cmp    $0xffffffff,%eax
je     1242 <main+0xc2>
mov    (%rbx),%rdi
call   1080 <usage@plt>
jmp    11f3 <main+0x73>
mov    0x2d97(%rip),%rax
mov    (%rax),%r14
sub    %r14d,%ebp
test   %ebp,%ebp
jle    1285 <main+0x105>
mov    $0x20,%edi
call   1070 <malloc@plt>
mov    %rax,%rbp
mov    (%rbx,%r14,8),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
lea    0xda2(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"This is a buggy program!\0A\00"
@.str.1 = [8 x i8] c"b:f:d:h\00"
@.str.2 = [3 x i8] c"%s\00"
@optarg = global i8* null
@.str.3 = [13 x i8] c"1 / %d = %d\0A\00"
@optind = global i64 0
@.str.4 = [17 x i8] c"You entered: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @usage(i8* %15)
br label %17
17: ; preds = %12, %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %46, %17
%20 = load i32, i32* %4
%21 = load i8**, i8*** %5
%22 = call i32 @getopt(i32 %20, i8** %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %22, i32* %6
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %47
24: ; preds = %19
%25 = load i32, i32* %6
switch i32 %25, label %40 [
i32 98, label %26
i32 102, label %30
i32 100, label %33
i32 104, label %41
]
26: ; preds = %24
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%28 = load i8*, i8** @optarg
%29 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %46
30: ; preds = %24
%31 = load i8*, i8** @optarg
%32 = call i32 (i8*, ...) @printf(i8* %31)
br label %46
33: ; preds = %24
%34 = load i8*, i8** @optarg
%35 = call i32 @atoi(i8* %34)
%36 = load i8*, i8** @optarg
%37 = call i32 @atoi(i8* %36)
%38 = sdiv i32 1, %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %35, i32 %38)
br label %46
40: ; preds = %24
br label %41
41: ; preds = %24, %40
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 0
%44 = load i8*, i8** %43
%45 = call i32 @usage(i8* %44)
br label %46
46: ; preds = %41, %33, %30, %26
br label %19
47: ; preds = %19
%48 = load i64, i64* @optind
%49 = load i32, i32* %4
%50 = sext i32 %49 to i64
%51 = sub nsw i64 %50, %48
%52 = trunc i64 %51 to i32
store i32 %52, i32* %4
%53 = load i64, i64* @optind
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 %53
store i8** %55, i8*** %5
%56 = load i32, i32* %4
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %70
58: ; preds = %47
%59 = call i64 @malloc(i32 32)
%60 = inttoptr i64 %59 to i8*
store i8* %60, i8** %9
%61 = load i8*, i8** %9
%62 = load i8**, i8*** %5
%63 = getelementptr inbounds i8*, i8** %62, i64 0
%64 = load i8*, i8** %63
%65 = call i32 @strcpy(i8* %61, i8* %64)
%66 = load i8*, i8** %9
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %66)
%68 = load i8*, i8** %9
%69 = call i32 @free(i8* %68)
br label %70
70: ; preds = %58, %47
%71 = load i32, i32* @EXIT_SUCCESS
ret i32 %71
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"b:f:d:h\00"
@optarg = global i8* null
@.str.3 = [13 x i8] c"1 / %d = %d\0A\00"
@optind = global i64 0
@.str.4 = [17 x i8] c"You entered: %s\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [25 x i8] c"This is a buggy program!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 1
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @usage(i8* %7)
br label %9
9: ; preds = %6, %2
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %29, %9
%12 = call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
switch i32 %12, label %26 [
i32 -1, label %30
i32 98, label %13
i32 102, label %16
i32 100, label %19
]
13: ; preds = %11
%14 = load i8*, i8** @optarg
%15 = call i8* bitcast (i32 (i8*, i8*)* @strcpy to i8* (i8*, i8*)*)(i8* %4, i8* %14)
br label %29
16: ; preds = %11
%17 = load i8*, i8** @optarg
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %17)
br label %29
19: ; preds = %11
%20 = load i8*, i8** @optarg
%21 = call i32 @atoi(i8* %20)
%22 = add i32 %21, 1
%23 = icmp ult i32 %22, 3
%24 = select i1 %23, i32 %21, i32 0
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %21, i32 %24)
br label %29
26: ; preds = %11
%27 = load i8*, i8** %1
%28 = call i32 @usage(i8* %27)
br label %29
29: ; preds = %26, %19, %16, %13
br label %11
30: ; preds = %11
%31 = load i64, i64* @optind
%32 = trunc i64 %31 to i32
%33 = sub i32 %0, %32
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %30
%36 = getelementptr inbounds i8*, i8** %1, i64 %31
%37 = call i64 @malloc(i32 32)
%38 = inttoptr i64 %37 to i8*
%39 = load i8*, i8** %36
%40 = call i32 @strcpy(i8* %38, i8* %39)
%41 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%42 = call i32 @free(i8* %38)
br label %43
43: ; preds = %35, %30
%44 = load i32, i32* @EXIT_SUCCESS
ret i32 %44
}