#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*/ memcpy(char *, unsigned char *, int const);
int strlen(char *);
void encrypt_old(unsigned char *buf, int size) {
  char key[] = "SCEEDEMO2DISC";
  const int keylen = strlen(key);
  unsigned char tmp[keylen];
  int i, j;
  for (i = 0, j = 0; i < size; i++, j++) {
    if (j == keylen) {
      memcpy(key, tmp, keylen);
      j = 0;
    }
    tmp[j] = buf[i];
    buf[i] ^= key[j];
  }
}


x86 assembly (clang, -O0):
<encrypt_old>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movabs $0x43534944324f4d,%rax
mov    %rax,-0x1a(%rbp)
movabs $0x4f4d454445454353,%rax
45 4d 4f
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1201 <encrypt_old+0xe1>
mov    -0x40(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11ba <encrypt_old+0x9a>
mov    -0x48(%rbp),%rsi
lea    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x40(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x8(%rbp),%rcx
movslq -0x3c(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x40(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
movslq -0x40(%rbp),%rax
movsbl -0x20(%rbp,%rax,1),%esi
mov    -0x8(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
xor    %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    118b <encrypt_old+0x6b>
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encrypt_old>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r12d
mov    %rdi,%r15
movabs $0x43534944324f4d,%rax
mov    %rax,-0x32(%rbp)
movabs $0x4f4d454445454353,%rax
45 4d 4f
mov    %rax,-0x38(%rbp)
lea    -0x38(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%r14d
mov    %rsp,-0x40(%rbp)
mov    %eax,%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
test   %r12d,%r12d
jle    11c6 <encrypt_old+0xa6>
mov    %r12d,%r13d
xor    %r12d,%r12d
xor    %eax,%eax
jmp    11ae <encrypt_old+0x8e>
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%r15,%r12,1),%ecx
cltq
mov    %cl,(%rbx,%rax,1)
xor    -0x38(%rbp,%rax,1),%cl
mov    %cl,(%r15,%r12,1)
add    $0x1,%r12
add    $0x1,%eax
cmp    %r12,%r13
je     11c6 <encrypt_old+0xa6>
cmp    %r14d,%eax
jne    1190 <encrypt_old+0x70>
lea    -0x38(%rbp),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <memcpy@plt>
xor    %eax,%eax
jmp    1190 <encrypt_old+0x70>
mov    -0x40(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.encrypt_old.key = [14 x i8] c"SCEEDEMO2DISC\00"
define void @encrypt_old(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [14 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%11 = bitcast [14 x i8]* %5 to i8*
%12 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %7
%17 = alloca i8, i64 %15
store i64 %15, i64* %8
store i32 0, i32* %9
store i32 0, i32* %10
br label %18
18: ; preds = %52, %2
%19 = load i32, i32* %9
%20 = load i32, i32* %4
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %57
22: ; preds = %18
%23 = load i32, i32* %10
%24 = load i32, i32* %6
%25 = icmp eq i32 %23, %24
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 0
%28 = load i32, i32* %6
%29 = call i32 @memcpy(i8* %27, i8* %17, i32 %28)
store i32 0, i32* %10
br label %30
30: ; preds = %26, %22
%31 = load i8*, i8** %3
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i8, i8* %34
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %17, i64 %37
store i8 %35, i8* %38
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [14 x i8], [14 x i8]* %5, i64 0, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = load i8*, i8** %3
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
%48 = load i8, i8* %47
%49 = zext i8 %48 to i32
%50 = xor i32 %49, %43
%51 = trunc i32 %50 to i8
store i8 %51, i8* %47
br label %52
52: ; preds = %30
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
store i32 %54, i32* %9
%55 = load i32, i32* %10
%56 = add nsw i32 %55, 1
store i32 %56, i32* %10
br label %18
57: ; preds = %18
%58 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@__const.encrypt_old.key = [14 x i8] c"SCEEDEMO2DISC\00"
define void @encrypt_old(i8* nocapture %0, i32 %1) {
%3 = alloca [14 x i8]
%4 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%5 = call i32 @strlen(i8* %4)
%6 = zext i32 %5 to i64
%7 = call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = icmp sgt i32 %1, 0
br i1 %9, label %10, label %30
10: ; preds = %2
%11 = zext i32 %1 to i64
br label %12
12: ; preds = %10, %18
%13 = phi i64 [ 0, %10 ], [ %27, %18 ]
%14 = phi i32 [ 0, %10 ], [ %28, %18 ]
%15 = icmp eq i32 %14, %5
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @memcpy(i8* %4, i8* %8, i32 %5)
br label %18
18: ; preds = %16, %12
%19 = phi i32 [ 0, %16 ], [ %14, %12 ]
%20 = getelementptr inbounds i8, i8* %0, i64 %13
%21 = load i8, i8* %20
%22 = sext i32 %19 to i64
%23 = getelementptr inbounds i8, i8* %8, i64 %22
store i8 %21, i8* %23
%24 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 %22
%25 = load i8, i8* %24
%26 = xor i8 %25, %21
store i8 %26, i8* %20
%27 = add nuw nsw i64 %13, 1
%28 = add nsw i32 %19, 1
%29 = icmp eq i64 %27, %11
br i1 %29, label %30, label %12
30: ; preds = %18, %2
ret void
}