#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 */
char pow(double, double);
int strlen(char *);
unsigned int bin_string_to_int(char *s) {
  unsigned int binary = 0;
  unsigned int index = strlen(s) - 1;
  while (*s != '\0') {
    binary += (*s - 48) * pow((double)2, (double)index);
    index--;
    s++;
  }
  return binary;
}


x86 assembly (clang, -O0):
<bin_string_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     119f <bin_string_to_int+0x7f>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
add    $0xffffffd0,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
cvtsi2sd %rax,%xmm1
movsd  0xe91(%rip),%xmm0
call   1040 <pow@plt>
mov    %al,%cl
mov    -0x14(%rbp),%eax
movsbl %cl,%ecx
imul   %ecx,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1142 <bin_string_to_int+0x22>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bin_string_to_int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    (%r15),%al
test   %al,%al
je     1187 <bin_string_to_int+0x67>
add    $0xffffffff,%ebp
add    $0x1,%r15
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsbl %al,%ebx
add    $0xffffffd0,%ebx
mov    %ebp,%eax
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
movsd  0xe98(%rip),%xmm0
call   1040 <pow@plt>
movsbl %al,%eax
imul   %ebx,%eax
add    %eax,%r14d
movzbl (%r15),%eax
add    $0xffffffff,%ebp
add    $0x1,%r15
test   %al,%al
jne    1150 <bin_string_to_int+0x30>
jmp    118a <bin_string_to_int+0x6a>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bin_string_to_int(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %3
%5 = load i8*, i8** %2
%6 = call i32 @strlen(i8* %5)
%7 = sub nsw i32 %6, 1
store i32 %7, i32* %4
br label %8
8: ; preds = %13, %1
%9 = load i8*, i8** %2
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %29
13: ; preds = %8
%14 = load i8*, i8** %2
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = sub nsw i32 %16, 48
%18 = load i32, i32* %4
%19 = uitofp i32 %18 to double
%20 = call signext i8 @pow(double 2.000000e+00, double %19)
%21 = sext i8 %20 to i32
%22 = mul nsw i32 %17, %21
%23 = load i32, i32* %3
%24 = add i32 %23, %22
store i32 %24, i32* %3
%25 = load i32, i32* %4
%26 = add i32 %25, -1
store i32 %26, i32* %4
%27 = load i8*, i8** %2
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %2
br label %8
29: ; preds = %8
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
define i32 @bin_string_to_int(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %21, label %5
5: ; preds = %1, %5
%6 = phi i8 [ %19, %5 ], [ %3, %1 ]
%7 = phi i32 [ %10, %5 ], [ %2, %1 ]
%8 = phi i32 [ %17, %5 ], [ 0, %1 ]
%9 = phi i8* [ %18, %5 ], [ %0, %1 ]
%10 = add i32 %7, -1
%11 = sext i8 %6 to i32
%12 = add nsw i32 %11, -48
%13 = uitofp i32 %10 to double
%14 = tail call signext i8 @pow(double 2.000000e+00, double %13)
%15 = sext i8 %14 to i32
%16 = mul nsw i32 %12, %15
%17 = add i32 %16, %8
%18 = getelementptr inbounds i8, i8* %9, i64 1
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %5
21: ; preds = %5, %1
%22 = phi i32 [ 0, %1 ], [ %17, %5 ]
ret i32 %22
}