#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 */
char *strchr(char *, char);
char *strdup(char *);
int strlen(char *);
char *strfilter1(char *string, char *letters) {
  char buffer[strlen(string) + 1];
  int j = 0;
  for (int i = 0; i < strlen(string); i++) {
    char *ptr = strchr(letters, string[i]);
    if (ptr) {
      buffer[j++] = string[i];
    }
  }
  buffer[j] = '\0';
  return strdup(buffer);
}


x86 assembly (clang, -O0):
<strfilter1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
cmp    %ecx,%eax
jge    11f0 <strfilter1+0xc0>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
call   1040 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11dd <strfilter1+0xad>
mov    -0x38(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0x28(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    11e2 <strfilter1+0xb2>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    117d <strfilter1+0x4d>
mov    -0x38(%rbp),%rdi
movslq -0x24(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
call   1050 <strdup@plt>
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strfilter1>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rcx,%rsp
mov    %r12,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    11ba <strfilter1+0x8a>
xor    %ebx,%ebx
xor    %r13d,%r13d
jmp    1193 <strfilter1+0x63>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%rbx
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    11bd <strfilter1+0x8d>
movzbl (%r12,%rbx,1),%r15d
movsbl %r15b,%esi
mov    %r14,%rdi
call   1040 <strchr@plt>
test   %rax,%rax
je     1180 <strfilter1+0x50>
movslq %r13d,%rax
add    $0x1,%r13d
mov    -0x30(%rbp),%rcx
mov    %r15b,(%rcx,%rax,1)
jmp    1180 <strfilter1+0x50>
xor    %r13d,%r13d
movslq %r13d,%rax
mov    -0x30(%rbp),%rdi
movb   $0x0,(%rdi,%rax,1)
call   1050 <strdup@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @strfilter1(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 1
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %5
%15 = alloca i8, i64 %13
store i64 %13, i64* %6
store i32 0, i32* %7
store i32 0, i32* %8
br label %16
16: ; preds = %42, %2
%17 = load i32, i32* %8
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = icmp slt i32 %17, %19
br i1 %20, label %21, label %45
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = load i8*, i8** %3
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = call i8* @strchr(i8* %22, i8 signext %27)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %41
31: ; preds = %21
%32 = load i8*, i8** %3
%33 = load i32, i32* %8
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = load i32, i32* %7
%38 = add nsw i32 %37, 1
store i32 %38, i32* %7
%39 = sext i32 %37 to i64
%40 = getelementptr inbounds i8, i8* %15, i64 %39
store i8 %36, i8* %40
br label %41
41: ; preds = %31, %21
br label %42
42: ; preds = %41
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %16
45: ; preds = %16
%46 = load i32, i32* %7
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %15, i64 %47
store i8 0, i8* %48
%49 = call i8* @strdup(i8* %15)
%50 = load i8*, i8** %5
ret i8* %49
}

LLVM IR (-O2):
define noalias i8* @strfilter1(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i32 @strlen(i8* %0)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %25, %2
%10 = phi i32 [ 0, %2 ], [ %26, %25 ]
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %6, i64 %11
store i8 0, i8* %12
%13 = call i8* @strdup(i8* %6)
ret i8* %13
14: ; preds = %2, %25
%15 = phi i64 [ %27, %25 ], [ 0, %2 ]
%16 = phi i32 [ %26, %25 ], [ 0, %2 ]
%17 = getelementptr inbounds i8, i8* %0, i64 %15
%18 = load i8, i8* %17
%19 = tail call i8* @strchr(i8* dereferenceable(1) %1, i8 signext %18)
%20 = icmp eq i8* %19, null
br i1 %20, label %25, label %21
21: ; preds = %14
%22 = add nsw i32 %16, 1
%23 = sext i32 %16 to i64
%24 = getelementptr inbounds i8, i8* %6, i64 %23
store i8 %18, i8* %24
br label %25
25: ; preds = %21, %14
%26 = phi i32 [ %22, %21 ], [ %16, %14 ]
%27 = add nuw nsw i64 %15, 1
%28 = tail call i32 @strlen(i8* %0)
%29 = sext i32 %28 to i64
%30 = icmp slt i64 %27, %29
br i1 %30, label %14, label %9
}