#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*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ generate_so(char *, int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ very_very_hard_filter(char *);
void set_filter() {
  char buf[0x1000];
  printf("make int filter(const char* arg) function!\n");
  printf("after you give filter, server compiles it by\n");
  printf("gcc -fPIC -Werror=implicit-function-declaration --shared -o "
         "filter.so filter.c\n");
  printf("example : if(arg[0] == 'C' && arg[1] == 'A') return 1;\n");
  memset(buf, '\x00', 0x1000);
  fgets(buf, 0x1000, stdin);
  int x = strlen(buf);
  if (very_very_hard_filter(buf)) {
    puts("invalid input");
    return;
  }
  char *sobuf = malloc(x + 0x200);
  strcpy(sobuf, "int filter(const char* arg){ ");
  strcat(sobuf, buf);
  strcat(sobuf, "}");
  generate_so(sobuf, strlen(sobuf));
  free(sobuf);
}


x86 assembly (clang, -O0):
<set_filter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
lea    0xe3e(%rip),%rdi
call   1080 <printf@plt>
lea    0xe5e(%rip),%rdi
call   1080 <printf@plt>
lea    0xe80(%rip),%rdi
call   1080 <printf@plt>
lea    0xec4(%rip),%rdi
call   1080 <printf@plt>
lea    -0x1000(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1090 <memset@plt>
lea    -0x1000(%rbp),%rdi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   10a0 <fgets@plt>
lea    -0x1000(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x1004(%rbp)
lea    -0x1000(%rbp),%rdi
call   10b0 <very_very_hard_filter@plt>
cmp    $0x0,%rax
je     1251 <set_filter+0xa1>
lea    0xe9b(%rip),%rdi
call   1050 <puts@plt>
jmp    12d6 <set_filter+0x126>
mov    -0x1004(%rbp),%edi
add    $0x200,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x1010(%rbp)
mov    -0x1010(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x1010(%rbp),%rdi
lea    -0x1000(%rbp),%rsi
call   10d0 <strcat@plt>
mov    -0x1010(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   10d0 <strcat@plt>
mov    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x1010(%rbp),%rdi
call   1070 <strlen@plt>
mov    -0x1018(%rbp),%rdi
mov    %eax,%esi
call   1060 <generate_so@plt>
mov    -0x1010(%rbp),%rdi
call   1030 <free@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_filter>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
lea    0xe7d(%rip),%rdi
call   1050 <puts@plt>
lea    0xe9c(%rip),%rdi
call   1050 <puts@plt>
lea    0xebd(%rip),%rdi
call   1050 <puts@plt>
lea    0xf00(%rip),%rdi
call   1050 <puts@plt>
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0x1000,%esi
call   1090 <fgets@plt>
mov    %r14,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   10a0 <very_very_hard_filter@plt>
test   %rax,%rax
je     1227 <set_filter+0x87>
lea    0xde0(%rip),%rdi
call   1050 <puts@plt>
jmp    127a <set_filter+0xda>
add    $0x200,%ebx
mov    %ebx,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
lea    0xdd0(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10c0 <strcat@plt>
lea    0xdd4(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <strcat@plt>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <generate_so@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [44 x i8] c"make int filter(const char* arg) function!\0A\00"
@.str.1 = [46 x i8] c"after you give filter, server compiles it by\0A\00"
@.str.2 = [80 x i8] c"gcc -fPIC -Werror=implicit-function-declaration --shared -o filter.so filter.c\0A\00"
@.str.3 = [56 x i8] c"example : if(arg[0] == 'C' && arg[1] == 'A') return 1;\0A\00"
@stdin = global i32 0
@.str.4 = [14 x i8] c"invalid input\00"
@.str.5 = [30 x i8] c"int filter(const char* arg){ \00"
@.str.6 = [2 x i8] c"}\00"
define void @set_filter() {
%1 = alloca [4096 x i8]
%2 = alloca i32
%3 = alloca i8*
%4 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32 @printf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str.2, i64 0, i64 0))
%7 = call i32 @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0))
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i8 signext 0, i32 4096)
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%11 = load i32, i32* @stdin
%12 = call i32 @fgets(i8* %10, i32 4096, i32 %11)
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %2
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%16 = call i64 @very_very_hard_filter(i8* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %37
20: ; preds = %0
%21 = load i32, i32* %2
%22 = add nsw i32 %21, 512
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %3
%24 = load i8*, i8** %3
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%26 = load i8*, i8** %3
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%28 = call i32 @strcat(i8* %26, i8* %27)
%29 = load i8*, i8** %3
%30 = call i32 @strcat(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%31 = load i8*, i8** %3
%32 = load i8*, i8** %3
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @generate_so(i8* %31, i32 %33)
%35 = load i8*, i8** %3
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %20, %18
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.4 = [14 x i8] c"invalid input\00"
@.str.5 = [30 x i8] c"int filter(const char* arg){ \00"
@.str.6 = [2 x i8] c"}\00"
@str = [43 x i8] c"make int filter(const char* arg) function!\00"
@str.7 = [45 x i8] c"after you give filter, server compiles it by\00"
@str.8 = [79 x i8] c"gcc -fPIC -Werror=implicit-function-declaration --shared -o filter.so filter.c\00"
@str.9 = [55 x i8] c"example : if(arg[0] == 'C' && arg[1] == 'A') return 1;\00"
define void @set_filter() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.7, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([79 x i8], [79 x i8]* @str.8, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.9, i64 0, i64 0))
%7 = call i32 @memset(i8* %2, i8 signext 0, i32 4096)
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %2, i32 4096, i32 %8)
%10 = call i32 @strlen(i8* %2)
%11 = call i64 @very_very_hard_filter(i8* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %0
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %24
15: ; preds = %0
%16 = add nsw i32 %10, 512
%17 = call i8* @malloc(i32 %16)
%18 = call i32 @strcpy(i8* %17, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%19 = call i32 @strcat(i8* %17, i8* %2)
%20 = call i32 @strcat(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%21 = call i32 @strlen(i8* %17)
%22 = call i32 @generate_so(i8* %17, i32 %21)
%23 = call i32 @free(i8* %17)
br label %24
24: ; preds = %15, %13
ret void
}