#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 atoi(char *);
int binaryToDec(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  if (argc < 3) {
    printf("Error\n Usage: ./convert flag value\n");
    return -1;
  }
  char flag = *argv[1];
  if (flag == 'b') {
    int value = atoi(argv[2]);
    if (value == 0) {
      printf("The initial value was: %d. The binary value is 0\n", value);
      return 0;
    }
    printf("The initial value was: %d. The binary value is: ", value);
    char binary[512];
    int i = 512;
    while (value > 0) {
      binary[i] = value % 2 == 0 ? '0' : '1';
      i--;
      value /= 2;
    }
    printf("%s\n", binary + i + 1);
    return 0;
  } else if (flag == 'd') {
    char *binary = argv[2];
    int decimal = binaryToDec(binary);
    printf("%d\n", decimal);
    return 0;
  }
  return 0;
}


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   $0x3,-0x8(%rbp)
jge    116d <main+0x3d>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b9 <main+0x189>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x62,%eax
jne    125f <main+0x12f>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1050 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11be <main+0x8e>
mov    -0x18(%rbp),%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b9 <main+0x189>
mov    -0x18(%rbp),%esi
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x200,-0x224(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1230 <main+0x100>
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    $0x31,%eax
mov    $0x30,%ecx
cmp    $0x0,%edx
cmove  %ecx,%eax
mov    %al,%cl
movslq -0x224(%rbp),%rax
mov    %cl,-0x220(%rbp,%rax,1)
mov    -0x224(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x224(%rbp)
mov    -0x18(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
jmp    11d9 <main+0xa9>
lea    -0x220(%rbp),%rsi
movslq -0x224(%rbp),%rax
add    %rax,%rsi
add    $0x1,%rsi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b9 <main+0x189>
movsbl -0x11(%rbp),%eax
cmp    $0x64,%eax
jne    12ad <main+0x17d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x230(%rbp)
mov    -0x230(%rbp),%rdi
call   1040 <binaryToDec@plt>
mov    %eax,-0x234(%rbp)
mov    -0x234(%rbp),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b9 <main+0x189>
jmp    12b2 <main+0x182>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x208,%rsp
cmp    $0x2,%edi
jg     1164 <main+0x24>
lea    0xf12(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
jmp    11ef <main+0xaf>
mov    0x8(%rsi),%rax
mov    (%rax),%al
cmp    $0x64,%al
je     11c7 <main+0x87>
xor    %ebx,%ebx
cmp    $0x62,%al
jne    11ef <main+0xaf>
mov    0x10(%rsi),%rdi
call   1060 <atoi@plt>
test   %eax,%eax
je     11dd <main+0x9d>
mov    %eax,%ebp
lea    0xea8(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x200,%eax
test   %ebp,%ebp
jle    11b8 <main+0x78>
mov    %ebp,%ecx
mov    %ebp,%edx
and    $0x1,%dl
or     $0x30,%dl
mov    %dl,(%rsp,%rax,1)
add    $0xffffffffffffffff,%rax
shr    %ecx
cmp    $0x1,%ebp
mov    %ecx,%ebp
ja     11a0 <main+0x60>
lea    (%rsp,%rax,1),%rdi
add    $0x1,%rdi
call   1030 <puts@plt>
jmp    11ef <main+0xaf>
mov    0x10(%rsi),%rdi
call   1050 <binaryToDec@plt>
lea    0xe8c(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
jmp    11e8 <main+0xa8>
lea    0xe1c(%rip),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x208,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Error\0A Usage: ./convert flag value\0A\00"
@.str.1 = [50 x i8] c"The initial value was: %d. The binary value is 0\0A\00"
@.str.2 = [49 x i8] c"The initial value was: %d. The binary value is: \00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca [512 x i8]
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %75
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i8, i8* %19
store i8 %20, i8* %6
%21 = load i8, i8* %6
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 98
br i1 %23, label %24, label %61
24: ; preds = %16
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* %7
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i32 0, i32* %3
br label %75
34: ; preds = %24
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %35)
store i32 512, i32* %9
br label %37
37: ; preds = %40, %34
%38 = load i32, i32* %7
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %54
40: ; preds = %37
%41 = load i32, i32* %7
%42 = srem i32 %41, 2
%43 = icmp eq i32 %42, 0
%44 = zext i1 %43 to i64
%45 = select i1 %43, i32 48, i32 49
%46 = trunc i32 %45 to i8
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 %48
store i8 %46, i8* %49
%50 = load i32, i32* %9
%51 = add nsw i32 %50, -1
store i32 %51, i32* %9
%52 = load i32, i32* %7
%53 = sdiv i32 %52, 2
store i32 %53, i32* %7
br label %37
54: ; preds = %37
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%56 = load i32, i32* %9
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = getelementptr inbounds i8, i8* %58, i64 1
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %59)
store i32 0, i32* %3
br label %75
61: ; preds = %16
%62 = load i8, i8* %6
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 100
br i1 %64, label %65, label %73
65: ; preds = %61
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 2
%68 = load i8*, i8** %67
store i8* %68, i8** %10
%69 = load i8*, i8** %10
%70 = call i32 @binaryToDec(i8* %69)
store i32 %70, i32* %11
%71 = load i32, i32* %11
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %71)
store i32 0, i32* %3
br label %75
73: ; preds = %61
br label %74
74: ; preds = %73
store i32 0, i32* %3
br label %75
75: ; preds = %74, %65, %54, %31, %14
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str.1 = [50 x i8] c"The initial value was: %d. The binary value is 0\0A\00"
@.str.2 = [49 x i8] c"The initial value was: %d. The binary value is: \00"
@.str.4 = [4 x i8] c"%d\0A\00"
@str = [35 x i8] c"Error\0A Usage: ./convert flag value\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %45
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i8, i8* %9
switch i8 %10, label %45 [
i8 98, label %11
i8 100, label %40
]
11: ; preds = %7
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %45
18: ; preds = %11
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%21 = icmp sgt i32 %14, 0
br i1 %21, label %22, label %35
22: ; preds = %18, %22
%23 = phi i64 [ %29, %22 ], [ 512, %18 ]
%24 = phi i32 [ %30, %22 ], [ %14, %18 ]
%25 = trunc i32 %24 to i8
%26 = and i8 %25, 1
%27 = or i8 %26, 48
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %23
store i8 %27, i8* %28
%29 = add nsw i64 %23, -1
%30 = lshr i32 %24, 1
%31 = icmp ult i32 %24, 2
br i1 %31, label %32, label %22
32: ; preds = %22
%33 = shl i64 %29, 32
%34 = ashr exact i64 %33, 32
br label %35
35: ; preds = %32, %18
%36 = phi i64 [ 512, %18 ], [ %34, %32 ]
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %36
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = call i32 @puts(i8* %38)
br label %45
40: ; preds = %7
%41 = getelementptr inbounds i8*, i8** %1, i64 2
%42 = load i8*, i8** %41
%43 = tail call i32 @binaryToDec(i8* %42)
%44 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %40, %35, %16, %7, %5
%46 = phi i32 [ -1, %5 ], [ 0, %7 ], [ 0, %16 ], [ 0, %35 ], [ 0, %40 ]
ret i32 %46
}