#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*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int num, bin_val, dec_val = 0, base = 1, rem;
  printf("Enter a binary numbers(1s and 0s) \n");
  scanf("%d", &num);
  bin_val = num;
  while (num > 0) {
    rem = num % 10;
    dec_val = dec_val + rem * base;
    num = num / 10;
    base = base * 2;
  }
  printf("The binary number is = %d \n", bin_val);
  printf("The decimal number is = %d \n", dec_val);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xed2(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11a4 <main+0x84>
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x18(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1161 <main+0x41>
mov    -0xc(%rbp),%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xf02(%rip),%rdi
call   1030 <puts@plt>
lea    0xeba(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x4(%rsp),%esi
test   %esi,%esi
jle    1198 <main+0x68>
mov    $0x1,%eax
xor    %ebp,%ebp
mov    $0xcccccccd,%r8d
mov    %esi,%edx
nopw   0x0(%rax,%rax,1)
mov    %edx,%ebx
mov    %edx,%edx
imul   %r8,%rdx
shr    $0x23,%rdx
lea    (%rdx,%rdx,1),%ecx
lea    (%rcx,%rcx,4),%ecx
mov    %ebx,%edi
sub    %ecx,%edi
imul   %eax,%edi
add    %edi,%ebp
add    %eax,%eax
cmp    $0x9,%ebx
ja     1170 <main+0x40>
mov    %edx,0x4(%rsp)
jmp    119a <main+0x6a>
xor    %ebp,%ebp
lea    0xe62(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe70(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Enter a binary numbers(1s and 0s) \0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [28 x i8] c"The binary number is = %d \0A\00"
@.str.3 = [29 x i8] c"The decimal number is = %d \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* %4
store i32 1, i32* %5
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 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
store i32 %9, i32* %3
br label %10
10: ; preds = %13, %0
%11 = load i32, i32* %2
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %25
13: ; preds = %10
%14 = load i32, i32* %2
%15 = srem i32 %14, 10
store i32 %15, i32* %6
%16 = load i32, i32* %4
%17 = load i32, i32* %6
%18 = load i32, i32* %5
%19 = mul nsw i32 %17, %18
%20 = add nsw i32 %16, %19
store i32 %20, i32* %4
%21 = load i32, i32* %2
%22 = sdiv i32 %21, 10
store i32 %22, i32* %2
%23 = load i32, i32* %5
%24 = mul nsw i32 %23, 2
store i32 %24, i32* %5
br label %10
25: ; preds = %10
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %4
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [28 x i8] c"The binary number is = %d \0A\00"
@.str.3 = [29 x i8] c"The decimal number is = %d \0A\00"
@str = [35 x i8] c"Enter a binary numbers(1s and 0s) \00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 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 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %0, %7
%8 = phi i32 [ %13, %7 ], [ 0, %0 ]
%9 = phi i32 [ %15, %7 ], [ 1, %0 ]
%10 = phi i32 [ %14, %7 ], [ %5, %0 ]
%11 = urem i32 %10, 10
%12 = mul nsw i32 %11, %9
%13 = add nsw i32 %8, %12
%14 = udiv i32 %10, 10
%15 = shl nsw i32 %9, 1
%16 = icmp ult i32 %10, 10
br i1 %16, label %17, label %7
17: ; preds = %7
store i32 %14, i32* %1
br label %18
18: ; preds = %17, %0
%19 = phi i32 [ %13, %17 ], [ 0, %0 ]
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %5)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i32 %19)
ret i32 0
}