#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 */
scalar_t__ isprint(char);
int strlen(char *);
void strip_non_printable(char *payload) {
  int i = 0;
  int char_seen = 0;
  while (i < strlen(payload)) {
    if (isprint(payload[i]) > 0) {
      payload[char_seen] = payload[i];
      ++char_seen;
    }
    ++i;
  }
  while (char_seen < strlen(payload)) {
    payload[char_seen] = 0;
    char_seen++;
  }
}


x86 assembly (clang, -O0):
<strip_non_printable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x14(%rbp),%eax
cmp    %ecx,%eax
jge    119e <strip_non_printable+0x7e>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isprint@plt>
cmp    $0x0,%rax
jle    1190 <strip_non_printable+0x70>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113a <strip_non_printable+0x1a>
jmp    11a3 <strip_non_printable+0x83>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
cmp    %ecx,%eax
jge    11d9 <strip_non_printable+0xb9>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a3 <strip_non_printable+0x83>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strip_non_printable>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1030 <strlen@plt>
test   %eax,%eax
jle    1174 <strip_non_printable+0x54>
xor    %ebx,%ebx
xor    %r14d,%r14d
jmp    1153 <strip_non_printable+0x33>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jge    1177 <strip_non_printable+0x57>
movsbl (%r15,%rbx,1),%edi
call   1040 <isprint@plt>
test   %rax,%rax
jle    1140 <strip_non_printable+0x20>
movzbl (%r15,%rbx,1),%eax
movslq %r14d,%r14
mov    %al,(%r15,%r14,1)
add    $0x1,%r14d
jmp    1140 <strip_non_printable+0x20>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1030 <strlen@plt>
cmp    %eax,%r14d
jge    11a8 <strip_non_printable+0x88>
movslq %r14d,%rbx
nopw   0x0(%rax,%rax,1)
movb   $0x0,(%r15,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1190 <strip_non_printable+0x70>
pop    %rbx
pop    %r14
pop    %r15
ret

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

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