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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
size_t iconv(scalar_t__, char **, size_t *, char **, size_t *);
int /*<<< orphan*/ iconv_close(scalar_t__);
scalar_t__ iconv_open(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
size_t strlen(char *);
int main(void) {
  FILE *fp = NULL;
  char buf[256] = {0};
  char *buf_ptr;
  size_t len;
  iconv_t iconv_dsc;
  char buf2[256] = {0};
  size_t len2;
  char *buf2_ptr;
  size_t ret_len;
  fp = fopen("test1.txt", "r");
  if (fp == NULL) {
    printf("fopen error!\n");
    return -1;
  }
  fgets(buf, 256, fp);
  len = strlen(buf);
  if (buf[len - 1] == '\n') {
    buf[len - 1] = '\0';
  }
  fclose(fp);
  iconv_dsc = iconv_open("Shift_JIS", "utf-8");
  if (iconv_dsc == (iconv_t)-1) {
    printf("iconv_open error!\n");
  }
  buf_ptr = buf;
  len = strlen(buf);
  len2 = sizeof(buf2);
  buf2_ptr = buf2;
  ret_len = iconv(iconv_dsc, &buf_ptr, &len, &buf2_ptr, &len2);
  if (ret_len == -1) {
    printf("iconv error!\n");
    perror("iconv error detail");
    return -3;
  }
  iconv_close(iconv_dsc);
  fp = fopen("test2.txt", "w");
  fputs(buf2, fp);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x250,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    -0x230(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1080 <memset@plt>
lea    0xe09(%rip),%rdi
lea    0xe0c(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    122a <main+0x7a>
lea    0xdf3(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139c <main+0x1ec>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1090 <fgets@plt>
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
sub    $0x1,%rax
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1281 <main+0xd1>
mov    -0x120(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x110(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xd89(%rip),%rdi
lea    0xd8c(%rip),%rsi
call   10d0 <iconv_open@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0xffffffffffffffff,-0x128(%rbp)
ff
jne    12be <main+0x10e>
lea    0xd71(%rip),%rdi
call   1060 <printf@plt>
lea    -0x110(%rbp),%rax
mov    %rax,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x120(%rbp)
movq   $0x100,-0x238(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x128(%rbp),%rdi
lea    -0x118(%rbp),%rsi
lea    -0x120(%rbp),%rdx
lea    -0x240(%rbp),%rcx
lea    -0x238(%rbp),%r8
call   1030 <iconv@plt>
mov    %rax,-0x248(%rbp)
cmpq   $0xffffffffffffffff,-0x248(%rbp)
ff
jne    1359 <main+0x1a9>
lea    0xd01(%rip),%rdi
call   1060 <printf@plt>
lea    0xd03(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    139c <main+0x1ec>
mov    -0x128(%rbp),%rdi
call   10a0 <iconv_close@plt>
lea    0xcf2(%rip),%rdi
lea    0xcf5(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x230(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x250,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x228,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x210(%rsp)
movaps %xmm0,0x200(%rsp)
movaps %xmm0,0x1f0(%rsp)
movaps %xmm0,0x1e0(%rsp)
movaps %xmm0,0x1d0(%rsp)
movaps %xmm0,0x1c0(%rsp)
movaps %xmm0,0x1b0(%rsp)
movaps %xmm0,0x1a0(%rsp)
movaps %xmm0,0x190(%rsp)
movaps %xmm0,0x180(%rsp)
movaps %xmm0,0x170(%rsp)
movaps %xmm0,0x160(%rsp)
movaps %xmm0,0x150(%rsp)
movaps %xmm0,0x140(%rsp)
movaps %xmm0,0x130(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
lea    0xd5e(%rip),%rdi
lea    0xd61(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1395 <main+0x1f5>
mov    %rax,%rbx
lea    0x120(%rsp),%r14
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
cmpb   $0xa,-0x1(%rax,%r14,1)
jne    12ea <main+0x14a>
movb   $0x0,0x11f(%rsp,%rax,1)
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xd13(%rip),%rdi
lea    0xd16(%rip),%rsi
call   10c0 <iconv_open@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
jne    131a <main+0x17a>
lea    0xd33(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,0x18(%rsp)
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rax,0x10(%rsp)
movq   $0x100,0x8(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,(%rsp)
lea    0x18(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
lea    0x8(%rsp),%r8
mov    %rbx,%rdi
call   1030 <iconv@plt>
cmp    $0xffffffffffffffff,%rax
je     13b1 <main+0x211>
mov    %rbx,%rdi
call   1090 <iconv_close@plt>
lea    0xcc2(%rip),%rdi
lea    0xcc5(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%rbx
lea    0x20(%rsp),%rdi
mov    %rax,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    13a6 <main+0x206>
lea    0xcbe(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
add    $0x228,%rsp
pop    %rbx
pop    %r14
ret
lea    0xc83(%rip),%rdi
call   1040 <puts@plt>
lea    0xc58(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xfffffffd,%eax
jmp    13a6 <main+0x206>

LLVM IR (-O0):
@.str = [10 x i8] c"test1.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c"fopen error!\0A\00"
@.str.3 = [10 x i8] c"Shift_JIS\00"
@.str.4 = [6 x i8] c"utf-8\00"
@.str.5 = [19 x i8] c"iconv_open error!\0A\00"
@.str.6 = [14 x i8] c"iconv error!\0A\00"
@.str.7 = [19 x i8] c"iconv error detail\00"
@.str.8 = [10 x i8] c"test2.txt\00"
@.str.9 = [2 x i8] c"w\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [256 x i8]
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca [256 x i8]
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %1
store i32* null, i32** %2
%11 = bitcast [256 x i8]* %3 to i8*
%12 = bitcast [256 x i8]* %7 to i8*
%13 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %2
%14 = load i32*, i32** %2
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %63
18: ; preds = %0
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%20 = load i32*, i32** %2
%21 = call i32 @fgets(i8* %19, i32 256, i32* %20)
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%23 = call i64 @strlen(i8* %22)
store i64 %23, i64* %5
%24 = load i64, i64* %5
%25 = sub i64 %24, 1
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %34
30: ; preds = %18
%31 = load i64, i64* %5
%32 = sub i64 %31, 1
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %30, %18
%35 = load i32*, i32** %2
%36 = call i32 @fclose(i32* %35)
%37 = call i64 @iconv_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i64 %37, i64* %6
%38 = load i64, i64* %6
%39 = icmp eq i64 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
store i8* %43, i8** %4
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%45 = call i64 @strlen(i8* %44)
store i64 %45, i64* %5
store i64 256, i64* %8
%46 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
store i8* %46, i8** %9
%47 = load i64, i64* %6
%48 = call i64 @iconv(i64 %47, i8** %4, i64* %5, i8** %9, i64* %8)
store i64 %48, i64* %10
%49 = load i64, i64* %10
%50 = icmp eq i64 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %42
%52 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
%53 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
store i32 -3, i32* %1
br label %63
54: ; preds = %42
%55 = load i64, i64* %6
%56 = call i32 @iconv_close(i64 %55)
%57 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
store i32* %57, i32** %2
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%59 = load i32*, i32** %2
%60 = call i32 @fputs(i8* %58, i32* %59)
%61 = load i32*, i32** %2
%62 = call i32 @fclose(i32* %61)
store i32 0, i32* %1
br label %63
63: ; preds = %54, %51, %16
%64 = load i32, i32* %1
ret i32 %64
}

LLVM IR (-O2):
@.str = [10 x i8] c"test1.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [10 x i8] c"Shift_JIS\00"
@.str.4 = [6 x i8] c"utf-8\00"
@.str.7 = [19 x i8] c"iconv error detail\00"
@.str.8 = [10 x i8] c"test2.txt\00"
@.str.9 = [2 x i8] c"w\00"
@str = [13 x i8] c"iconv error!\00"
@str.10 = [18 x i8] c"iconv_open error!\00"
@str.11 = [13 x i8] c"fopen error!\00"
define i32 @main() {
%1 = alloca [256 x i8]
%2 = alloca i8*
%3 = alloca i64
%4 = alloca [256 x i8]
%5 = alloca i64
%6 = alloca i8*
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%8 = bitcast i8** %2 to i8*
%9 = bitcast i64* %3 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%11 = bitcast i64* %5 to i8*
%12 = bitcast i8** %6 to i8*
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.11, i64 0, i64 0))
br label %43
17: ; preds = %0
%18 = call i32 @fgets(i8* %7, i32 256, i32* %13)
%19 = call i64 @strlen(i8* %7)
%20 = add i64 %19, -1
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %24, label %25
24: ; preds = %17
store i8 0, i8* %21
br label %25
25: ; preds = %24, %17
%26 = call i32 @fclose(i32* %13)
%27 = call i64 @iconv_open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%28 = icmp eq i64 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.10, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
store i8* %7, i8** %2
%32 = call i64 @strlen(i8* %7)
store i64 %32, i64* %3
store i64 256, i64* %5
store i8* %10, i8** %6
%33 = call i64 @iconv(i64 %27, i8** %2, i64* %3, i8** %6, i64* %5)
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
br label %43
38: ; preds = %31
%39 = call i32 @iconv_close(i64 %27)
%40 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
%41 = call i32 @fputs(i8* %10, i32* %40)
%42 = call i32 @fclose(i32* %40)
br label %43
43: ; preds = %38, %35, %15
%44 = phi i32 [ -1, %15 ], [ -3, %35 ], [ 0, %38 ]
ret i32 %44
}