#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;

/* Variables and functions */
int find_in_data(unsigned char *, size_t, unsigned char);
int /*<<< orphan*/ printf(char *);
int cipher_substitution(unsigned char *data, size_t datalen,
                        unsigned char *sortedalphabet, unsigned char *alphabet,
                        size_t alphabetlen, unsigned char *out) {
  size_t i;
  for (i = 0; i < datalen; ++i) {
    ssize_t idx = find_in_data(alphabet, alphabetlen, data[i]);
    if (idx == -1) {
      printf("Out of bound data, unable to cipher_substi!\n");
      return 1;
    }
    out[i] = sortedalphabet[idx];
  }
  return 0;
}


x86 assembly (clang, -O0):
<cipher_substitution>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11bb <cipher_substitution+0x9b>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
call   1030 <find_in_data@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1194 <cipher_substitution+0x74>
lea    0xe7d(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c2 <cipher_substitution+0xa2>
mov    -0x20(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x38(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1148 <cipher_substitution+0x28>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cipher_substitution>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,(%rsp)
test   %rsi,%rsi
je     117e <cipher_substitution+0x5e>
mov    %r8,%r15
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%rbp
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
movzbl 0x0(%rbp,%r14,1),%edx
mov    %r13,%rdi
mov    %r15,%rsi
call   1040 <find_in_data@plt>
cmp    $0xffffffff,%eax
je     1182 <cipher_substitution+0x62>
cltq
movzbl (%r12,%rax,1),%eax
mov    (%rsp),%rcx
mov    %al,(%rcx,%r14,1)
add    $0x1,%r14
cmp    %r14,%rbx
jne    1150 <cipher_substitution+0x30>
xor    %eax,%eax
jmp    1193 <cipher_substitution+0x73>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [45 x i8] c"Out of bound data, unable to cipher_substi!\0A\00"
define i32 @cipher_substitution(i8* %0, i64 %1, i8* %2, i8* %3, i64 %4, i8* %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
store i8* %0, i8** %8
store i64 %1, i64* %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i64 %4, i64* %12
store i8* %5, i8** %13
store i64 0, i64* %14
br label %16
16: ; preds = %41, %6
%17 = load i64, i64* %14
%18 = load i64, i64* %9
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %44
20: ; preds = %16
%21 = load i8*, i8** %11
%22 = load i64, i64* %12
%23 = load i8*, i8** %8
%24 = load i64, i64* %14
%25 = getelementptr inbounds i8, i8* %23, i64 %24
%26 = load i8, i8* %25
%27 = call i32 @find_in_data(i8* %21, i64 %22, i8 zeroext %26)
store i32 %27, i32* %15
%28 = load i32, i32* %15
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %7
br label %45
32: ; preds = %20
%33 = load i8*, i8** %10
%34 = load i32, i32* %15
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = load i8*, i8** %13
%39 = load i64, i64* %14
%40 = getelementptr inbounds i8, i8* %38, i64 %39
store i8 %37, i8* %40
br label %41
41: ; preds = %32
%42 = load i64, i64* %14
%43 = add i64 %42, 1
store i64 %43, i64* %14
br label %16
44: ; preds = %16
store i32 0, i32* %7
br label %45
45: ; preds = %44, %30
%46 = load i32, i32* %7
ret i32 %46
}

LLVM IR (-O2):
@str = [44 x i8] c"Out of bound data, unable to cipher_substi!\00"
define i32 @cipher_substitution(i8* nocapture %0, i64 %1, i8* nocapture %2, i8* %3, i64 %4, i8* nocapture writeonly %5) {
%7 = icmp eq i64 %1, 0
br i1 %7, label %23, label %8
8: ; preds = %6, %16
%9 = phi i64 [ %21, %16 ], [ 0, %6 ]
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = tail call i32 @find_in_data(i8* %3, i64 %4, i8 zeroext %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
br label %23
16: ; preds = %8
%17 = sext i32 %12 to i64
%18 = getelementptr inbounds i8, i8* %2, i64 %17
%19 = load i8, i8* %18
%20 = getelementptr inbounds i8, i8* %5, i64 %9
store i8 %19, i8* %20
%21 = add nuw i64 %9, 1
%22 = icmp eq i64 %21, %1
br i1 %22, label %23, label %8
23: ; preds = %16, %6, %14
%24 = phi i32 [ 1, %14 ], [ 0, %6 ], [ 0, %16 ]
ret i32 %24
}