#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 /*<<< orphan*/ binaryToDecimal(int, char *);
int /*<<< orphan*/ exit(int);
int getchar();
int /*<<< orphan*/ printf(char *);
int main() {
  char buffer[50];
  printf("Enter binary: ");
  int i = 0, ch;
  while (ch = getchar()) {
    buffer[i] = ch;
    int check = buffer[i] - '0';
    if (ch == '\n') {
      buffer[i] = '\0';
      break;
    } else if (check > 1 || check < 0) {
      printf("Not a binary!");
      exit(1);
    }
    i++;
  }
  binaryToDecimal(i, buffer);
  return (EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x44(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
je     11e6 <main+0xa6>
mov    -0x48(%rbp),%eax
mov    %al,%cl
movslq -0x44(%rbp),%rax
mov    %cl,-0x40(%rbp,%rax,1)
movslq -0x44(%rbp),%rax
movsbl -0x40(%rbp,%rax,1),%eax
sub    $0x30,%eax
mov    %eax,-0x4c(%rbp)
cmpl   $0xa,-0x48(%rbp)
jne    11a9 <main+0x69>
movslq -0x44(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
jmp    11e6 <main+0xa6>
cmpl   $0x1,-0x4c(%rbp)
jg     11bd <main+0x7d>
cmpl   $0x0,-0x4c(%rbp)
jge    11d3 <main+0x93>
lea    0xe4b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11d8 <main+0x98>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1162 <main+0x22>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
call   1030 <binaryToDecimal@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x40,%rsp
lea    0xeb4(%rip),%rdi
call   1040 <printf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
test   %eax,%eax
je     118e <main+0x4e>
xor    %ebx,%ebx
mov    %al,(%rsp,%rbx,1)
cmp    $0xa,%eax
je     1188 <main+0x48>
shl    $0x18,%eax
add    $0xceffffff,%eax
cmp    $0xfefffffe,%eax
jbe    11a7 <main+0x67>
add    $0x1,%rbx
xor    %eax,%eax
call   1050 <getchar@plt>
test   %eax,%eax
jne    1160 <main+0x20>
jmp    118e <main+0x4e>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rsi
mov    %ebx,%edi
call   1030 <binaryToDecimal@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbx
ret
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"Enter binary: \00"
@.str.1 = [14 x i8] c"Not a binary!\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [50 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %7
7: ; preds = %38, %0
%8 = call i32 (...) @getchar()
store i32 %8, i32* %4
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %41
10: ; preds = %7
%11 = load i32, i32* %4
%12 = trunc i32 %11 to i8
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %14
store i8 %12, i8* %15
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = sub nsw i32 %20, 48
store i32 %21, i32* %5
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 10
br i1 %23, label %24, label %28
24: ; preds = %10
%25 = load i32, i32* %3
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 %26
store i8 0, i8* %27
br label %41
28: ; preds = %10
%29 = load i32, i32* %5
%30 = icmp sgt i32 %29, 1
br i1 %30, label %34, label %31
31: ; preds = %28
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31, %28
%35 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %31
br label %38
38: ; preds = %37
%39 = load i32, i32* %3
%40 = add nsw i32 %39, 1
store i32 %40, i32* %3
br label %7
41: ; preds = %24, %7
%42 = load i32, i32* %3
%43 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%44 = call i32 @binaryToDecimal(i32 %42, i8* %43)
%45 = load i32, i32* @EXIT_SUCCESS
ret i32 %45
}

LLVM IR (-O2):
@.str = [15 x i8] c"Enter binary: \00"
@.str.1 = [14 x i8] c"Not a binary!\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca [50 x i8]
%2 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 (...) @getchar()
%5 = icmp eq i32 %4, 0
br i1 %5, label %29, label %6
6: ; preds = %0, %23
%7 = phi i64 [ %24, %23 ], [ 0, %0 ]
%8 = phi i32 [ %25, %23 ], [ %4, %0 ]
%9 = trunc i32 %8 to i8
%10 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 %7
store i8 %9, i8* %10
%11 = icmp eq i32 %8, 10
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = trunc i64 %7 to i32
%14 = and i64 %7, 4294967295
%15 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 %14
store i8 0, i8* %15
br label %29
16: ; preds = %6
%17 = shl i32 %8, 24
%18 = add i32 %17, -822083585
%19 = icmp ult i32 %18, -16777217
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = add nuw i64 %7, 1
%25 = tail call i32 (...) @getchar()
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %6
27: ; preds = %23
%28 = trunc i64 %24 to i32
br label %29
29: ; preds = %27, %0, %12
%30 = phi i32 [ %13, %12 ], [ 0, %0 ], [ %28, %27 ]
%31 = call i32 @binaryToDecimal(i32 %30, i8* %2)
%32 = load i32, i32* @EXIT_SUCCESS
ret i32 %32
}