#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_affine(unsigned char *data, size_t datalen, int a, int b,
                  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]);
    unsigned int res = idx * a + b;
    if (idx == -1) {
      printf("Out of bound data, unable to cipher_affine!\n");
      return 1;
    }
    out[i] = alphabet[res % alphabetlen];
  }
  return 0;
}


x86 assembly (clang, -O0):
<cipher_affine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d9 <cipher_affine+0xb9>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
call   1030 <find_in_data@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
imul   -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11a3 <cipher_affine+0x83>
lea    0xe6e(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e0 <cipher_affine+0xc0>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%eax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x30(%rbp)
mov    -0x48(%rbp),%rax
mov    (%rax,%rdx,1),%dl
mov    0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    114a <cipher_affine+0x2a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cipher_affine>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,0x4(%rsp)
xor    %ebx,%ebx
test   %rsi,%rsi
je     11a6 <cipher_affine+0x86>
mov    %r9,%r14
mov    %r8,%r15
mov    %edx,%r13d
mov    %rsi,%rbp
mov    %rdi,%r12
jmp    1166 <cipher_affine+0x46>
nopw   0x0(%rax,%rax,1)
movzbl (%r15,%rax,1),%eax
mov    0x40(%rsp),%rcx
mov    %al,(%rcx,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
je     1191 <cipher_affine+0x71>
movzbl (%r12,%rbx,1),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <find_in_data@plt>
cmp    $0xffffffff,%eax
je     1195 <cipher_affine+0x75>
imul   %r13d,%eax
add    0x4(%rsp),%eax
cmp    %r14,%rax
jb     1150 <cipher_affine+0x30>
xor    %edx,%edx
div    %r14d
mov    %edx,%eax
jmp    1150 <cipher_affine+0x30>
xor    %ebx,%ebx
jmp    11a6 <cipher_affine+0x86>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
mov    %ebx,%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_affine!\0A\00"
define i32 @cipher_affine(i8* %0, i64 %1, i32 %2, i32 %3, i8* %4, i64 %5, i8* %6) {
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %9
store i64 %1, i64* %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store i8* %4, i8** %13
store i64 %5, i64* %14
store i8* %6, i8** %15
store i64 0, i64* %16
br label %19
19: ; preds = %51, %7
%20 = load i64, i64* %16
%21 = load i64, i64* %10
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %54
23: ; preds = %19
%24 = load i8*, i8** %13
%25 = load i64, i64* %14
%26 = load i8*, i8** %9
%27 = load i64, i64* %16
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = load i8, i8* %28
%30 = call i32 @find_in_data(i8* %24, i64 %25, i8 zeroext %29)
store i32 %30, i32* %17
%31 = load i32, i32* %17
%32 = load i32, i32* %11
%33 = mul nsw i32 %31, %32
%34 = load i32, i32* %12
%35 = add nsw i32 %33, %34
store i32 %35, i32* %18
%36 = load i32, i32* %17
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %23
%39 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %8
br label %55
40: ; preds = %23
%41 = load i8*, i8** %13
%42 = load i32, i32* %18
%43 = zext i32 %42 to i64
%44 = load i64, i64* %14
%45 = urem i64 %43, %44
%46 = getelementptr inbounds i8, i8* %41, i64 %45
%47 = load i8, i8* %46
%48 = load i8*, i8** %15
%49 = load i64, i64* %16
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 %47, i8* %50
br label %51
51: ; preds = %40
%52 = load i64, i64* %16
%53 = add i64 %52, 1
store i64 %53, i64* %16
br label %19
54: ; preds = %19
store i32 0, i32* %8
br label %55
55: ; preds = %54, %38
%56 = load i32, i32* %8
ret i32 %56
}

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