#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 checkBinary(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
main() {
  int n;
  int decimal = 0;
  int exponent = 1;
  int remainder = 0;
  printf("Enter a binary number\n");
  scanf("%i", &n);
  int check = checkBinary(n);
  if (check == 0) {
    printf("Error, this is not a binary number\n");
    return 0;
  }
  while (n > 0) {
    remainder = n % 10;
    decimal = decimal + remainder * exponent;
    exponent = exponent * 2;
    n = n / 10;
  }
  printf("%i\n", decimal);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xeae(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%edi
call   1050 <checkBinary@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11a1 <main+0x71>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1201 <main+0xd1>
jmp    11a6 <main+0x76>
cmpl   $0x0,-0x8(%rbp)
jle    11e9 <main+0xb9>
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    -0x14(%rbp),%ecx
imul   -0x10(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
jmp    11a6 <main+0x76>
mov    -0xc(%rbp),%esi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
lea    0xeac(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x4(%rsp),%edi
call   1060 <checkBinary@plt>
test   %eax,%eax
je     11b9 <main+0x79>
mov    0x4(%rsp),%eax
xor    %esi,%esi
test   %eax,%eax
jle    11a9 <main+0x69>
mov    $0x1,%ecx
mov    $0xcccccccd,%r8d
mov    %eax,%r9d
mov    %eax,%eax
imul   %r8,%rax
shr    $0x23,%rax
lea    (%rax,%rax,1),%edx
lea    (%rdx,%rdx,4),%edx
mov    %r9d,%edi
sub    %edx,%edi
imul   %ecx,%edi
add    %edi,%esi
add    %ecx,%ecx
cmp    $0x9,%r9d
ja     1180 <main+0x40>
mov    %eax,0x4(%rsp)
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11c5 <main+0x85>
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Enter a binary number\0A\00"
@.str.1 = [3 x i8] c"%i\00"
@.str.2 = [36 x i8] c"Error, this is not a binary number\0A\00"
@.str.3 = [4 x i8] c"%i\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %3
store i32 1, i32* %4
store i32 0, i32* %5
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%9 = load i32, i32* %2
%10 = call i32 @checkBinary(i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %1
br label %34
15: ; preds = %0
br label %16
16: ; preds = %19, %15
%17 = load i32, i32* %2
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %16
%20 = load i32, i32* %2
%21 = srem i32 %20, 10
store i32 %21, i32* %5
%22 = load i32, i32* %3
%23 = load i32, i32* %5
%24 = load i32, i32* %4
%25 = mul nsw i32 %23, %24
%26 = add nsw i32 %22, %25
store i32 %26, i32* %3
%27 = load i32, i32* %4
%28 = mul nsw i32 %27, 2
store i32 %28, i32* %4
%29 = load i32, i32* %2
%30 = sdiv i32 %29, 10
store i32 %30, i32* %2
br label %16
31: ; preds = %16
%32 = load i32, i32* %3
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %32)
store i32 0, i32* %1
br label %34
34: ; preds = %31, %13
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%i\00"
@.str.3 = [4 x i8] c"%i\0A\00"
@str = [22 x i8] c"Enter a binary number\00"
@str.4 = [35 x i8] c"Error, this is not a binary number\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = call i32 @checkBinary(i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.4, i64 0, i64 0))
br label %27
10: ; preds = %0
%11 = load i32, i32* %1
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %10, %13
%14 = phi i32 [ %20, %13 ], [ 1, %10 ]
%15 = phi i32 [ %19, %13 ], [ 0, %10 ]
%16 = phi i32 [ %21, %13 ], [ %11, %10 ]
%17 = urem i32 %16, 10
%18 = mul nsw i32 %14, %17
%19 = add nsw i32 %18, %15
%20 = shl nsw i32 %14, 1
%21 = udiv i32 %16, 10
%22 = icmp ult i32 %16, 10
br i1 %22, label %23, label %13
23: ; preds = %13
store i32 %21, i32* %1
br label %24
24: ; preds = %23, %10
%25 = phi i32 [ %19, %23 ], [ 0, %10 ]
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %8
ret i32 0
}