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

/* Variables and functions */
scalar_t__ memcmp(int *, char *, int);
int patch_get_output_crc(uint8_t *patch, size_t patch_size, uint32_t *crc) {
  if (patch_size < 16)
    return -1;
  if ((memcmp(patch, "UPS1", 4) == 0) || (memcmp(patch, "BPS1", 4) == 0)) {
    int offset = patch_size - 8;
    if (crc) {
      *crc = patch[offset];
      *crc |= patch[offset + 1] << 8;
      *crc |= patch[offset + 2] << 16;
      *crc |= patch[offset + 3] << 24;
    }
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<patch_get_output_crc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x10,-0x18(%rbp)
jae    113b <patch_get_output_crc+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1204 <patch_get_output_crc+0xf4>
mov    -0x10(%rbp),%rdi
lea    0xeba(%rip),%rsi
mov    $0x4,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
je     1179 <patch_get_output_crc+0x69>
mov    -0x10(%rbp),%rdi
lea    0xea0(%rip),%rsi
mov    $0x4,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    11fd <patch_get_output_crc+0xed>
mov    -0x18(%rbp),%rax
sub    $0x8,%rax
mov    %eax,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f1 <patch_get_output_crc+0xe1>
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
shl    $0x8,%ecx
mov    -0x20(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
shl    $0x10,%ecx
mov    -0x20(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x3,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%ecx
shl    $0x18,%ecx
mov    -0x20(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    1204 <patch_get_output_crc+0xf4>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<patch_get_output_crc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r15d
cmp    $0x10,%rsi
jb     11d6 <patch_get_output_crc+0xc6>
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xec9(%rip),%rsi
mov    $0x4,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
je     115f <patch_get_output_crc+0x4f>
lea    0xeb8(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
jne    11d6 <patch_get_output_crc+0xc6>
xor    %r15d,%r15d
test   %r14,%r14
je     11d6 <patch_get_output_crc+0xc6>
shl    $0x20,%rbp
movabs $0xfffffff800000000,%rax
ff ff ff
add    %rbp,%rax
sar    $0x1e,%rax
mov    (%rbx,%rax,1),%eax
mov    %eax,(%r14)
movabs $0xfffffff900000000,%rcx
ff ff ff
add    %rbp,%rcx
sar    $0x1e,%rcx
mov    (%rbx,%rcx,1),%ecx
shl    $0x8,%ecx
or     %eax,%ecx
mov    %ecx,(%r14)
movabs $0xfffffffa00000000,%rax
ff ff ff
add    %rbp,%rax
sar    $0x1e,%rax
mov    (%rbx,%rax,1),%eax
shl    $0x10,%eax
or     %ecx,%eax
mov    %eax,(%r14)
movabs $0xfffffffb00000000,%rcx
ff ff ff
add    %rbp,%rcx
sar    $0x1e,%rcx
mov    (%rbx,%rcx,1),%ecx
shl    $0x18,%ecx
or     %eax,%ecx
mov    %ecx,(%r14)
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"UPS1\00"
@.str.1 = [5 x i8] c"BPS1\00"
define i32 @patch_get_output_crc(i32* %0, i64 %1, i32* writeonly %2) {
%4 = icmp ult i64 %1, 16
br i1 %4, label %37, label %5
5: ; preds = %3
%6 = tail call i64 @memcmp(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %5
%9 = tail call i64 @memcmp(i32* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %37
11: ; preds = %8, %5
%12 = icmp eq i32* %2, null
br i1 %12, label %37, label %13
13: ; preds = %11
%14 = shl i64 %1, 32
%15 = add i64 %14, -34359738368
%16 = ashr exact i64 %15, 32
%17 = getelementptr inbounds i32, i32* %0, i64 %16
%18 = load i32, i32* %17
store i32 %18, i32* %2
%19 = add i64 %14, -30064771072
%20 = ashr exact i64 %19, 32
%21 = getelementptr inbounds i32, i32* %0, i64 %20
%22 = load i32, i32* %21
%23 = shl i32 %22, 8
%24 = or i32 %23, %18
store i32 %24, i32* %2
%25 = add i64 %14, -25769803776
%26 = ashr exact i64 %25, 32
%27 = getelementptr inbounds i32, i32* %0, i64 %26
%28 = load i32, i32* %27
%29 = shl i32 %28, 16
%30 = or i32 %29, %24
store i32 %30, i32* %2
%31 = add i64 %14, -21474836480
%32 = ashr exact i64 %31, 32
%33 = getelementptr inbounds i32, i32* %0, i64 %32
%34 = load i32, i32* %33
%35 = shl i32 %34, 24
%36 = or i32 %35, %30
store i32 %36, i32* %2
br label %37
37: ; preds = %8, %11, %13, %3
%38 = phi i32 [ -1, %3 ], [ 0, %13 ], [ 0, %11 ], [ -1, %8 ]
ret i32 %38
}