#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;

/* Variables and functions */
size_t decode(int /*<<< orphan*/ *, unsigned int *);
scalar_t__ memcmp(int /*<<< orphan*/ *, char *, int);
int patch_get_output_size(uint8_t *patch, size_t patch_size,
                          size_t *output_size) {
  unsigned patch_offset;
  size_t target_size;
  patch_offset = 0;
  if (patch_size < 4)
    return -1;
  if ((memcmp(patch, "UPS1", 4) == 0) || (memcmp(patch, "BPS1", 4) == 0)) {
    patch_offset += 4;
    target_size = decode(patch, &patch_offset);
    target_size = decode(patch, &patch_offset);
    if (output_size)
      *output_size = target_size;
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<patch_get_output_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x4,-0x18(%rbp)
jae    1152 <patch_get_output_size+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e4 <patch_get_output_size+0xc4>
mov    -0x10(%rbp),%rdi
lea    0xea3(%rip),%rsi
mov    $0x4,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
je     1190 <patch_get_output_size+0x70>
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
mov    $0x4,%edx
call   1030 <memcmp@plt>
cmp    $0x0,%rax
jne    11dd <patch_get_output_size+0xbd>
mov    -0x24(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1040 <decode@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x24(%rbp),%rsi
call   1040 <decode@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d1 <patch_get_output_size+0xb1>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11e4 <patch_get_output_size+0xc4>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<patch_get_output_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%ebp
cmp    $0x4,%rsi
jb     1195 <patch_get_output_size+0x75>
mov    %rdx,%r14
mov    %rdi,%rbx
lea    0xec0(%rip),%rsi
mov    $0x4,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
je     1168 <patch_get_output_size+0x48>
lea    0xeaf(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <memcmp@plt>
test   %rax,%rax
jne    1195 <patch_get_output_size+0x75>
movl   $0x4,0xc(%rsp)
lea    0xc(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <decode@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1040 <decode@plt>
xor    %ebp,%ebp
test   %r14,%r14
je     1195 <patch_get_output_size+0x75>
mov    %rax,(%r14)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
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_size(i32* %0, i64 %1, i64* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64*
%8 = alloca i32
%9 = alloca i64
store i32* %0, i32** %5
store i64 %1, i64* %6
store i64* %2, i64** %7
store i32 0, i32* %8
%10 = load i64, i64* %6
%11 = icmp ult i64 %10, 4
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 -1, i32* %4
br label %35
13: ; preds = %3
%14 = load i32*, i32** %5
%15 = call i64 @memcmp(i32* %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %13
%18 = load i32*, i32** %5
%19 = call i64 @memcmp(i32* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %34
21: ; preds = %17, %13
%22 = load i32, i32* %8
%23 = add i32 %22, 4
store i32 %23, i32* %8
%24 = load i32*, i32** %5
%25 = call i64 @decode(i32* %24, i32* %8)
store i64 %25, i64* %9
%26 = load i32*, i32** %5
%27 = call i64 @decode(i32* %26, i32* %8)
store i64 %27, i64* %9
%28 = load i64*, i64** %7
%29 = icmp ne i64* %28, null
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i64, i64* %9
%32 = load i64*, i64** %7
store i64 %31, i64* %32
br label %33
33: ; preds = %30, %21
store i32 0, i32* %4
br label %35
34: ; preds = %17
store i32 -1, i32* %4
br label %35
35: ; preds = %34, %33, %12
%36 = load i32, i32* %4
ret i32 %36
}

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