#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(char *);
char *iconv_impl(char *, int, char *, char *, int *, int *);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int main(int argc, const char *argv[]) {
  if (argc < 2) {
    return -1;
  }
  FILE *fp = fopen(argv[1], "r");
  if (!fp) {
    return -1;
  }
  char *s = malloc(65535);
  if (!s) {
    return -1;
  }
  memset(s, 0, 65535);
  int outputsiz = 0, misssiz = 0;
  while (fgets(s, 65535, fp)) {
    outputsiz = 0;
    misssiz = 0;
    char *output = iconv_impl(s, -1, "GB18030", "UTF-8", &outputsiz, &misssiz);
    printf("%s", output);
    if (output) {
      free(output);
    }
  }
  free(s);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ac <main+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x132>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe45(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11db <main+0x5b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x132>
mov    $0xffff,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1200 <main+0x80>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b2 <main+0x132>
mov    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xffff,%edx
call   1060 <memset@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xffff,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1299 <main+0x119>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xffffffff,%esi
lea    0xdaa(%rip),%rdx
lea    0xdab(%rip),%rcx
lea    -0x24(%rbp),%r8
lea    -0x28(%rbp),%r9
call   1090 <iconv_impl@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xd95(%rip),%rdi
call   1050 <printf@plt>
cmpq   $0x0,-0x30(%rbp)
je     1294 <main+0x114>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
jmp    121e <main+0x9e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
cmp    $0x2,%edi
jl     1285 <main+0x105>
mov    0x8(%rsi),%rdi
lea    0xe5b(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1285 <main+0x105>
mov    %rax,%rbx
mov    $0xffff,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1285 <main+0x105>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xffff,%edx
call   1060 <memset@plt>
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
mov    %rbp,%rdi
mov    $0xffff,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1272 <main+0xf2>
lea    0x4(%rsp),%r13
mov    %rsp,%r14
lea    0xe02(%rip),%r15
jmp    1225 <main+0xa5>
mov    %rbp,%rdi
mov    $0xffff,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     1272 <main+0xf2>
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
mov    %rbp,%rdi
mov    $0xffffffff,%esi
lea    0xdbf(%rip),%rdx
lea    0xdc0(%rip),%rcx
mov    %r13,%r8
mov    %r14,%r9
call   1090 <iconv_impl@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
test   %r12,%r12
je     1210 <main+0x90>
mov    %r12,%rdi
call   1030 <free@plt>
jmp    1210 <main+0x90>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [8 x i8] c"GB18030\00"
@.str.2 = [6 x i8] c"UTF-8\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %51
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %6
%19 = load i32*, i32** %6
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %14
store i32 -1, i32* %3
br label %51
22: ; preds = %14
%23 = call i8* @malloc(i32 65535)
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = icmp ne i8* %24, null
br i1 %25, label %27, label %26
26: ; preds = %22
store i32 -1, i32* %3
br label %51
27: ; preds = %22
%28 = load i8*, i8** %7
%29 = call i32 @memset(i8* %28, i32 0, i32 65535)
store i32 0, i32* %8
store i32 0, i32* %9
br label %30
30: ; preds = %45, %27
%31 = load i8*, i8** %7
%32 = load i32*, i32** %6
%33 = call i64 @fgets(i8* %31, i32 65535, i32* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %46
35: ; preds = %30
store i32 0, i32* %8
store i32 0, i32* %9
%36 = load i8*, i8** %7
%37 = call i8* @iconv_impl(i8* %36, i32 -1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %8, i32* %9)
store i8* %37, i8** %10
%38 = load i8*, i8** %10
%39 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %38)
%40 = load i8*, i8** %10
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %45
42: ; preds = %35
%43 = load i8*, i8** %10
%44 = call i32 @free(i8* %43)
br label %45
45: ; preds = %42, %35
br label %30
46: ; preds = %30
%47 = load i8*, i8** %7
%48 = call i32 @free(i8* %47)
%49 = load i32*, i32** %6
%50 = call i32 @fclose(i32* %49)
store i32 0, i32* %3
br label %51
51: ; preds = %46, %26, %21, %13
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [8 x i8] c"GB18030\00"
@.str.2 = [6 x i8] c"UTF-8\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = icmp slt i32 %0, 2
br i1 %5, label %32, label %6
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %32, label %11
11: ; preds = %6
%12 = tail call dereferenceable_or_null(65535) i8* @malloc(i32 65535)
%13 = icmp eq i8* %12, null
br i1 %13, label %32, label %14
14: ; preds = %11
%15 = tail call i32 @memset(i8* %12, i32 0, i32 65535)
%16 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%17 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%18 = call i64 @fgets(i8* %12, i32 65535, i32* %9)
%19 = icmp eq i64 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %14, %26
store i32 0, i32* %3
store i32 0, i32* %4
%21 = call i8* @iconv_impl(i8* %12, i32 -1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %4)
%22 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = icmp eq i8* %21, null
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = call i32 @free(i8* %21)
br label %26
26: ; preds = %24, %20
%27 = call i64 @fgets(i8* %12, i32 65535, i32* %9)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %20
29: ; preds = %26, %14
%30 = call i32 @free(i8* %12)
%31 = call i32 @fclose(i32* %9)
br label %32
32: ; preds = %6, %11, %29, %2
%33 = phi i32 [ -1, %2 ], [ -1, %6 ], [ 0, %29 ], [ -1, %11 ]
ret i32 %33
}