#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 */
char *calloc(int, size_t);
int code_conv(char *, char *, char *, size_t, char *, size_t);
int /*<<< orphan*/ free(char *);
size_t strlen(char *);
char *code_conv_u2g(char *inbuf) {
  size_t inlen;
  size_t outlen;
  char *outbuf;
  int ret;
  inlen = strlen(inbuf);
  if (inlen == 0)
    return NULL;
  outlen = inlen * 2 + 1;
  outbuf = calloc(1, outlen);
  if (outbuf == NULL)
    return NULL;
  ret = code_conv("utf-8", "gb2312", inbuf, inlen, outbuf, outlen);
  if (ret == -1) {
    free(outbuf);
    return NULL;
  }
  return outbuf;
}


x86 assembly (clang, -O0):
<code_conv_u2g>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1171 <code_conv_u2g+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <code_conv_u2g+0xb9>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ab <code_conv_u2g+0x6b>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <code_conv_u2g+0xb9>
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8
mov    -0x20(%rbp),%r9
lea    0xe3e(%rip),%rdi
lea    0xe3d(%rip),%rsi
call   1060 <code_conv@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11f1 <code_conv_u2g+0xb1>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11f9 <code_conv_u2g+0xb9>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<code_conv_u2g>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %rax,%rax
je     11a1 <code_conv_u2g+0x61>
mov    %rax,%rbx
lea    (%rax,%rax,1),%r12
add    $0x1,%r12
mov    $0x1,%edi
mov    %r12,%rsi
call   1050 <calloc@plt>
test   %rax,%rax
je     11a1 <code_conv_u2g+0x61>
mov    %rax,%r15
lea    0xe84(%rip),%rdi
lea    0xe83(%rip),%rsi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %rax,%r8
mov    %r12,%r9
call   1060 <code_conv@plt>
cmp    $0xffffffff,%eax
jne    11a4 <code_conv_u2g+0x64>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"utf-8\00"
@.str.1 = [7 x i8] c"gb2312\00"
define i8* @code_conv_u2g(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %4
%10 = load i64, i64* %4
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i8* null, i8** %2
br label %35
13: ; preds = %1
%14 = load i64, i64* %4
%15 = mul i64 %14, 2
%16 = add i64 %15, 1
store i64 %16, i64* %5
%17 = load i64, i64* %5
%18 = call i8* @calloc(i32 1, i64 %17)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %22
21: ; preds = %13
store i8* null, i8** %2
br label %35
22: ; preds = %13
%23 = load i8*, i8** %3
%24 = load i64, i64* %4
%25 = load i8*, i8** %6
%26 = load i64, i64* %5
%27 = call i32 @code_conv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %23, i64 %24, i8* %25, i64 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = load i8*, i8** %6
%32 = call i32 @free(i8* %31)
store i8* null, i8** %2
br label %35
33: ; preds = %22
%34 = load i8*, i8** %6
store i8* %34, i8** %2
br label %35
35: ; preds = %33, %30, %21, %12
%36 = load i8*, i8** %2
ret i8* %36
}

LLVM IR (-O2):
@.str = [6 x i8] c"utf-8\00"
@.str.1 = [7 x i8] c"gb2312\00"
define i8* @code_conv_u2g(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %14, label %4
4: ; preds = %1
%5 = shl i64 %2, 1
%6 = or i64 %5, 1
%7 = tail call i8* @calloc(i32 1, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = tail call i32 @code_conv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 %2, i8* %7, i64 %6)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @free(i8* %7)
br label %14
14: ; preds = %9, %4, %1, %12
%15 = phi i8* [ null, %12 ], [ null, %1 ], [ null, %4 ], [ %7, %9 ]
ret i8* %15
}