#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 getchar();
void scan(int len, char *s) {
  int i = 0, c;
  while (i < len) {
    c = getchar();
    if (c != '0' && c != '1')
      continue;
    s[i++] = c;
  }
  s[i] = 0;
}


x86 assembly (clang, -O0):
<scan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1174 <scan+0x64>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x30,-0x18(%rbp)
je     1155 <scan+0x45>
cmpl   $0x31,-0x18(%rbp)
je     1155 <scan+0x45>
jmp    1126 <scan+0x16>
mov    -0x18(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1126 <scan+0x16>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scan>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
test   %edi,%edi
jle    1153 <scan+0x43>
mov    %edi,%r14d
mov    %edi,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ecx
and    $0xfffffffe,%ecx
cmp    $0x30,%ecx
jne    1130 <scan+0x20>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbp,%rbx
jne    1130 <scan+0x20>
movslq %r14d,%rax
jmp    1155 <scan+0x45>
xor    %eax,%eax
movb   $0x0,(%r15,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @scan(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %7
7: ; preds = %19, %18, %2
%8 = load i32, i32* %5
%9 = load i32, i32* %3
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %27
11: ; preds = %7
%12 = call i32 (...) @getchar()
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 48
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 49
br i1 %17, label %18, label %19
18: ; preds = %15
br label %7
19: ; preds = %15, %11
%20 = load i32, i32* %6
%21 = trunc i32 %20 to i8
%22 = load i8*, i8** %4
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 %21, i8* %26
br label %7
27: ; preds = %7
%28 = load i8*, i8** %4
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
store i8 0, i8* %31
ret void
}

LLVM IR (-O2):
define void @scan(i32 %0, i8* nocapture writeonly %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %19
4: ; preds = %2
%5 = zext i32 %0 to i64
br label %6
6: ; preds = %4, %12
%7 = phi i64 [ 0, %4 ], [ %14, %12 ]
br label %8
8: ; preds = %6, %8
%9 = tail call i32 (...) @getchar()
%10 = and i32 %9, -2
%11 = icmp eq i32 %10, 48
br i1 %11, label %12, label %8
12: ; preds = %8
%13 = trunc i32 %9 to i8
%14 = add nuw nsw i64 %7, 1
%15 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %13, i8* %15
%16 = icmp eq i64 %14, %5
br i1 %16, label %17, label %6
17: ; preds = %12
%18 = sext i32 %0 to i64
br label %19
19: ; preds = %17, %2
%20 = phi i64 [ 0, %2 ], [ %18, %17 ]
%21 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 0, i8* %21
ret void
}