#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*/ exit(int /*<<< orphan*/);
scalar_t__ pow(int, int);
int /*<<< orphan*/ printf(char *);
int binary_decimal(char *str) {
  int decimal = 0;
  int i = 0;
  while (str[i] != '\0') {
    if (str[i] != '1' && str[i] != '0') {
      printf("ERROR: Input must be in binary format.\n");
      exit(0);
    }
    decimal += (str[i] - '0') * (int)pow(2, 23 - i);
    i++;
  }
  if (i != 24) {
    printf("ERROR: Input must be 21 digits in length.\n");
    exit(0);
  }
  return decimal;
}


x86 assembly (clang, -O0):
<binary_decimal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     11dd <binary_decimal+0xad>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x31,%eax
je     119c <binary_decimal+0x6c>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
je     119c <binary_decimal+0x6c>
lea    0xe70(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
sub    $0x30,%eax
mov    %eax,-0x14(%rbp)
mov    $0x17,%esi
sub    -0x10(%rbp),%esi
mov    $0x2,%edi
call   1040 <pow@plt>
mov    %rax,%rcx
mov    -0x14(%rbp),%eax
imul   %ecx,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114a <binary_decimal+0x1a>
cmpl   $0x18,-0x10(%rbp)
je     11fa <binary_decimal+0xca>
lea    0xe3a(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<binary_decimal>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
xor    %r14d,%r14d
mov    $0x17,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl 0x0(%rbp),%eax
lea    -0x30(%rax),%ecx
cmp    $0x2,%cl
jae    1182 <binary_decimal+0x52>
movzbl %al,%ebx
add    $0xffffffd0,%ebx
mov    $0x2,%edi
mov    %r15d,%esi
call   1040 <pow@plt>
imul   %ebx,%eax
add    %r14d,%eax
add    $0xffffffff,%r15d
add    $0x1,%rbp
mov    %eax,%r14d
jmp    1150 <binary_decimal+0x20>
test   %al,%al
jne    119a <binary_decimal+0x6a>
cmp    $0xffffffff,%r15d
jne    11ad <binary_decimal+0x7d>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1050 <exit@plt>
lea    0xe4c(%rip),%rdi
jmp    11a1 <binary_decimal+0x71>

LLVM IR (-O0):
@.str = [40 x i8] c"ERROR: Input must be in binary format.\0A\00"
@.str.1 = [43 x i8] c"ERROR: Input must be 21 digits in length.\0A\00"
define i32 @binary_decimal(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
store i32 0, i32* %4
br label %5
5: ; preds = %32, %1
%6 = load i8*, i8** %2
%7 = load i32, i32* %4
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %6, i64 %8
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %49
13: ; preds = %5
%14 = load i8*, i8** %2
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, 49
br i1 %20, label %21, label %32
21: ; preds = %13
%22 = load i8*, i8** %2
%23 = load i32, i32* %4
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 48
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %21, %13
%33 = load i8*, i8** %2
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = sub nsw i32 %38, 48
%40 = load i32, i32* %4
%41 = sub nsw i32 23, %40
%42 = call i64 @pow(i32 2, i32 %41)
%43 = trunc i64 %42 to i32
%44 = mul nsw i32 %39, %43
%45 = load i32, i32* %3
%46 = add nsw i32 %45, %44
store i32 %46, i32* %3
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %5
49: ; preds = %5
%50 = load i32, i32* %4
%51 = icmp ne i32 %50, 24
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %49
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
@str = [42 x i8] c"ERROR: Input must be 21 digits in length.\00"
@str.2 = [39 x i8] c"ERROR: Input must be in binary format.\00"
define i32 @binary_decimal(i8* nocapture %0) {
br label %2
2: ; preds = %10, %1
%3 = phi i64 [ %19, %10 ], [ 0, %1 ]
%4 = phi i32 [ %18, %10 ], [ 0, %1 ]
%5 = getelementptr inbounds i8, i8* %0, i64 %3
%6 = load i8, i8* %5
switch i8 %6, label %7 [
i8 0, label %20
i8 49, label %10
i8 48, label %10
]
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2, %2
%11 = zext i8 %6 to i32
%12 = add nsw i32 %11, -48
%13 = trunc i64 %3 to i32
%14 = sub nsw i32 23, %13
%15 = tail call i64 @pow(i32 2, i32 %14)
%16 = trunc i64 %15 to i32
%17 = mul nuw nsw i32 %12, %16
%18 = add nsw i32 %17, %4
%19 = add nuw i64 %3, 1
br label %2
20: ; preds = %2
%21 = trunc i64 %3 to i32
%22 = icmp eq i32 %21, 24
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %20
ret i32 %4
}