#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*/ strcpy(char *, char const *);
unsigned int strlen(char const *);
bool crypt_firmware(const char *key, unsigned char *buffer, size_t len) {
  char key_cpy[255];
  unsigned int i;
  unsigned int tmp = 0;
  int key_length = strlen(key);
  strcpy(key_cpy, key);
  for (i = 0; i < strlen(key); i++)
    key_cpy[i] = key[i] - 1;
  for (i = 0; i < len; i++) {
    buffer[i] ^= key_cpy[tmp] | 0x80;
    tmp = (tmp + 1) % key_length;
  }
  return true;
}


x86 assembly (clang, -O0):
<crypt_firmware>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x128(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x12c(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x124(%rbp)
mov    -0x124(%rbp),%eax
mov    %eax,-0x130(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x130(%rbp),%eax
cmp    %ecx,%eax
jae    11c3 <crypt_firmware+0xa3>
mov    -0x8(%rbp),%rax
mov    -0x124(%rbp),%ecx
movsbl (%rax,%rcx,1),%eax
sub    $0x1,%eax
mov    %al,%cl
mov    -0x124(%rbp),%eax
mov    %cl,-0x120(%rbp,%rax,1)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    116a <crypt_firmware+0x4a>
movl   $0x0,-0x124(%rbp)
mov    -0x124(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    122f <crypt_firmware+0x10f>
mov    -0x128(%rbp),%eax
movsbl -0x120(%rbp,%rax,1),%esi
ff
or     $0x80,%esi
mov    -0x10(%rbp),%rax
mov    -0x124(%rbp),%ecx
movzbl (%rax,%rcx,1),%edx
xor    %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x128(%rbp),%eax
add    $0x1,%eax
xor    %edx,%edx
divl   -0x12c(%rbp)
mov    %edx,-0x128(%rbp)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    11cd <crypt_firmware+0xad>
mov    $0x1,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypt_firmware>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
je     117c <crypt_firmware+0x5c>
xor    %ebp,%ebp
nopl   0x0(%rax)
movzbl (%rbx,%rbp,1),%eax
add    $0xff,%al
mov    %al,(%rsp,%rbp,1)
add    $0x1,%rbp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%eax
cmp    %rax,%rbp
jb     1160 <crypt_firmware+0x40>
test   %r15,%r15
je     11ae <crypt_firmware+0x8e>
xor    %ecx,%ecx
xor    %edx,%edx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %edx,%eax
movzbl (%rsp,%rax,1),%eax
or     $0x80,%al
xor    %al,(%r14,%rcx,1)
add    $0x1,%edx
mov    %edx,%eax
xor    %edx,%edx
div    %r12d
add    $0x1,%ecx
cmp    %r15,%rcx
jb     1190 <crypt_firmware+0x70>
mov    $0x1,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @crypt_firmware(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [255 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
store i32 0, i32* %9
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %10
%13 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%14 = load i8*, i8** %4
%15 = call i32 @strcpy(i8* %13, i8* %14)
store i32 0, i32* %8
br label %16
16: ; preds = %33, %3
%17 = load i32, i32* %8
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = icmp ult i32 %17, %19
br i1 %20, label %21, label %36
21: ; preds = %16
%22 = load i8*, i8** %4
%23 = load i32, i32* %8
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = sub nsw i32 %27, 1
%29 = trunc i32 %28 to i8
%30 = load i32, i32* %8
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 %31
store i8 %29, i8* %32
br label %33
33: ; preds = %21
%34 = load i32, i32* %8
%35 = add i32 %34, 1
store i32 %35, i32* %8
br label %16
36: ; preds = %16
store i32 0, i32* %8
br label %37
37: ; preds = %61, %36
%38 = load i32, i32* %8
%39 = zext i32 %38 to i64
%40 = load i64, i64* %6
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %64
42: ; preds = %37
%43 = load i32, i32* %9
%44 = zext i32 %43 to i64
%45 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = or i32 %47, 128
%49 = load i8*, i8** %5
%50 = load i32, i32* %8
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %49, i64 %51
%53 = load i8, i8* %52
%54 = zext i8 %53 to i32
%55 = xor i32 %54, %48
%56 = trunc i32 %55 to i8
store i8 %56, i8* %52
%57 = load i32, i32* %9
%58 = add i32 %57, 1
%59 = load i32, i32* %10
%60 = urem i32 %58, %59
store i32 %60, i32* %9
br label %61
61: ; preds = %42
%62 = load i32, i32* %8
%63 = add i32 %62, 1
store i32 %63, i32* %8
br label %37
64: ; preds = %37
ret i32 1
}

LLVM IR (-O2):
define i32 @crypt_firmware(i8* %0, i8* nocapture %1, i64 %2) {
%4 = alloca [255 x i8]
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %0)
%7 = call i32 @strcpy(i8* %5, i8* %0)
%8 = call i32 @strlen(i8* %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %12, %3
%11 = icmp eq i64 %2, 0
br i1 %11, label %37, label %22
12: ; preds = %3, %12
%13 = phi i64 [ %18, %12 ], [ 0, %3 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = add i8 %15, -1
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %13
store i8 %16, i8* %17
%18 = add nuw nsw i64 %13, 1
%19 = call i32 @strlen(i8* %0)
%20 = zext i32 %19 to i64
%21 = icmp ult i64 %18, %20
br i1 %21, label %12, label %10
22: ; preds = %10, %22
%23 = phi i64 [ %35, %22 ], [ 0, %10 ]
%24 = phi i32 [ %33, %22 ], [ 0, %10 ]
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = or i8 %27, -128
%29 = getelementptr inbounds i8, i8* %1, i64 %23
%30 = load i8, i8* %29
%31 = xor i8 %28, %30
store i8 %31, i8* %29
%32 = add i32 %24, 1
%33 = urem i32 %32, %6
%34 = add nuw nsw i64 %23, 1
%35 = and i64 %34, 4294967295
%36 = icmp ult i64 %35, %2
br i1 %36, label %22, label %37
37: ; preds = %22, %10
ret i32 1
}