#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 */
typedef int uint32_t;

/* Variables and functions */
scalar_t__ strncmp(char *, char *, int);
int strtoul(char *, char **, int /*<<< orphan*/);
int str_to_uint(char *s, uint32_t *out) {
  int i;
  char *suffix;
  if (strncmp(s, "0b", 2)) {
    *out = strtoul(s, &suffix, 0);
    if (*suffix != '\0')
      return 0;
  } else {
    *out = 0;
    for (i = 2; s[i]; i++) {
      if (s[i] != '0' && s[i] != '1')
        return 0;
      *out <<= 1;
      *out |= s[i] - 0x30;
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<str_to_uint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xec5(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1187 <str_to_uint+0x67>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1182 <str_to_uint+0x62>
movl   $0x0,-0x4(%rbp)
jmp    121c <str_to_uint+0xfc>
jmp    1215 <str_to_uint+0xf5>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
movl   $0x2,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     1210 <str_to_uint+0xf0>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x30,%eax
je     11e0 <str_to_uint+0xc0>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x31,%eax
je     11e0 <str_to_uint+0xc0>
movl   $0x0,-0x4(%rbp)
jmp    121c <str_to_uint+0xfc>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
shl    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
sub    $0x30,%ecx
mov    -0x18(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1198 <str_to_uint+0x78>
jmp    1215 <str_to_uint+0xf5>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_to_uint>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xecb(%rip),%rsi
mov    $0x2,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     1164 <str_to_uint+0x44>
lea    0x8(%rsp),%rsi
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    %eax,(%r14)
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
jne    11ae <str_to_uint+0x8e>
jmp    11a5 <str_to_uint+0x85>
movl   $0x0,(%r14)
add    $0x2,%rbx
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movzbl (%rbx),%ecx
lea    -0x30(%rcx),%edx
cmp    $0x2,%dl
jae    11a1 <str_to_uint+0x81>
add    %eax,%eax
mov    %eax,(%r14)
movsbl (%rbx),%ecx
add    $0xffffffd0,%ecx
or     %ecx,%eax
mov    %eax,(%r14)
add    $0x1,%rbx
jmp    1180 <str_to_uint+0x60>
test   %cl,%cl
jne    11ac <str_to_uint+0x8c>
mov    $0x1,%ebp
jmp    11ae <str_to_uint+0x8e>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"0b\00"
define i32 @str_to_uint(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i64 @strncmp(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 @strtoul(i8* %12, i8** %7, i32 0)
%14 = load i32*, i32** %5
store i32 %13, i32* %14
%15 = load i8*, i8** %7
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11
store i32 0, i32* %3
br label %66
20: ; preds = %11
br label %65
21: ; preds = %2
%22 = load i32*, i32** %5
store i32 0, i32* %22
store i32 2, i32* %6
br label %23
23: ; preds = %61, %21
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %30, label %64
30: ; preds = %23
%31 = load i8*, i8** %4
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 48
br i1 %37, label %38, label %47
38: ; preds = %30
%39 = load i8*, i8** %4
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 49
br i1 %45, label %46, label %47
46: ; preds = %38
store i32 0, i32* %3
br label %66
47: ; preds = %38, %30
%48 = load i32*, i32** %5
%49 = load i32, i32* %48
%50 = shl i32 %49, 1
store i32 %50, i32* %48
%51 = load i8*, i8** %4
%52 = load i32, i32* %6
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = sub nsw i32 %56, 48
%58 = load i32*, i32** %5
%59 = load i32, i32* %58
%60 = or i32 %59, %57
store i32 %60, i32* %58
br label %61
61: ; preds = %47
%62 = load i32, i32* %6
%63 = add nsw i32 %62, 1
store i32 %63, i32* %6
br label %23
64: ; preds = %23
br label %65
65: ; preds = %64, %20
store i32 1, i32* %3
br label %66
66: ; preds = %65, %46, %19
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [3 x i8] c"0b\00"
define i32 @str_to_uint(i8* %0, i32* nocapture %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = call i32 @strtoul(i8* %0, i8** %3, i32 0)
store i32 %8, i32* %1
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %25, label %26
12: ; preds = %2
store i32 0, i32* %1
br label %13
13: ; preds = %18, %12
%14 = phi i32 [ %23, %18 ], [ 0, %12 ]
%15 = phi i64 [ %24, %18 ], [ 2, %12 ]
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
switch i8 %17, label %26 [
i8 0, label %25
i8 48, label %18
i8 49, label %18
]
18: ; preds = %13, %13
%19 = shl i32 %14, 1
store i32 %19, i32* %1
%20 = load i8, i8* %16
%21 = sext i8 %20 to i32
%22 = add nsw i32 %21, -48
%23 = or i32 %22, %19
store i32 %23, i32* %1
%24 = add nuw i64 %15, 1
br label %13
25: ; preds = %13, %7
br label %26
26: ; preds = %13, %7, %25
%27 = phi i32 [ 1, %25 ], [ 0, %7 ], [ 0, %13 ]
ret i32 %27
}