#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*/ gets(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strstr(char *, char *);
int /*<<< orphan*/ vuln_func(char *);
int main(int argc, char *argv[]) {
  char str[1024];
  gets(str);
  if (strstr(str, "nullptr_read")) {
    char *nullptr = NULL;
    printf("%s\n", nullptr);
  }
  if (strstr(str, "nullptr_write")) {
    char *nullptr = NULL;
    strcpy(nullptr, str);
  }
  if (strlen(str) > 100) {
    while (1)
      ;
  }
  vuln_func(str);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
call   1070 <gets@plt>
lea    -0x410(%rbp),%rdi
lea    0xe6d(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     11c0 <main+0x60>
movq   $0x0,-0x418(%rbp)
mov    -0x418(%rbp),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     11fb <main+0x9b>
movq   $0x0,-0x420(%rbp)
mov    -0x420(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x64,%eax
jle    121a <main+0xba>
jmp    1215 <main+0xb5>
jmp    1215 <main+0xb5>
lea    -0x410(%rbp),%rdi
call   1060 <vuln_func@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x400,%rsp
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1070 <gets@plt>
lea    0xe86(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     1195 <main+0x35>
lea    0xe7f(%rip),%rdi
xor    %esi,%esi
call   1050 <printf@plt>
lea    0xe75(%rip),%rsi
mov    %rsp,%rdi
call   1080 <strstr@plt>
test   %rax,%rax
je     11b3 <main+0x53>
mov    %rsp,%rsi
xor    %edi,%edi
call   1030 <strcpy@plt>
mov    %rsp,%rdi
call   1040 <strlen@plt>
cmp    $0x65,%eax
jl     11c2 <main+0x62>
jmp    11c0 <main+0x60>
mov    %rsp,%rdi
call   1060 <vuln_func@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"nullptr_read\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [14 x i8] c"nullptr_write\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = call i32 @gets(i8* %9)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%12 = call i64 @strstr(i8* %11, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
store i8* null, i8** %7
%15 = load i8*, i8** %7
%16 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %2
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%19 = call i64 @strstr(i8* %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
store i8* null, i8** %8
%22 = load i8*, i8** %8
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%24 = call i32 @strcpy(i8* %22, i8* %23)
br label %25
25: ; preds = %21, %17
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = icmp sgt i32 %27, 100
br i1 %28, label %29, label %31
29: ; preds = %25
br label %30
30: ; preds = %29, %30
br label %30
31: ; preds = %25
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%33 = call i32 @vuln_func(i8* %32)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"nullptr_read\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [14 x i8] c"nullptr_write\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = call i32 @gets(i8* %4)
%6 = call i64 @strstr(i8* %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* null)
br label %10
10: ; preds = %8, %2
%11 = call i64 @strstr(i8* %4, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = call i32 @strcpy(i8* null, i8* %4)
br label %15
15: ; preds = %13, %10
%16 = call i32 @strlen(i8* %4)
%17 = icmp sgt i32 %16, 100
br i1 %17, label %18, label %19
18: ; preds = %15, %18
br label %18
19: ; preds = %15
%20 = call i32 @vuln_func(i8* %4)
ret i32 0
}