#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 gb18030_unicode_range_lookup(unsigned int, unsigned char *);
int gb18030_unicode_table_lookup(unsigned int, unsigned char *);
int gb18030_encode(unsigned int unicode, unsigned char out[4]) {
  int len;
  if (unicode < 0x80) {
    out[0] = unicode;
    len = 1;
  } else {
    len = gb18030_unicode_table_lookup(unicode, out);
    if (!len)
      len = gb18030_unicode_range_lookup(unicode, out);
  }
  return len;
}


x86 assembly (clang, -O0):
<gb18030_encode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x80,-0x4(%rbp)
jae    1153 <gb18030_encode+0x33>
mov    -0x4(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
movl   $0x1,-0x14(%rbp)
jmp    1180 <gb18030_encode+0x60>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <gb18030_unicode_table_lookup@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    117b <gb18030_encode+0x5b>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1040 <gb18030_unicode_range_lookup@plt>
mov    %eax,-0x14(%rbp)
jmp    1180 <gb18030_encode+0x60>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gb18030_encode>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
cmp    $0x7f,%edi
ja     1137 <gb18030_encode+0x17>
mov    %bpl,(%rbx)
mov    $0x1,%eax
jmp    1145 <gb18030_encode+0x25>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <gb18030_unicode_table_lookup@plt>
test   %eax,%eax
je     114c <gb18030_encode+0x2c>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %ebp,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <gb18030_unicode_range_lookup@plt>

LLVM IR (-O0):
define i32 @gb18030_encode(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%6 = load i32, i32* %3
%7 = icmp ult i32 %6, 128
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* %3
%10 = trunc i32 %9 to i8
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 0
store i8 %10, i8* %12
store i32 1, i32* %5
br label %24
13: ; preds = %2
%14 = load i32, i32* %3
%15 = load i8*, i8** %4
%16 = call i32 @gb18030_unicode_table_lookup(i32 %14, i8* %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %13
%20 = load i32, i32* %3
%21 = load i8*, i8** %4
%22 = call i32 @gb18030_unicode_range_lookup(i32 %20, i8* %21)
store i32 %22, i32* %5
br label %23
23: ; preds = %19, %13
br label %24
24: ; preds = %23, %8
%25 = load i32, i32* %5
ret i32 %25
}

LLVM IR (-O2):
define i32 @gb18030_encode(i32 %0, i8* %1) {
%3 = icmp ult i32 %0, 128
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = trunc i32 %0 to i8
store i8 %5, i8* %1
br label %11
6: ; preds = %2
%7 = tail call i32 @gb18030_unicode_table_lookup(i32 %0, i8* %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @gb18030_unicode_range_lookup(i32 %0, i8* %1)
br label %11
11: ; preds = %6, %9, %4
%12 = phi i32 [ 1, %4 ], [ %7, %6 ], [ %10, %9 ]
ret i32 %12
}