#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*/ strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *strstr(char const *, char *);
const char *rewrite_executable(const char *filename, char *buffer,
                               int buffer_len) {
  strcpy(buffer, "/data/data/io.neoterm/files/usr/bin/");
  char *bin_match = strstr(filename, "/bin/");
  if (bin_match == filename || bin_match == (filename + 4)) {
    strncpy(buffer + 36, bin_match + 5, (size_t)(buffer_len - 37));
    filename = buffer;
  }
  return filename;
}


x86 assembly (clang, -O0):
<rewrite_executable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rdi
lea    0xec1(%rip),%rsi
call   1050 <strstr@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     118a <rewrite_executable+0x5a>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jne    11b0 <rewrite_executable+0x80>
mov    -0x10(%rbp),%rdi
add    $0x24,%rdi
mov    -0x20(%rbp),%rsi
add    $0x5,%rsi
mov    -0x14(%rbp),%eax
sub    $0x25,%eax
movslq %eax,%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewrite_executable>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xebc(%rip),%rsi
mov    %rbp,%rdi
call   1040 <strcpy@plt>
lea    0xecc(%rip),%rsi
mov    %rbx,%rdi
call   1050 <strstr@plt>
cmp    %rbx,%rax
je     1169 <rewrite_executable+0x39>
lea    0x4(%rbx),%rcx
cmp    %rcx,%rax
jne    1183 <rewrite_executable+0x53>
lea    0x24(%rbp),%rdi
add    $0x5,%rax
add    $0xffffffdb,%r14d
movslq %r14d,%rdx
mov    %rax,%rsi
call   1030 <strncpy@plt>
mov    %rbp,%rbx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"/data/data/io.neoterm/files/usr/bin/\00"
@.str.1 = [6 x i8] c"/bin/\00"
define i8* @rewrite_executable(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%8 = load i8*, i8** %5
%9 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %4
%11 = call i8* @strstr(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = load i8*, i8** %4
%14 = icmp eq i8* %12, %13
br i1 %14, label %20, label %15
15: ; preds = %3
%16 = load i8*, i8** %7
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 4
%19 = icmp eq i8* %16, %18
br i1 %19, label %20, label %30
20: ; preds = %15, %3
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i64 36
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i64 5
%25 = load i32, i32* %6
%26 = sub nsw i32 %25, 37
%27 = sext i32 %26 to i64
%28 = call i32 @strncpy(i8* %22, i8* %24, i64 %27)
%29 = load i8*, i8** %5
store i8* %29, i8** %4
br label %30
30: ; preds = %20, %15
%31 = load i8*, i8** %4
ret i8* %31
}

LLVM IR (-O2):
@.str = [37 x i8] c"/data/data/io.neoterm/files/usr/bin/\00"
@.str.1 = [6 x i8] c"/bin/\00"
define i8* @rewrite_executable(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%5 = tail call i8* @strstr(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i8* %5, %0
%7 = getelementptr inbounds i8, i8* %0, i64 4
%8 = icmp eq i8* %5, %7
%9 = select i1 %6, i1 true, i1 %8
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = getelementptr inbounds i8, i8* %1, i64 36
%12 = getelementptr inbounds i8, i8* %5, i64 5
%13 = add nsw i32 %2, -37
%14 = sext i32 %13 to i64
%15 = tail call i32 @strncpy(i8* %11, i8* %12, i64 %14)
br label %16
16: ; preds = %3, %10
%17 = phi i8* [ %1, %10 ], [ %0, %3 ]
ret i8* %17
}