#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__ allocmem(scalar_t__, int);
scalar_t__ length(char *, int);
scalar_t__ store(char, int);
char *filter(char *code, int mode) {
  char *buff = (char *)allocmem(length(code, mode) + 1, sizeof(char));
  char *out = buff;
  char c;
  while ((c = *code++))
    if (store(c, mode))
      *buff++ = c;
  return out;
}


x86 assembly (clang, -O0):
<filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <length@plt>
mov    %rax,%rdi
add    $0x1,%rdi
mov    $0x1,%esi
call   1050 <allocmem@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%al
mov    %al,-0x21(%rbp)
cmp    $0x0,%al
je     11b5 <filter+0x85>
mov    -0x21(%rbp),%al
mov    -0xc(%rbp),%esi
movsbl %al,%edi
call   1030 <store@plt>
cmp    $0x0,%rax
je     11b0 <filter+0x80>
mov    -0x21(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    1168 <filter+0x38>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filter>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r15d
mov    %rdi,%rbp
call   1040 <length@plt>
lea    0x1(%rax),%rdi
mov    $0x1,%esi
call   1050 <allocmem@plt>
mov    %rax,%r14
mov    0x0(%rbp),%bl
test   %bl,%bl
je     1196 <filter+0x66>
add    $0x1,%rbp
mov    %r14,%r12
jmp    117c <filter+0x4c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl 0x0(%rbp),%ebx
add    $0x1,%rbp
test   %bl,%bl
je     1196 <filter+0x66>
movsbl %bl,%edi
mov    %r15d,%esi
call   1030 <store@plt>
test   %rax,%rax
je     1170 <filter+0x40>
mov    %bl,(%r12)
add    $0x1,%r12
jmp    1170 <filter+0x40>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @filter(i8* %0, i32 %1) {
%3 = tail call i64 @length(i8* %0, i32 %1)
%4 = add nsw i64 %3, 1
%5 = tail call i64 @allocmem(i64 %4, i32 1)
%6 = inttoptr i64 %5 to i8*
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %2, %18
%10 = phi i8 [ %20, %18 ], [ %7, %2 ]
%11 = phi i8* [ %13, %18 ], [ %0, %2 ]
%12 = phi i8* [ %19, %18 ], [ %6, %2 ]
%13 = getelementptr inbounds i8, i8* %11, i64 1
%14 = tail call i64 @store(i8 signext %10, i32 %1)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %10, i8* %12
br label %18
18: ; preds = %16, %9
%19 = phi i8* [ %17, %16 ], [ %12, %9 ]
%20 = load i8, i8* %13
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %9
22: ; preds = %18, %2
ret i8* %6
}