#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 /*<<< orphan*/ uint8_t;
typedef int uint32_t;

/* Variables and functions */
int crc32buf(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/ *);
scalar_t__ memcmp(int /*<<< orphan*/ *, char *, int);
uint32_t patch_calc_patch_crc(uint8_t *patch, size_t patch_size) {
  uint32_t patch_crc;
  size_t size;
  if (patch_size < 16)
    return -1;
  if ((memcmp(patch, "UPS1", 4) == 0) || (memcmp(patch, "BPS1", 4) == 0)) {
    size = patch_size - 4;
  } else {
    size = patch_size;
  }
  patch_crc = crc32buf(patch, size, NULL);
  return patch_crc;
}


x86 assembly (clang, -O0):
<patch_calc_patch_crc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x10,-0x18(%rbp)
jae    1147 <patch_calc_patch_crc+0x27>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b8 <patch_calc_patch_crc+0x98>
mov    -0x10(%rbp),%rdi
lea    0xeae(%rip),%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     1185 <patch_calc_patch_crc+0x65>
mov    -0x10(%rbp),%rdi
lea    0xe94(%rip),%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    1196 <patch_calc_patch_crc+0x76>
mov    -0x18(%rbp),%rax
sub    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    119e <patch_calc_patch_crc+0x7e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <crc32buf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<patch_calc_patch_crc>:
cmp    $0x10,%rsi
jae    112c <patch_calc_patch_crc+0xc>
mov    $0xffffffff,%eax
ret
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xec3(%rip),%rsi
mov    $0x4,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     1165 <patch_calc_patch_crc+0x45>
lea    0xeb2(%rip),%rsi
mov    %r14,%rdi
mov    $0x4,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
jne    1169 <patch_calc_patch_crc+0x49>
add    $0xfffffffffffffffc,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <crc32buf@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"UPS1\00"
@.str.1 = [5 x i8] c"BPS1\00"
define i32 @patch_calc_patch_crc(i32* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
store i32* %0, i32** %4
store i64 %1, i64* %5
%8 = load i64, i64* %5
%9 = icmp ult i64 %8, 16
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %29
11: ; preds = %2
%12 = load i32*, i32** %4
%13 = call i64 @memcmp(i32* %12, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32*, i32** %4
%17 = call i64 @memcmp(i32* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15, %11
%20 = load i64, i64* %5
%21 = sub i64 %20, 4
store i64 %21, i64* %7
br label %24
22: ; preds = %15
%23 = load i64, i64* %5
store i64 %23, i64* %7
br label %24
24: ; preds = %22, %19
%25 = load i32*, i32** %4
%26 = load i64, i64* %7
%27 = call i32 @crc32buf(i32* %25, i64 %26, i32* null)
store i32 %27, i32* %6
%28 = load i32, i32* %6
store i32 %28, i32* %3
br label %29
29: ; preds = %24, %10
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [5 x i8] c"UPS1\00"
@.str.1 = [5 x i8] c"BPS1\00"
define i32 @patch_calc_patch_crc(i32* %0, i64 %1) {
%3 = icmp ult i64 %1, 16
br i1 %3, label %15, label %4
4: ; preds = %2
%5 = tail call i64 @memcmp(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i64 @memcmp(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %4
%11 = add i64 %1, -4
br label %12
12: ; preds = %7, %10
%13 = phi i64 [ %11, %10 ], [ %1, %7 ]
%14 = tail call i32 @crc32buf(i32* %0, i64 %13, i32* null)
br label %15
15: ; preds = %2, %12
%16 = phi i32 [ %14, %12 ], [ -1, %2 ]
ret i32 %16
}