#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*/ encrypt(unsigned char **, unsigned char const *, int);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
unsigned int detect_prefix_block_pad(unsigned int pref_block_num) {
  const unsigned char three_blocks_of_a[3 * (16) + 1] =
      "PPPPPPPPPPPPPPPPPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  unsigned char *cipher;
  unsigned int i;
  for (i = 1; i < (16) + 1; i++) {
    encrypt(&cipher, three_blocks_of_a + ((16) + 1 - i), 2 * (16) + i);
    if (!strncmp((char *)cipher + (pref_block_num + 1) * (16),
                 (char *)cipher + (pref_block_num + 2) * (16), (16))) {
      free(cipher);
      break;
    }
    free(cipher);
  }
  return (i) ? (16) - i : 0;
}


x86 assembly (clang, -O0):
<detect_prefix_block_pad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x40(%rbp),%rdi
lea    0xeaa(%rip),%rsi
mov    $0x31,%edx
call   1060 <memcpy@plt>
movl   $0x1,-0x4c(%rbp)
cmpl   $0x11,-0x4c(%rbp)
jae    11ed <detect_prefix_block_pad+0xad>
lea    -0x40(%rbp),%rsi
mov    $0x11,%eax
sub    -0x4c(%rbp),%eax
mov    %eax,%eax
add    %rax,%rsi
mov    -0x4c(%rbp),%edx
add    $0x20,%edx
lea    -0x48(%rbp),%rdi
call   1050 <encrypt@plt>
mov    -0x48(%rbp),%rdi
mov    -0x4(%rbp),%eax
add    $0x1,%eax
shl    $0x4,%eax
mov    %eax,%eax
add    %rax,%rdi
mov    -0x48(%rbp),%rsi
mov    -0x4(%rbp),%eax
add    $0x2,%eax
shl    $0x4,%eax
mov    %eax,%eax
add    %rax,%rsi
mov    $0x10,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    11d6 <detect_prefix_block_pad+0x96>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    11ed <detect_prefix_block_pad+0xad>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1167 <detect_prefix_block_pad+0x27>
cmpl   $0x0,-0x4c(%rbp)
je     1207 <detect_prefix_block_pad+0xc7>
mov    $0x10,%eax
sub    -0x4c(%rbp),%eax
mov    %eax,-0x50(%rbp)
jmp    1211 <detect_prefix_block_pad+0xd1>
xor    %eax,%eax
mov    %eax,-0x50(%rbp)
jmp    1211 <detect_prefix_block_pad+0xd1>
mov    -0x50(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detect_prefix_block_pad>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x50,%rsp
mov    %edi,%r14d
movaps 0xed9(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xebd(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xea1(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movb   $0x41,0x40(%rsp)
lea    0x20(%rsp),%r12
shl    $0x4,%r14d
lea    0x10(%r14),%r13d
add    $0x20,%r14d
xor    %ebx,%ebx
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x21(%rbx),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1050 <encrypt@plt>
mov    0x8(%rsp),%rsi
lea    (%rsi,%r13,1),%rdi
add    %r14,%rsi
mov    $0x10,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     11d7 <detect_prefix_block_pad+0xa7>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%r12
cmp    $0x10,%rbx
jne    1190 <detect_prefix_block_pad+0x60>
mov    $0xffffffff,%eax
jmp    11e8 <detect_prefix_block_pad+0xb8>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0xf,%eax
sub    %ebx,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.detect_prefix_block_pad.three_blocks_of_a = [49 x i8] c"PPPPPPPPPPPPPPPPPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
define i32 @detect_prefix_block_pad(i32 %0) {
%2 = alloca i32
%3 = alloca [49 x i8]
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = bitcast [49 x i8]* %3 to i8*
store i32 1, i32* %5
br label %7
7: ; preds = %39, %1
%8 = load i32, i32* %5
%9 = icmp ult i32 %8, 17
br i1 %9, label %10, label %42
10: ; preds = %7
%11 = getelementptr inbounds [49 x i8], [49 x i8]* %3, i64 0, i64 0
%12 = load i32, i32* %5
%13 = sub i32 17, %12
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %11, i64 %14
%16 = load i32, i32* %5
%17 = add i32 32, %16
%18 = call i32 @encrypt(i8** %4, i8* %15, i32 %17)
%19 = load i8*, i8** %4
%20 = load i32, i32* %2
%21 = add i32 %20, 1
%22 = mul i32 %21, 16
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %19, i64 %23
%25 = load i8*, i8** %4
%26 = load i32, i32* %2
%27 = add i32 %26, 2
%28 = mul i32 %27, 16
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %25, i64 %29
%31 = call i32 @strncmp(i8* %24, i8* %30, i32 16)
%32 = icmp ne i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %10
%34 = load i8*, i8** %4
%35 = call i32 @free(i8* %34)
br label %42
36: ; preds = %10
%37 = load i8*, i8** %4
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %36
%40 = load i32, i32* %5
%41 = add i32 %40, 1
store i32 %41, i32* %5
br label %7
42: ; preds = %33, %7
%43 = load i32, i32* %5
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* %5
%47 = sub i32 16, %46
br label %49
48: ; preds = %42
br label %49
49: ; preds = %48, %45
%50 = phi i32 [ %47, %45 ], [ 0, %48 ]
ret i32 %50
}

LLVM IR (-O2):
@__const.detect_prefix_block_pad.three_blocks_of_a = [49 x i8] c"PPPPPPPPPPPPPPPPPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
define i32 @detect_prefix_block_pad(i32 %0) {
%2 = alloca [49 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [49 x i8], [49 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
%6 = shl i32 %0, 4
%7 = add i32 %6, 16
%8 = zext i32 %7 to i64
%9 = add i32 %6, 32
%10 = zext i32 %9 to i64
br label %11
11: ; preds = %1, %28
%12 = phi i64 [ 1, %1 ], [ %31, %28 ]
%13 = sub nuw nsw i64 17, %12
%14 = getelementptr inbounds [49 x i8], [49 x i8]* %2, i64 0, i64 %13
%15 = trunc i64 %12 to i32
%16 = add i32 %15, 32
%17 = call i32 @encrypt(i8** %3, i8* %14, i32 %16)
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i64 %8
%20 = getelementptr inbounds i8, i8* %18, i64 %10
%21 = call i32 @strncmp(i8* %19, i8* %20, i32 16)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %11
%24 = trunc i64 %12 to i32
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
%27 = sub i32 16, %24
br label %33
28: ; preds = %11
%29 = load i8*, i8** %3
%30 = call i32 @free(i8* %29)
%31 = add nuw nsw i64 %12, 1
%32 = icmp eq i64 %31, 17
br i1 %32, label %33, label %11
33: ; preds = %28, %23
%34 = phi i32 [ %27, %23 ], [ -1, %28 ]
ret i32 %34
}