#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*/ gets(char *);
scalar_t__ isspace(char);
int /*<<< orphan*/ printf(char *);
int strlen(char *);
unsigned short readasbits() {
  char buf[256];
  printf("bits: ");
  gets(buf);
  unsigned short val = 0;
  int len = strlen(buf);
  int i = 0;
  while ((i < len) && (isspace(buf[i])))
    i++;
  while ((i < len) && ((buf[i] == '0') || (buf[i] == '1'))) {
    val = val << 1;
    if (buf[i] == '1') {
      val = val | 01;
    }
    i++;
  }
  return val;
}


x86 assembly (clang, -O0):
<readasbits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
lea    0xeae(%rip),%rdi
call   1040 <printf@plt>
lea    -0x100(%rbp),%rdi
call   1060 <gets@plt>
movw   $0x0,-0x102(%rbp)
lea    -0x100(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x108(%rbp)
movl   $0x0,-0x10c(%rbp)
mov    -0x10c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x108(%rbp),%ecx
mov    %al,-0x10d(%rbp)
jge    11c3 <readasbits+0x83>
movslq -0x10c(%rbp),%rax
movsbl -0x100(%rbp,%rax,1),%edi
ff
call   1050 <isspace@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x10d(%rbp)
mov    -0x10d(%rbp),%al
test   $0x1,%al
jne    11d6 <readasbits+0x96>
jmp    11ea <readasbits+0xaa>
mov    -0x10c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10c(%rbp)
jmp    1188 <readasbits+0x48>
jmp    11ef <readasbits+0xaf>
mov    -0x10c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x108(%rbp),%ecx
mov    %al,-0x10e(%rbp)
jge    1250 <readasbits+0x110>
movslq -0x10c(%rbp),%rax
movsbl -0x100(%rbp,%rax,1),%ecx
ff
mov    $0x1,%al
cmp    $0x30,%ecx
mov    %al,-0x10f(%rbp)
je     1244 <readasbits+0x104>
movslq -0x10c(%rbp),%rax
movsbl -0x100(%rbp,%rax,1),%eax
ff
cmp    $0x31,%eax
sete   %al
mov    %al,-0x10f(%rbp)
mov    -0x10f(%rbp),%al
mov    %al,-0x10e(%rbp)
mov    -0x10e(%rbp),%al
test   $0x1,%al
jne    1263 <readasbits+0x123>
jmp    12b1 <readasbits+0x171>
movzwl -0x102(%rbp),%eax
shl    $0x1,%eax
mov    %ax,-0x102(%rbp)
movslq -0x10c(%rbp),%rax
movsbl -0x100(%rbp,%rax,1),%eax
ff
cmp    $0x31,%eax
jne    129d <readasbits+0x15d>
movzwl -0x102(%rbp),%eax
or     $0x1,%eax
mov    %ax,-0x102(%rbp)
mov    -0x10c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10c(%rbp)
jmp    11ef <readasbits+0xaf>
movzwl -0x102(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readasbits>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
lea    0xeae(%rip),%rdi
call   1040 <printf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <gets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
test   %eax,%eax
jle    1199 <readasbits+0x59>
mov    %r14d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movsbl (%rsp,%rbx,1),%edi
call   1050 <isspace@plt>
test   %rax,%rax
je     119b <readasbits+0x5b>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1180 <readasbits+0x40>
jmp    11d6 <readasbits+0x96>
xor    %ebx,%ebx
cmp    %r14d,%ebx
jge    11d6 <readasbits+0x96>
mov    %ebx,%eax
lea    (%rsp,%rax,1),%rsi
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
movzbl (%rsi),%ecx
mov    %ecx,%edx
and    $0xfe,%dl
cmp    $0x30,%dl
jne    11d8 <readasbits+0x98>
xor    %edx,%edx
cmp    $0x31,%cl
sete   %dl
lea    (%rdx,%rax,2),%eax
add    $0x1,%rsi
add    $0x1,%ebx
cmp    %ebx,%r14d
jg     11b0 <readasbits+0x70>
jmp    11d8 <readasbits+0x98>
xor    %eax,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [7 x i8] c"bits: \00"
define zeroext i16 @readasbits() {
%1 = alloca [256 x i8]
%2 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @gets(i8* %2)
%5 = call i32 @strlen(i8* %2)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %20
7: ; preds = %0
%8 = zext i32 %5 to i64
br label %9
9: ; preds = %7, %15
%10 = phi i64 [ 0, %7 ], [ %16, %15 ]
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %10
%12 = load i8, i8* %11
%13 = call i64 @isspace(i8 signext %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %9
%16 = add nuw nsw i64 %10, 1
%17 = icmp eq i64 %16, %8
br i1 %17, label %40, label %9
18: ; preds = %9
%19 = trunc i64 %10 to i32
br label %20
20: ; preds = %18, %0
%21 = phi i32 [ 0, %0 ], [ %19, %18 ]
%22 = icmp slt i32 %21, %5
br i1 %22, label %23, label %40
23: ; preds = %20
%24 = zext i32 %21 to i64
br label %25
25: ; preds = %23, %32
%26 = phi i64 [ %24, %23 ], [ %37, %32 ]
%27 = phi i16 [ 0, %23 ], [ %36, %32 ]
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %26
%29 = load i8, i8* %28
%30 = and i8 %29, -2
%31 = icmp eq i8 %30, 48
br i1 %31, label %32, label %40
32: ; preds = %25
%33 = shl i16 %27, 1
%34 = icmp eq i8 %29, 49
%35 = zext i1 %34 to i16
%36 = or i16 %33, %35
%37 = add nuw nsw i64 %26, 1
%38 = trunc i64 %37 to i32
%39 = icmp sgt i32 %5, %38
br i1 %39, label %25, label %40
40: ; preds = %15, %32, %25, %20
%41 = phi i16 [ 0, %20 ], [ %27, %25 ], [ %36, %32 ], [ 0, %15 ]
ret i16 %41
}