#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 */
unsigned int encrypt(unsigned char **, unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
unsigned int detect_prefix_block_num(unsigned long block_len) {
  unsigned char *cipher0, *cipher1;
  unsigned int clen0, clen1, i;
  clen0 = encrypt(&cipher0, NULL, 0);
  if (NULL == cipher0 || 0 == clen0)
    return 0;
  clen1 = encrypt(&cipher1, (unsigned char *)"A", 1);
  if (NULL == cipher1 || 0 == clen1) {
    free(cipher0);
    return 0;
  }
  i = 0;
  while (i < clen1 && i < clen0 && cipher0[i] == cipher1[i])
    i++;
  free(cipher0);
  free(cipher1);
  return i / block_len;
}


x86 assembly (clang, -O0):
<detect_prefix_block_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <encrypt@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
je     1155 <detect_prefix_block_num+0x35>
xor    %eax,%eax
cmp    -0x24(%rbp),%eax
jne    1161 <detect_prefix_block_num+0x41>
movl   $0x0,-0x4(%rbp)
jmp    122a <detect_prefix_block_num+0x10a>
lea    -0x20(%rbp),%rdi
lea    0xe94(%rip),%rsi
mov    $0x1,%edx
call   1040 <encrypt@plt>
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
je     1190 <detect_prefix_block_num+0x70>
xor    %eax,%eax
cmp    -0x28(%rbp),%eax
jne    11a5 <detect_prefix_block_num+0x85>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    122a <detect_prefix_block_num+0x10a>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x28(%rbp),%ecx
mov    %al,-0x2d(%rbp)
jae    11ec <detect_prefix_block_num+0xcc>
mov    -0x2c(%rbp),%ecx
xor    %eax,%eax
cmp    -0x24(%rbp),%ecx
mov    %al,-0x2d(%rbp)
jae    11ec <detect_prefix_block_num+0xcc>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
movzbl (%rax,%rcx,1),%eax
mov    -0x20(%rbp),%rcx
mov    -0x2c(%rbp),%edx
movzbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11fc <detect_prefix_block_num+0xdc>
jmp    120a <detect_prefix_block_num+0xea>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11ac <detect_prefix_block_num+0x8c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x10(%rbp)
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_prefix_block_num>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %ebx,%ebx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <encrypt@plt>
cmpq   $0x0,0x8(%rsp)
je     11c0 <detect_prefix_block_num+0xa0>
mov    %eax,%ebp
test   %eax,%eax
je     11c0 <detect_prefix_block_num+0xa0>
lea    0xeb0(%rip),%rsi
mov    %rsp,%rdi
mov    $0x1,%edx
call   1040 <encrypt@plt>
mov    (%rsp),%rcx
mov    0x8(%rsp),%rdi
test   %rcx,%rcx
je     11b7 <detect_prefix_block_num+0x97>
test   %eax,%eax
je     11b7 <detect_prefix_block_num+0x97>
mov    %ebp,%edx
mov    %eax,%esi
xor    %eax,%eax
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%rdi,%rax,1),%ebx
cmp    (%rcx,%rax,1),%bl
jne    1197 <detect_prefix_block_num+0x77>
add    $0x1,%rax
cmp    %rsi,%rax
jae    1197 <detect_prefix_block_num+0x77>
cmp    %rdx,%rax
jb     1180 <detect_prefix_block_num+0x60>
mov    %eax,%ebx
call   1030 <free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
cmp    %r14,%rbx
jb     11be <detect_prefix_block_num+0x9e>
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
mov    %eax,%ebx
jmp    11c0 <detect_prefix_block_num+0xa0>
call   1030 <free@plt>
jmp    11c0 <detect_prefix_block_num+0xa0>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"A\00"
define i32 @detect_prefix_block_num(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i64 %0, i64* %3
%9 = call i32 @encrypt(i8** %4, i8* null, i32 0)
store i32 %9, i32* %6
%10 = load i8*, i8** %4
%11 = icmp eq i8* null, %10
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i32, i32* %6
%14 = icmp eq i32 0, %13
br i1 %14, label %15, label %16
15: ; preds = %12, %1
store i32 0, i32* %2
br label %64
16: ; preds = %12
%17 = call i32 @encrypt(i8** %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %17, i32* %7
%18 = load i8*, i8** %5
%19 = icmp eq i8* null, %18
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* %7
%22 = icmp eq i32 0, %21
br i1 %22, label %23, label %26
23: ; preds = %20, %16
%24 = load i8*, i8** %4
%25 = call i32 @free(i8* %24)
store i32 0, i32* %2
br label %64
26: ; preds = %20
store i32 0, i32* %8
br label %27
27: ; preds = %51, %26
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = icmp ult i32 %28, %29
br i1 %30, label %31, label %49
31: ; preds = %27
%32 = load i32, i32* %8
%33 = load i32, i32* %6
%34 = icmp ult i32 %32, %33
br i1 %34, label %35, label %49
35: ; preds = %31
%36 = load i8*, i8** %4
%37 = load i32, i32* %8
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = load i8*, i8** %5
%43 = load i32, i32* %8
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = icmp eq i32 %41, %47
br label %49
49: ; preds = %35, %31, %27
%50 = phi i1 [ false, %31 ], [ false, %27 ], [ %48, %35 ]
br i1 %50, label %51, label %54
51: ; preds = %49
%52 = load i32, i32* %8
%53 = add i32 %52, 1
store i32 %53, i32* %8
br label %27
54: ; preds = %49
%55 = load i8*, i8** %4
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %5
%58 = call i32 @free(i8* %57)
%59 = load i32, i32* %8
%60 = zext i32 %59 to i64
%61 = load i64, i64* %3
%62 = udiv i64 %60, %61
%63 = trunc i64 %62 to i32
store i32 %63, i32* %2
br label %64
64: ; preds = %54, %23, %15
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
@.str = [2 x i8] c"A\00"
define i32 @detect_prefix_block_num(i64 %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = bitcast i8** %2 to i8*
%5 = bitcast i8** %3 to i8*
%6 = call i32 @encrypt(i8** %2, i8* null, i32 0)
%7 = load i8*, i8** %2
%8 = icmp eq i8* %7, null
%9 = icmp eq i32 %6, 0
%10 = select i1 %8, i1 true, i1 %9
br i1 %10, label %43, label %11
11: ; preds = %1
%12 = call i32 @encrypt(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1)
%13 = load i8*, i8** %3
%14 = icmp eq i8* %13, null
%15 = icmp eq i32 %12, 0
%16 = select i1 %14, i1 true, i1 %15
%17 = load i8*, i8** %2
br i1 %16, label %21, label %18
18: ; preds = %11
%19 = zext i32 %6 to i64
%20 = zext i32 %12 to i64
br label %23
21: ; preds = %11
%22 = call i32 @free(i8* %17)
br label %43
23: ; preds = %18, %30
%24 = phi i64 [ 0, %18 ], [ %31, %30 ]
%25 = getelementptr inbounds i8, i8* %17, i64 %24
%26 = load i8, i8* %25
%27 = getelementptr inbounds i8, i8* %13, i64 %24
%28 = load i8, i8* %27
%29 = icmp eq i8 %26, %28
br i1 %29, label %30, label %35
30: ; preds = %23
%31 = add nuw nsw i64 %24, 1
%32 = icmp ult i64 %31, %20
%33 = icmp ult i64 %31, %19
%34 = select i1 %32, i1 %33, i1 false
br i1 %34, label %23, label %35
35: ; preds = %23, %30
%36 = phi i64 [ %24, %23 ], [ %31, %30 ]
%37 = and i64 %36, 4294967295
%38 = call i32 @free(i8* %17)
%39 = load i8*, i8** %3
%40 = call i32 @free(i8* %39)
%41 = udiv i64 %37, %0
%42 = trunc i64 %41 to i32
br label %43
43: ; preds = %1, %35, %21
%44 = phi i32 [ 0, %21 ], [ %42, %35 ], [ 0, %1 ]
ret i32 %44
}