#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 next_utf8(char const **, size_t *);
size_t strlen(char const *);
void charset_conv_utf8_to_ucs_2be(char *ucs, const char *utf8) {
  const char *utf8cur = utf8;
  size_t utf8len = strlen(utf8);
  unsigned char *ucscur = ucs;
  while (utf8len) {
    unsigned int nextpt = next_utf8(&utf8cur, &utf8len);
    if (nextpt == 0) {
      break;
    } else if (nextpt < 0x10000) {
      ucscur[0] = (nextpt >> 8) & 0xff;
      ucscur[1] = nextpt & 0xff;
      ucscur += 2;
    }
  }
  *ucscur = '\0';
}


x86 assembly (clang, -O0):
<charset_conv_utf8_to_ucs_2be>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11be <charset_conv_utf8_to_ucs_2be+0x9e>
lea    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <next_utf8@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1177 <charset_conv_utf8_to_ucs_2be+0x57>
jmp    11be <charset_conv_utf8_to_ucs_2be+0x9e>
cmpl   $0x10000,-0x2c(%rbp)
jae    11b4 <charset_conv_utf8_to_ucs_2be+0x94>
mov    -0x2c(%rbp),%eax
shr    $0x8,%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x2c(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
jmp    11b9 <charset_conv_utf8_to_ucs_2be+0x99>
jmp    114d <charset_conv_utf8_to_ucs_2be+0x2d>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<charset_conv_utf8_to_ucs_2be>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    %rsi,0x8(%rsp)
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     117a <charset_conv_utf8_to_ucs_2be+0x5a>
lea    0x8(%rsp),%r14
mov    %rsp,%r15
jmp    1157 <charset_conv_utf8_to_ucs_2be+0x37>
nopl   0x0(%rax)
cmpq   $0x0,(%rsp)
je     117a <charset_conv_utf8_to_ucs_2be+0x5a>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <next_utf8@plt>
test   %eax,%eax
je     117a <charset_conv_utf8_to_ucs_2be+0x5a>
cmp    $0xffff,%eax
ja     1150 <charset_conv_utf8_to_ucs_2be+0x30>
rol    $0x8,%ax
mov    %ax,(%rbx)
add    $0x2,%rbx
jmp    1150 <charset_conv_utf8_to_ucs_2be+0x30>
movb   $0x0,(%rbx)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define void @charset_conv_utf8_to_ucs_2be(i8* nocapture writeonly %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
store i8* %1, i8** %3
%6 = bitcast i64* %4 to i8*
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
store i64 %7, i64* %4
%8 = icmp eq i64 %7, 0
br i1 %8, label %25, label %13
9: ; preds = %17, %19
%10 = phi i8* [ %14, %17 ], [ %24, %19 ]
%11 = load i64, i64* %4
%12 = icmp eq i64 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %2, %9
%14 = phi i8* [ %10, %9 ], [ %0, %2 ]
%15 = call i32 @next_utf8(i8** %3, i64* %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %13
%18 = icmp ult i32 %15, 65536
br i1 %18, label %19, label %9
19: ; preds = %17
%20 = lshr i32 %15, 8
%21 = trunc i32 %20 to i8
store i8 %21, i8* %14
%22 = trunc i32 %15 to i8
%23 = getelementptr inbounds i8, i8* %14, i64 1
store i8 %22, i8* %23
%24 = getelementptr inbounds i8, i8* %14, i64 2
br label %9
25: ; preds = %9, %13, %2
%26 = phi i8* [ %0, %2 ], [ %14, %13 ], [ %10, %9 ]
store i8 0, i8* %26
ret void
}