#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 */
scalar_t__ strchr(char *, char);
unsigned int bstr_i(char *cptr) {
  unsigned int i, j = 0;
  while (cptr && *cptr && strchr("01", *cptr)) {
    i = *cptr++ - '0';
    j <<= 1;
    j |= (i & 0x01);
  }
  return (j);
}


x86 assembly (clang, -O0):
<bstr_i>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x11(%rbp)
je     1165 <bstr_i+0x55>
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
je     1165 <bstr_i+0x55>
mov    -0x8(%rbp),%rax
lea    0xead(%rip),%rdi
movsbl (%rax),%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1175 <bstr_i+0x65>
jmp    11a7 <bstr_i+0x97>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
sub    $0x30,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
and    $0x1,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1123 <bstr_i+0x13>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bstr_i>:
push   %r15
push   %r14
push   %rbx
xor    %r14d,%r14d
test   %rdi,%rdi
je     1166 <bstr_i+0x56>
mov    %rdi,%rbx
mov    (%rdi),%al
test   %al,%al
je     1166 <bstr_i+0x56>
add    $0x1,%rbx
xor    %r14d,%r14d
lea    0xecc(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl %al,%esi
mov    %r15,%rdi
call   1030 <strchr@plt>
test   %rax,%rax
je     1166 <bstr_i+0x56>
movzbl -0x1(%rbx),%eax
and    $0x1,%eax
lea    (%rax,%r14,2),%r14d
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1140 <bstr_i+0x30>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"01\00"
define i32 @bstr_i(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
br label %5
5: ; preds = %20, %1
%6 = load i8*, i8** %2
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %18
8: ; preds = %5
%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 %18
13: ; preds = %8
%14 = load i8*, i8** %2
%15 = load i8, i8* %14
%16 = call i64 @strchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %15)
%17 = icmp ne i64 %16, 0
br label %18
18: ; preds = %13, %8, %5
%19 = phi i1 [ false, %8 ], [ false, %5 ], [ %17, %13 ]
br i1 %19, label %20, label %32
20: ; preds = %18
%21 = load i8*, i8** %2
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %2
%23 = load i8, i8* %21
%24 = sext i8 %23 to i32
%25 = sub nsw i32 %24, 48
store i32 %25, i32* %3
%26 = load i32, i32* %4
%27 = shl i32 %26, 1
store i32 %27, i32* %4
%28 = load i32, i32* %3
%29 = and i32 %28, 1
%30 = load i32, i32* %4
%31 = or i32 %30, %29
store i32 %31, i32* %4
br label %5
32: ; preds = %18
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [3 x i8] c"01\00"
define i32 @bstr_i(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %21, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %21, label %6
6: ; preds = %3, %12
%7 = phi i8 [ %19, %12 ], [ %4, %3 ]
%8 = phi i8* [ %13, %12 ], [ %0, %3 ]
%9 = phi i32 [ %18, %12 ], [ 0, %3 ]
%10 = tail call i64 @strchr(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %6
%13 = getelementptr inbounds i8, i8* %8, i64 1
%14 = load i8, i8* %8
%15 = shl i32 %9, 1
%16 = and i8 %14, 1
%17 = zext i8 %16 to i32
%18 = or i32 %15, %17
%19 = load i8, i8* %13
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %6
21: ; preds = %12, %6, %3, %1
%22 = phi i32 [ 0, %1 ], [ 0, %3 ], [ %9, %6 ], [ %18, %12 ]
ret i32 %22
}