#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 pow(int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, long *);
int bintodec() {
  long int num;
  int dec = 0, i = 0, rem;
  printf("\nEnter a binary number: ");
  scanf("%ld", &num);
  while (num != 0) {
    rem = num % 10;
    num = num / 10;
    dec = dec + rem * pow(2, i);
    ++i;
  }
  printf("\nThe number in decimal: %d", dec);
}


x86 assembly (clang, -O0):
<bintodec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xebe(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <scanf@plt>
cmpq   $0x0,-0x10(%rbp)
je     11cb <bintodec+0x9b>
mov    -0x10(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %edx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xa,%ecx
cqto
idiv   %rcx
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%esi
mov    $0x2,%edi
call   1050 <pow@plt>
mov    -0x24(%rbp),%ecx
mov    %eax,%edx
mov    -0x20(%rbp),%eax
imul   %edx,%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1164 <bintodec+0x34>
mov    -0x14(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bintodec>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xec2(%rip),%rdi
xor    %r14d,%r14d
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xeca(%rip),%rdi
mov    %rsp,%rsi
call   1040 <scanf@plt>
mov    (%rsp),%rbx
test   %rbx,%rbx
je     11ae <bintodec+0x7e>
xor    %r14d,%r14d
movabs $0x6666666666666667,%r15
66 66 66
xor    %ebp,%ebp
nop
mov    %rbx,%rax
imul   %r15
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x2,%rdx
add    %rax,%rdx
lea    (%rdx,%rdx,1),%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebx
mov    %rdx,(%rsp)
mov    $0x2,%edi
mov    %ebp,%esi
call   1050 <pow@plt>
imul   %ebx,%eax
add    %eax,%r14d
add    $0x1,%ebp
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1170 <bintodec+0x40>
lea    0xe68(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"\0AEnter a binary number: \00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [27 x i8] c"\0AThe number in decimal: %d\00"
define i32 @bintodec() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %3
store i32 0, i32* %4
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %2)
br label %8
8: ; preds = %11, %0
%9 = load i64, i64* %2
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %8
%12 = load i64, i64* %2
%13 = srem i64 %12, 10
%14 = trunc i64 %13 to i32
store i32 %14, i32* %5
%15 = load i64, i64* %2
%16 = sdiv i64 %15, 10
store i64 %16, i64* %2
%17 = load i32, i32* %3
%18 = load i32, i32* %5
%19 = load i32, i32* %4
%20 = call i32 @pow(i32 2, i32 %19)
%21 = mul nsw i32 %18, %20
%22 = add nsw i32 %17, %21
store i32 %22, i32* %3
%23 = load i32, i32* %4
%24 = add nsw i32 %23, 1
store i32 %24, i32* %4
br label %8
25: ; preds = %8
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [25 x i8] c"\0AEnter a binary number: \00"
@.str.1 = [4 x i8] c"%ld\00"
@.str.2 = [27 x i8] c"\0AThe number in decimal: %d\00"
define i32 @bintodec() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %1)
%5 = load i64, i64* %1
%6 = icmp eq i64 %5, 0
br i1 %6, label %20, label %7
7: ; preds = %0, %7
%8 = phi i64 [ %18, %7 ], [ %5, %0 ]
%9 = phi i32 [ %16, %7 ], [ 0, %0 ]
%10 = phi i32 [ %17, %7 ], [ 0, %0 ]
%11 = srem i64 %8, 10
%12 = trunc i64 %11 to i32
%13 = sdiv i64 %8, 10
store i64 %13, i64* %1
%14 = call i32 @pow(i32 2, i32 %10)
%15 = mul nsw i32 %14, %12
%16 = add nsw i32 %15, %9
%17 = add nuw nsw i32 %10, 1
%18 = load i64, i64* %1
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %7
20: ; preds = %7, %0
%21 = phi i32 [ 0, %0 ], [ %16, %7 ]
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %21)
ret i32 undef
}