#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 */
int unicoder_utf16_encode(unsigned char *, unsigned int, int);
int unicoder_utf32_encode(unsigned char *, unsigned int, int);
int unicoder_utf8_encode(unsigned char *, unsigned int);
int unicoder_writeCodePoint(unsigned char *p, unsigned int x,
                            unsigned int encoding) {
  if (p == NULL)
    return -1;
  switch (encoding) {
  case 1:
    if (x > 0x0000007f)
      return -256;
    (*p) = (unsigned char)x;
    return 1;
    break;
  case 2:
    return unicoder_utf8_encode(p, x);
    break;
  case 3:
    return unicoder_utf16_encode(p, x, 2);
    break;
  case 4:
    return unicoder_utf16_encode(p, x, 1);
    break;
  case 5:
    return unicoder_utf32_encode(p, x, 2);
    break;
  case 6:
    return unicoder_utf32_encode(p, x, 1);
    break;
  default:
    return -2;
    break;
  };
  return -1024;
}


x86 assembly (clang, -O0):
<unicoder_writeCodePoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1159 <unicoder_writeCodePoint+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    122e <unicoder_writeCodePoint+0xfe>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x5,%eax
ja     1227 <unicoder_writeCodePoint+0xf7>
mov    -0x20(%rbp),%rax
lea    0xe87(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmpl   $0x7f,-0x14(%rbp)
jbe    1198 <unicoder_writeCodePoint+0x68>
movl   $0xffffff00,-0x4(%rbp)
jmp    122e <unicoder_writeCodePoint+0xfe>
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    122e <unicoder_writeCodePoint+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <unicoder_utf8_encode@plt>
mov    %eax,-0x4(%rbp)
jmp    122e <unicoder_writeCodePoint+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x2,%edx
call   1040 <unicoder_utf16_encode@plt>
mov    %eax,-0x4(%rbp)
jmp    122e <unicoder_writeCodePoint+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x1,%edx
call   1040 <unicoder_utf16_encode@plt>
mov    %eax,-0x4(%rbp)
jmp    122e <unicoder_writeCodePoint+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x2,%edx
call   1030 <unicoder_utf32_encode@plt>
mov    %eax,-0x4(%rbp)
jmp    122e <unicoder_writeCodePoint+0xfe>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x1,%edx
call   1030 <unicoder_utf32_encode@plt>
mov    %eax,-0x4(%rbp)
jmp    122e <unicoder_writeCodePoint+0xfe>
movl   $0xfffffffe,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unicoder_writeCodePoint>:
test   %rdi,%rdi
je     1160 <unicoder_writeCodePoint+0x30>
add    $0xffffffff,%edx
cmp    $0x5,%edx
ja     1184 <unicoder_writeCodePoint+0x54>
lea    0xebc(%rip),%rax
movslq (%rax,%rdx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    $0xffffff00,%eax
cmp    $0x7f,%esi
ja     115f <unicoder_writeCodePoint+0x2f>
mov    %sil,(%rdi)
mov    $0x1,%eax
ret
mov    $0xffffffff,%eax
ret
mov    $0x2,%edx
jmp    1040 <unicoder_utf16_encode@plt>
mov    $0x1,%edx
jmp    1040 <unicoder_utf16_encode@plt>
mov    $0x2,%edx
jmp    1030 <unicoder_utf32_encode@plt>
mov    $0xfffffffe,%eax
ret
jmp    1050 <unicoder_utf8_encode@plt>
mov    $0x1,%edx
jmp    1030 <unicoder_utf32_encode@plt>

LLVM IR (-O0):
define i32 @unicoder_writeCodePoint(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %3
store i32 -1, i32* %4
br label %42
11: ; preds = %3
%12 = load i32, i32* %7
switch i32 %12, label %41 [
i32 1, label %13
i32 2, label %21
i32 3, label %25
i32 4, label %29
i32 5, label %33
i32 6, label %37
]
13: ; preds = %11
%14 = load i32, i32* %6
%15 = icmp ugt i32 %14, 127
br i1 %15, label %16, label %17
16: ; preds = %13
store i32 -256, i32* %4
br label %42
17: ; preds = %13
%18 = load i32, i32* %6
%19 = trunc i32 %18 to i8
%20 = load i8*, i8** %5
store i8 %19, i8* %20
store i32 1, i32* %4
br label %42
21: ; preds = %11
%22 = load i8*, i8** %5
%23 = load i32, i32* %6
%24 = call i32 @unicoder_utf8_encode(i8* %22, i32 %23)
store i32 %24, i32* %4
br label %42
25: ; preds = %11
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = call i32 @unicoder_utf16_encode(i8* %26, i32 %27, i32 2)
store i32 %28, i32* %4
br label %42
29: ; preds = %11
%30 = load i8*, i8** %5
%31 = load i32, i32* %6
%32 = call i32 @unicoder_utf16_encode(i8* %30, i32 %31, i32 1)
store i32 %32, i32* %4
br label %42
33: ; preds = %11
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = call i32 @unicoder_utf32_encode(i8* %34, i32 %35, i32 2)
store i32 %36, i32* %4
br label %42
37: ; preds = %11
%38 = load i8*, i8** %5
%39 = load i32, i32* %6
%40 = call i32 @unicoder_utf32_encode(i8* %38, i32 %39, i32 1)
store i32 %40, i32* %4
br label %42
41: ; preds = %11
store i32 -2, i32* %4
br label %42
42: ; preds = %41, %37, %33, %29, %25, %21, %17, %16, %10
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
define i32 @unicoder_writeCodePoint(i8* %0, i32 %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %20, label %5
5: ; preds = %3
switch i32 %2, label %20 [
i32 1, label %6
i32 2, label %10
i32 3, label %12
i32 4, label %14
i32 5, label %16
i32 6, label %18
]
6: ; preds = %5
%7 = icmp ugt i32 %1, 127
br i1 %7, label %20, label %8
8: ; preds = %6
%9 = trunc i32 %1 to i8
store i8 %9, i8* %0
br label %20
10: ; preds = %5
%11 = tail call i32 @unicoder_utf8_encode(i8* %0, i32 %1)
br label %20
12: ; preds = %5
%13 = tail call i32 @unicoder_utf16_encode(i8* %0, i32 %1, i32 2)
br label %20
14: ; preds = %5
%15 = tail call i32 @unicoder_utf16_encode(i8* %0, i32 %1, i32 1)
br label %20
16: ; preds = %5
%17 = tail call i32 @unicoder_utf32_encode(i8* %0, i32 %1, i32 2)
br label %20
18: ; preds = %5
%19 = tail call i32 @unicoder_utf32_encode(i8* %0, i32 %1, i32 1)
br label %20
20: ; preds = %5, %6, %3, %18, %16, %14, %12, %10, %8
%21 = phi i32 [ %19, %18 ], [ %17, %16 ], [ %15, %14 ], [ %13, %12 ], [ %11, %10 ], [ 1, %8 ], [ -1, %3 ], [ -256, %6 ], [ -2, %5 ]
ret i32 %21
}