#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 */
typedef scalar_t__ iconv_t;

/* Variables and functions */
int /*<<< orphan*/ av_freep(char **);
char *av_malloc(size_t);
int /*<<< orphan*/ http_log(char *, ...);
int iconv(scalar_t__, char **, size_t *, char **, size_t *);
scalar_t__ iconv_open(char *, char *);
scalar_t__ is_utf8(unsigned char *);
size_t strlen(unsigned char *);
int /*<<< orphan*/ strncpy(unsigned char *, char *, int);
int url_local(unsigned char *utf8, int len) {
  if (!utf8 || len < 1) {
    http_log("bad arg len %d\n", len);
    return 0;
  }
  if (is_utf8(utf8)) {
    return 0;
  }
  static iconv_t ic_g2u = (iconv_t)-1;
  iconv_t ic;
  size_t inlen0, inlen, outlen0, outlen;
  char *outbuf, *pin, *pout;
  int ret;
  if ((iconv_t)-1 == ic_g2u) {
    ic_g2u = iconv_open("utf-8", "gbk");
  }
  ic = ic_g2u;
  inlen0 = inlen = strlen(utf8);
  outlen0 = outlen = inlen * 3;
  pin = (char *)utf8;
  pout = outbuf = av_malloc(outlen);
  if (!pout) {
    http_log("malloc for local fail\n");
    return 0;
  }
  ret = iconv(ic, &pin, &inlen, &pout, &outlen);
  if (!ret && !inlen) {
    strncpy(utf8, outbuf, len - 1);
    utf8[len - 1] = 0;
  } else {
    http_log("conv fail ret %d left in %d\n", ret, (int)inlen);
  }
  av_freep(&outbuf);
  return 1;
}


x86 assembly (clang, -O0):
<url_local>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a4 <url_local+0x24>
cmpl   $0x1,-0x14(%rbp)
jge    11c1 <url_local+0x41>
mov    -0x14(%rbp),%esi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1070 <http_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f6 <url_local+0x176>
mov    -0x10(%rbp),%rdi
call   1040 <is_utf8@plt>
cmp    $0x0,%rax
je     11e0 <url_local+0x60>
movl   $0x0,-0x4(%rbp)
jmp    12f6 <url_local+0x176>
mov    $0xffffffffffffffff,%rax
cmp    0x2e72(%rip),%rax
jne    120e <url_local+0x8e>
lea    0xe15(%rip),%rdi
lea    0xe14(%rip),%rsi
call   10a0 <iconv_open@plt>
mov    %rax,0x2e52(%rip)
mov    0x2e4b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    %rax,-0x28(%rbp)
imul   $0x3,-0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
call   1030 <av_malloc@plt>
mov    %rax,-0x48(%rbp)
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1275 <url_local+0xf5>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1070 <http_log@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f6 <url_local+0x176>
mov    -0x20(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x58(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1060 <iconv@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jne    12cf <url_local+0x14f>
cmpq   $0x0,-0x30(%rbp)
jne    12cf <url_local+0x14f>
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1050 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12e6 <url_local+0x166>
mov    -0x5c(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
lea    0xd52(%rip),%rdi
mov    $0x0,%al
call   1070 <http_log@plt>
lea    -0x48(%rbp),%rdi
call   1090 <av_freep@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<url_local>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %esi,%r14d
test   %rdi,%rdi
je     1253 <url_local+0xd3>
test   %r14d,%r14d
jle    1253 <url_local+0xd3>
mov    %rdi,%rbx
call   1040 <is_utf8@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    1266 <url_local+0xe6>
mov    0x2ea5(%rip),%r15
cmp    $0xffffffffffffffff,%r15
jne    11de <url_local+0x5e>
lea    0xe48(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10a0 <iconv_open@plt>
mov    %rax,%r15
mov    %rax,0x2e82(%rip)
mov    %rbx,%rdi
call   1080 <strlen@plt>
mov    %rax,0x8(%rsp)
lea    (%rax,%rax,2),%rdi
mov    %rdi,0x28(%rsp)
mov    %rbx,0x18(%rsp)
call   1030 <av_malloc@plt>
mov    %rax,0x20(%rsp)
mov    %rax,0x10(%rsp)
test   %rax,%rax
je     1275 <url_local+0xf5>
mov    %rax,%r12
lea    0x18(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0x28(%rsp),%r8
mov    %r15,%rdi
call   1060 <iconv@plt>
mov    0x8(%rsp),%rdx
test   %eax,%eax
jne    1287 <url_local+0x107>
test   %rdx,%rdx
jne    1287 <url_local+0x107>
add    $0xffffffff,%r14d
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14d,%edx
call   1050 <strncpy@plt>
movb   $0x0,(%rbx,%r14,1)
jmp    1297 <url_local+0x117>
lea    0xda6(%rip),%rdi
xor    %ebp,%ebp
mov    %r14d,%esi
xor    %eax,%eax
call   1070 <http_log@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9e(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <http_log@plt>
jmp    1266 <url_local+0xe6>
lea    0xda3(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <http_log@plt>
lea    0x20(%rsp),%rdi
call   1090 <av_freep@plt>
mov    $0x1,%ebp
jmp    1266 <url_local+0xe6>

LLVM IR (-O0):
@.str = [16 x i8] c"bad arg len %d\0A\00"
@url_local.ic_g2u = internal global i64 -1
@.str.1 = [6 x i8] c"utf-8\00"
@.str.2 = [4 x i8] c"gbk\00"
@.str.3 = [23 x i8] c"malloc for local fail\0A\00"
@.str.4 = [29 x i8] c"conv fail ret %d left in %d\0A\00"
define i32 @url_local(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%15 = load i8*, i8** %4
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %17, %2
%21 = load i32, i32* %5
%22 = call i32 (i8*, ...) @http_log(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %21)
store i32 0, i32* %3
br label %72
23: ; preds = %17
%24 = load i8*, i8** %4
%25 = call i64 @is_utf8(i8* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 0, i32* %3
br label %72
28: ; preds = %23
%29 = load i64, i64* @url_local.ic_g2u
%30 = icmp eq i64 -1, %29
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i64 %32, i64* @url_local.ic_g2u
br label %33
33: ; preds = %31, %28
%34 = load i64, i64* @url_local.ic_g2u
store i64 %34, i64* %6
%35 = load i8*, i8** %4
%36 = call i64 @strlen(i8* %35)
store i64 %36, i64* %8
store i64 %36, i64* %7
%37 = load i64, i64* %8
%38 = mul i64 %37, 3
store i64 %38, i64* %10
store i64 %38, i64* %9
%39 = load i8*, i8** %4
store i8* %39, i8** %12
%40 = load i64, i64* %10
%41 = call i8* @av_malloc(i64 %40)
store i8* %41, i8** %11
store i8* %41, i8** %13
%42 = load i8*, i8** %13
%43 = icmp ne i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %33
%45 = call i32 (i8*, ...) @http_log(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %72
46: ; preds = %33
%47 = load i64, i64* %6
%48 = call i32 @iconv(i64 %47, i8** %12, i64* %8, i8** %13, i64* %10)
store i32 %48, i32* %14
%49 = load i32, i32* %14
%50 = icmp ne i32 %49, 0
br i1 %50, label %65, label %51
51: ; preds = %46
%52 = load i64, i64* %8
%53 = icmp ne i64 %52, 0
br i1 %53, label %65, label %54
54: ; preds = %51
%55 = load i8*, i8** %4
%56 = load i8*, i8** %11
%57 = load i32, i32* %5
%58 = sub nsw i32 %57, 1
%59 = call i32 @strncpy(i8* %55, i8* %56, i32 %58)
%60 = load i8*, i8** %4
%61 = load i32, i32* %5
%62 = sub nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 0, i8* %64
br label %70
65: ; preds = %51, %46
%66 = load i32, i32* %14
%67 = load i64, i64* %8
%68 = trunc i64 %67 to i32
%69 = call i32 (i8*, ...) @http_log(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %66, i32 %68)
br label %70
70: ; preds = %65, %54
%71 = call i32 @av_freep(i8** %11)
store i32 1, i32* %3
br label %72
72: ; preds = %70, %44, %27, %20
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [16 x i8] c"bad arg len %d\0A\00"
@url_local.ic_g2u = internal unnamed_addr global i64 -1
@.str.1 = [6 x i8] c"utf-8\00"
@.str.2 = [4 x i8] c"gbk\00"
@.str.3 = [23 x i8] c"malloc for local fail\0A\00"
@.str.4 = [29 x i8] c"conv fail ret %d left in %d\0A\00"
define i32 @url_local(i8* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = icmp eq i8* %0, null
%9 = icmp slt i32 %1, 1
%10 = or i1 %8, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 (i8*, ...) @http_log(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %1)
br label %52
13: ; preds = %2
%14 = tail call i64 @is_utf8(i8* %0)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %52
16: ; preds = %13
%17 = bitcast i64* %3 to i8*
%18 = bitcast i64* %4 to i8*
%19 = bitcast i8** %5 to i8*
%20 = bitcast i8** %6 to i8*
%21 = bitcast i8** %7 to i8*
%22 = load i64, i64* @url_local.ic_g2u
%23 = icmp eq i64 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = tail call i64 @iconv_open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i64 %25, i64* @url_local.ic_g2u
br label %26
26: ; preds = %24, %16
%27 = phi i64 [ %25, %24 ], [ %22, %16 ]
%28 = tail call i64 @strlen(i8* dereferenceable(1) %0)
store i64 %28, i64* %3
%29 = mul i64 %28, 3
store i64 %29, i64* %4
store i8* %0, i8** %6
%30 = tail call i8* @av_malloc(i64 %29)
store i8* %30, i8** %5
store i8* %30, i8** %7
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = tail call i32 (i8*, ...) @http_log(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %50
34: ; preds = %26
%35 = call i32 @iconv(i64 %27, i8** %6, i64* %3, i8** %7, i64* %4)
%36 = icmp ne i32 %35, 0
%37 = load i64, i64* %3
%38 = icmp ne i64 %37, 0
%39 = select i1 %36, i1 true, i1 %38
br i1 %39, label %45, label %40
40: ; preds = %34
%41 = add nsw i32 %1, -1
%42 = call i32 @strncpy(i8* %0, i8* %30, i32 %41)
%43 = zext i32 %41 to i64
%44 = getelementptr inbounds i8, i8* %0, i64 %43
store i8 0, i8* %44
br label %48
45: ; preds = %34
%46 = trunc i64 %37 to i32
%47 = call i32 (i8*, ...) @http_log(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %35, i32 %46)
br label %48
48: ; preds = %45, %40
%49 = call i32 @av_freep(i8** %5)
br label %50
50: ; preds = %48, %32
%51 = phi i32 [ 1, %48 ], [ 0, %32 ]
br label %52
52: ; preds = %13, %50, %11
%53 = phi i32 [ 0, %11 ], [ %51, %50 ], [ 0, %13 ]
ret i32 %53
}