#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 */
typedef int ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fn_readmem(char *);
int /*<<< orphan*/ fn_syscall(char *);
int /*<<< orphan*/ fn_writemem(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
char *strstr(char *, char *);
int main(void) {
  FILE *f = fdopen(fileno(stdin), "rb");
  char *line = NULL;
  size_t len = 0;
  ssize_t cnt;
  while ((cnt = getline(&line, &len, f)) != -1) {
    line[cnt - 1] = 0;
    if (strstr(line, "readmem") == line) {
      fn_readmem(line + 7);
    } else if (strstr(line, "writemem") == line) {
      fn_writemem(line + 8);
    } else if (strstr(line, "syscall") == line) {
      fn_syscall(line + 7);
    }
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1070 <fileno@plt>
mov    %eax,%edi
lea    0xe5a(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1090 <getline@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     127b <main+0xfb>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
lea    0xe0b(%rip),%rsi
call   10a0 <strstr@plt>
cmp    -0x18(%rbp),%rax
jne    1219 <main+0x99>
mov    -0x18(%rbp),%rdi
add    $0x7,%rdi
call   1060 <fn_readmem@plt>
jmp    1276 <main+0xf6>
mov    -0x18(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   10a0 <strstr@plt>
cmp    -0x18(%rbp),%rax
jne    1245 <main+0xc5>
mov    -0x18(%rbp),%rdi
add    $0x8,%rdi
call   1050 <fn_writemem@plt>
jmp    1271 <main+0xf1>
mov    -0x18(%rbp),%rdi
lea    0xdc4(%rip),%rsi
call   10a0 <strstr@plt>
cmp    -0x18(%rbp),%rax
jne    126c <main+0xec>
mov    -0x18(%rbp),%rdi
add    $0x7,%rdi
call   1030 <fn_syscall@plt>
jmp    1271 <main+0xf1>
jmp    1276 <main+0xf6>
jmp    11bf <main+0x3f>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1080 <fileno@plt>
lea    0xe5d(%rip),%rsi
mov    %eax,%edi
call   1090 <fdopen@plt>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     1291 <main+0x111>
lea    0xe22(%rip),%r14
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
lea    0xe19(%rip),%r13
jmp    121f <main+0x9f>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x7,%rbp
mov    %rbp,%rdi
call   1070 <fn_readmem@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   10a0 <getline@plt>
cmp    $0xffffffff,%eax
je     1291 <main+0x111>
mov    0x8(%rsp),%rcx
cltq
movb   $0x0,-0x1(%rax,%rcx,1)
mov    0x8(%rsp),%rbp
mov    $0x7,%edx
mov    %rbp,%rdi
mov    %r14,%rsi
call   1040 <strncmp@plt>
test   %eax,%eax
je     1200 <main+0x80>
mov    $0x8,%edx
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <strncmp@plt>
test   %eax,%eax
je     1280 <main+0x100>
mov    $0x7,%edx
mov    %rbp,%rdi
lea    0xdad(%rip),%rsi
call   1040 <strncmp@plt>
test   %eax,%eax
jne    120c <main+0x8c>
add    $0x7,%rbp
mov    %rbp,%rdi
call   1030 <fn_syscall@plt>
jmp    120c <main+0x8c>
xchg   %ax,%ax
add    $0x8,%rbp
mov    %rbp,%rdi
call   1060 <fn_writemem@plt>
jmp    120c <main+0x8c>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdin = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [8 x i8] c"readmem\00"
@.str.2 = [9 x i8] c"writemem\00"
@.str.3 = [8 x i8] c"syscall\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @stdin
%7 = call i32 @fileno(i32 %6)
%8 = call i32* @fdopen(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %2
store i8* null, i8** %3
store i64 0, i64* %4
br label %9
9: ; preds = %47, %0
%10 = load i32*, i32** %2
%11 = call i32 @getline(i8** %3, i64* %4, i32* %10)
store i32 %11, i32* %5
%12 = icmp ne i32 %11, -1
br i1 %12, label %13, label %48
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
store i8 0, i8* %18
%19 = load i8*, i8** %3
%20 = call i8* @strstr(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** %3
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %27
23: ; preds = %13
%24 = load i8*, i8** %3
%25 = getelementptr inbounds i8, i8* %24, i64 7
%26 = call i32 @fn_readmem(i8* %25)
br label %47
27: ; preds = %13
%28 = load i8*, i8** %3
%29 = call i8* @strstr(i8* %28, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%30 = load i8*, i8** %3
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i64 8
%35 = call i32 @fn_writemem(i8* %34)
br label %46
36: ; preds = %27
%37 = load i8*, i8** %3
%38 = call i8* @strstr(i8* %37, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%39 = load i8*, i8** %3
%40 = icmp eq i8* %38, %39
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = load i8*, i8** %3
%43 = getelementptr inbounds i8, i8* %42, i64 7
%44 = call i32 @fn_syscall(i8* %43)
br label %45
45: ; preds = %41, %36
br label %46
46: ; preds = %45, %32
br label %47
47: ; preds = %46, %23
br label %9
48: ; preds = %9
%49 = load i32*, i32** %2
%50 = call i32 @fclose(i32* %49)
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [3 x i8] c"rb\00"
@.str.1 = [8 x i8] c"readmem\00"
@.str.2 = [9 x i8] c"writemem\00"
@.str.3 = [8 x i8] c"syscall\00"
define i32 @main() {
%1 = alloca i8*
%2 = alloca i64
%3 = load i32, i32* @stdin
%4 = tail call i32 @fileno(i32 %3)
%5 = tail call i32* @fdopen(i32 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%7 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%8 = call i32 @getline(i8** %1, i64* %2, i32* %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %37, label %10
10: ; preds = %0, %34
%11 = phi i32 [ %35, %34 ], [ %8, %0 ]
%12 = load i8*, i8** %1
%13 = add nsw i32 %11, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8 0, i8* %15
%16 = load i8*, i8** %1
%17 = call i32 @strncmp(i8* dereferenceable(1) %16, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i64 7)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %10
%20 = getelementptr inbounds i8, i8* %16, i64 7
%21 = call i32 @fn_readmem(i8* %20)
br label %34
22: ; preds = %10
%23 = call i32 @strncmp(i8* dereferenceable(1) %16, i8* dereferenceable(9) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i64 8)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = getelementptr inbounds i8, i8* %16, i64 8
%27 = call i32 @fn_writemem(i8* %26)
br label %34
28: ; preds = %22
%29 = call i32 @strncmp(i8* dereferenceable(1) %16, i8* dereferenceable(8) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 7)
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = getelementptr inbounds i8, i8* %16, i64 7
%33 = call i32 @fn_syscall(i8* %32)
br label %34
34: ; preds = %25, %31, %28, %19
%35 = call i32 @getline(i8** %1, i64* %2, i32* %5)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %10
37: ; preds = %34, %0
%38 = call i32 @fclose(i32* %5)
ret i32 0
}