#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 */
unsigned int unicoder_utf16_decode(unsigned int *, unsigned char *, int);
unsigned int unicoder_utf32_decode(unsigned int *, unsigned char *, int);
unsigned int unicoder_utf8_decode(unsigned int *, unsigned char *);
int unicoder_readCodePoint(unsigned char *p, unsigned int *result,
                           unsigned int encoding) {
  unsigned char temp[4];
  int i;
  unsigned int originalPosition, currentChar, bytesRead;
  if (p == NULL)
    return -1;
  if (result == NULL)
    return -1;
  switch (encoding) {
  case 1:
    (*result) = (unsigned int)(*p);
    bytesRead = 1;
    break;
  case 2:
    bytesRead = unicoder_utf8_decode(result, p);
    break;
  case 3:
    bytesRead = unicoder_utf16_decode(result, p, 2);
    break;
  case 4:
    bytesRead = unicoder_utf16_decode(result, p, 1);
    break;
  case 5:
    bytesRead = unicoder_utf32_decode(result, p, 2);
    break;
  case 6:
    bytesRead = unicoder_utf32_decode(result, p, 1);
    break;
  default:
    return -2;
    break;
  };
  return bytesRead;
}


x86 assembly (clang, -O0):
<unicoder_readCodePoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115a <unicoder_readCodePoint+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <unicoder_readCodePoint+0x112>
cmpq   $0x0,-0x18(%rbp)
jne    1171 <unicoder_readCodePoint+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <unicoder_readCodePoint+0x112>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x5,%eax
ja     1230 <unicoder_readCodePoint+0x100>
mov    -0x38(%rbp),%rax
lea    0xe6f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x10(%rbp),%rax
movzbl (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x30(%rbp)
jmp    123c <unicoder_readCodePoint+0x10c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <unicoder_utf8_decode@plt>
mov    %eax,-0x30(%rbp)
jmp    123c <unicoder_readCodePoint+0x10c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1030 <unicoder_utf16_decode@plt>
mov    %eax,-0x30(%rbp)
jmp    123c <unicoder_readCodePoint+0x10c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <unicoder_utf16_decode@plt>
mov    %eax,-0x30(%rbp)
jmp    123c <unicoder_readCodePoint+0x10c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x2,%edx
call   1050 <unicoder_utf32_decode@plt>
mov    %eax,-0x30(%rbp)
jmp    123c <unicoder_readCodePoint+0x10c>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1050 <unicoder_utf32_decode@plt>
mov    %eax,-0x30(%rbp)
jmp    123c <unicoder_readCodePoint+0x10c>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1242 <unicoder_readCodePoint+0x112>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicoder_readCodePoint>:
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1164 <unicoder_readCodePoint+0x34>
test   %rsi,%rsi
je     1164 <unicoder_readCodePoint+0x34>
add    $0xffffffff,%edx
cmp    $0x5,%edx
ja     1165 <unicoder_readCodePoint+0x35>
mov    %rdi,%rcx
lea    0xeaf(%rip),%rax
movslq (%rax,%rdx,4),%rdx
add    %rax,%rdx
jmp    *%rdx
movzbl (%rcx),%eax
mov    %eax,(%rsi)
mov    $0x1,%eax
ret
mov    $0xfffffffe,%eax
ret
mov    %rsi,%rdi
mov    %rcx,%rsi
jmp    1040 <unicoder_utf8_decode@plt>
mov    %rsi,%rdi
mov    %rcx,%rsi
mov    $0x2,%edx
jmp    1030 <unicoder_utf16_decode@plt>
mov    %rsi,%rdi
mov    %rcx,%rsi
mov    $0x1,%edx
jmp    1030 <unicoder_utf16_decode@plt>
mov    %rsi,%rdi
mov    %rcx,%rsi
mov    $0x2,%edx
jmp    1050 <unicoder_utf32_decode@plt>
mov    %rsi,%rdi
mov    %rcx,%rsi
mov    $0x1,%edx
jmp    1050 <unicoder_utf32_decode@plt>

LLVM IR (-O0):
define i32 @unicoder_readCodePoint(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [4 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %16
15: ; preds = %3
store i32 -1, i32* %4
br label %50
16: ; preds = %3
%17 = load i32*, i32** %6
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 -1, i32* %4
br label %50
20: ; preds = %16
%21 = load i32, i32* %7
switch i32 %21, label %47 [
i32 1, label %22
i32 2, label %27
i32 3, label %31
i32 4, label %35
i32 5, label %39
i32 6, label %43
]
22: ; preds = %20
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = load i32*, i32** %6
store i32 %25, i32* %26
store i32 1, i32* %12
br label %48
27: ; preds = %20
%28 = load i32*, i32** %6
%29 = load i8*, i8** %5
%30 = call i32 @unicoder_utf8_decode(i32* %28, i8* %29)
store i32 %30, i32* %12
br label %48
31: ; preds = %20
%32 = load i32*, i32** %6
%33 = load i8*, i8** %5
%34 = call i32 @unicoder_utf16_decode(i32* %32, i8* %33, i32 2)
store i32 %34, i32* %12
br label %48
35: ; preds = %20
%36 = load i32*, i32** %6
%37 = load i8*, i8** %5
%38 = call i32 @unicoder_utf16_decode(i32* %36, i8* %37, i32 1)
store i32 %38, i32* %12
br label %48
39: ; preds = %20
%40 = load i32*, i32** %6
%41 = load i8*, i8** %5
%42 = call i32 @unicoder_utf32_decode(i32* %40, i8* %41, i32 2)
store i32 %42, i32* %12
br label %48
43: ; preds = %20
%44 = load i32*, i32** %6
%45 = load i8*, i8** %5
%46 = call i32 @unicoder_utf32_decode(i32* %44, i8* %45, i32 1)
store i32 %46, i32* %12
br label %48
47: ; preds = %20
store i32 -2, i32* %4
br label %50
48: ; preds = %43, %39, %35, %31, %27, %22
%49 = load i32, i32* %12
store i32 %49, i32* %4
br label %50
50: ; preds = %48, %47, %19, %15
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
define i32 @unicoder_readCodePoint(i8* %0, i32* %1, i32 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i32* %1, null
%6 = or i1 %4, %5
br i1 %6, label %21, label %7
7: ; preds = %3
switch i32 %2, label %21 [
i32 1, label %8
i32 2, label %11
i32 3, label %13
i32 4, label %15
i32 5, label %17
i32 6, label %19
]
8: ; preds = %7
%9 = load i8, i8* %0
%10 = zext i8 %9 to i32
store i32 %10, i32* %1
br label %21
11: ; preds = %7
%12 = tail call i32 @unicoder_utf8_decode(i32* %1, i8* %0)
br label %21
13: ; preds = %7
%14 = tail call i32 @unicoder_utf16_decode(i32* %1, i8* %0, i32 2)
br label %21
15: ; preds = %7
%16 = tail call i32 @unicoder_utf16_decode(i32* %1, i8* %0, i32 1)
br label %21
17: ; preds = %7
%18 = tail call i32 @unicoder_utf32_decode(i32* %1, i8* %0, i32 2)
br label %21
19: ; preds = %7
%20 = tail call i32 @unicoder_utf32_decode(i32* %1, i8* %0, i32 1)
br label %21
21: ; preds = %8, %11, %13, %15, %17, %19, %7, %3
%22 = phi i32 [ -1, %3 ], [ -2, %7 ], [ %20, %19 ], [ %18, %17 ], [ %16, %15 ], [ %14, %13 ], [ %12, %11 ], [ 1, %8 ]
ret i32 %22
}