#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__ append_unicode_char(int **, size_t *, size_t *, int);
int unicode_decode(uint8_t *srcbuf, size_t srcbuflen, int mode,
                   uint8_t **destbuf, size_t *destbuflen, size_t *destbufpos) {
  size_t i;
  uint32_t msb = mode << 8;
  for (i = 0; i < srcbuflen; i++) {
    if (append_unicode_char(destbuf, destbuflen, destbufpos, msb + srcbuf[i]))
      return -1;
  }
  return *destbufpos;
}


x86 assembly (clang, -O0):
<unicode_decode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x8,%eax
mov    %eax,-0x44(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    119a <unicode_decode+0x8a>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x44(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%r8
add    (%rax,%r8,4),%ecx
call   1030 <append_unicode_char@plt>
cmp    $0x0,%rax
je     1184 <unicode_decode+0x74>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a4 <unicode_decode+0x94>
jmp    1189 <unicode_decode+0x79>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1140 <unicode_decode+0x30>
mov    -0x38(%rbp),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicode_decode>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%r12
mov    %r8,(%rsp)
test   %rsi,%rsi
je     1164 <unicode_decode+0x54>
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r13
shl    $0x8,%ebp
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13,%r14,4),%ecx
add    %ebp,%ecx
mov    %r15,%rdi
mov    (%rsp),%rsi
mov    %r12,%rdx
call   1030 <append_unicode_char@plt>
test   %rax,%rax
jne    116a <unicode_decode+0x5a>
add    $0x1,%r14
cmp    %r14,%rbx
jne    1140 <unicode_decode+0x30>
mov    (%r12),%eax
jmp    116f <unicode_decode+0x5f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @unicode_decode(i32* %0, i64 %1, i32 %2, i32** %3, i64* %4, i64* %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32**
%12 = alloca i64*
%13 = alloca i64*
%14 = alloca i64
%15 = alloca i32
store i32* %0, i32** %8
store i64 %1, i64* %9
store i32 %2, i32* %10
store i32** %3, i32*** %11
store i64* %4, i64** %12
store i64* %5, i64** %13
%16 = load i32, i32* %10
%17 = shl i32 %16, 8
store i32 %17, i32* %15
store i64 0, i64* %14
br label %18
18: ; preds = %36, %6
%19 = load i64, i64* %14
%20 = load i64, i64* %9
%21 = icmp ult i64 %19, %20
br i1 %21, label %22, label %39
22: ; preds = %18
%23 = load i32**, i32*** %11
%24 = load i64*, i64** %12
%25 = load i64*, i64** %13
%26 = load i32, i32* %15
%27 = load i32*, i32** %8
%28 = load i64, i64* %14
%29 = getelementptr inbounds i32, i32* %27, i64 %28
%30 = load i32, i32* %29
%31 = add nsw i32 %26, %30
%32 = call i64 @append_unicode_char(i32** %23, i64* %24, i64* %25, i32 %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %22
store i32 -1, i32* %7
br label %43
35: ; preds = %22
br label %36
36: ; preds = %35
%37 = load i64, i64* %14
%38 = add i64 %37, 1
store i64 %38, i64* %14
br label %18
39: ; preds = %18
%40 = load i64*, i64** %13
%41 = load i64, i64* %40
%42 = trunc i64 %41 to i32
store i32 %42, i32* %7
br label %43
43: ; preds = %39, %34
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
define i32 @unicode_decode(i32* nocapture %0, i64 %1, i32 %2, i32** %3, i64* %4, i64* %5) {
%7 = shl i32 %2, 8
%8 = icmp eq i64 %1, 0
br i1 %8, label %19, label %11
9: ; preds = %11
%10 = icmp eq i64 %18, %1
br i1 %10, label %19, label %11
11: ; preds = %6, %9
%12 = phi i64 [ %18, %9 ], [ 0, %6 ]
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* %13
%15 = add nsw i32 %14, %7
%16 = tail call i64 @append_unicode_char(i32** %3, i64* %4, i64* %5, i32 %15)
%17 = icmp eq i64 %16, 0
%18 = add nuw i64 %12, 1
br i1 %17, label %9, label %22
19: ; preds = %9, %6
%20 = load i64, i64* %5
%21 = trunc i64 %20 to i32
br label %22
22: ; preds = %11, %19
%23 = phi i32 [ %21, %19 ], [ -1, %11 ]
ret i32 %23
}