#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 */
char EOF;
char conv(char);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ *tmpfile();
int convert(const char *fname) {
  FILE *tin;
  FILE *in;
  char ch;
  if ((tin = tmpfile()) == NULL) {
    perror("tmpfile()");
    return -1;
  }
  if ((in = fopen(fname, "r+")) == NULL) {
    perror(fname);
    return -1;
  }
  while ((ch = fgetc(in)) != EOF) {
    ch = conv(ch);
    fputc(ch, tin);
  }
  rewind(in);
  rewind(tin);
  while ((ch = fgetc(tin)) != EOF) {
    fputc(ch, in);
  }
  fclose(in);
  fclose(tin);
  return 0;
}


x86 assembly (clang, -O0):
<convert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <tmpfile@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11b9 <convert+0x39>
lea    0xe58(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <convert+0x112>
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ec <convert+0x6c>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1292 <convert+0x112>
jmp    11f1 <convert+0x71>
mov    -0x20(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x21(%rbp)
movsbl %al,%eax
mov    0x2de1(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1232 <convert+0xb2>
movsbl -0x21(%rbp),%edi
call   1040 <conv@plt>
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
mov    -0x18(%rbp),%rsi
movsbl %al,%edi
call   1070 <fputc@plt>
jmp    11f1 <convert+0x71>
mov    -0x20(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %al,-0x21(%rbp)
movsbl %al,%eax
mov    0x2d8e(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     1279 <convert+0xf9>
mov    -0x21(%rbp),%al
mov    -0x20(%rbp),%rsi
movsbl %al,%edi
call   1070 <fputc@plt>
jmp    1244 <convert+0xc4>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %eax,%eax
call   1080 <tmpfile@plt>
test   %rax,%rax
je     1232 <convert+0xb2>
mov    %rax,%r14
lea    0xe68(%rip),%rsi
mov    %r15,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     123b <convert+0xbb>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fgetc@plt>
mov    0x2e23(%rip),%r15
cmp    (%r15),%al
je     11f0 <convert+0x70>
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1040 <conv@plt>
movsbl %al,%edi
mov    %r14,%rsi
call   1070 <fputc@plt>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r15),%al
jne    11d0 <convert+0x50>
mov    %rbx,%rdi
call   1050 <rewind@plt>
mov    %r14,%rdi
call   1050 <rewind@plt>
mov    %r14,%rdi
call   1060 <fgetc@plt>
cmp    (%r15),%al
je     121a <convert+0x9a>
movsbl %al,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
jmp    1200 <convert+0x80>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdc7(%rip),%rdi
jmp    123e <convert+0xbe>
mov    %r15,%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
jmp    122c <convert+0xac>

LLVM IR (-O0):
@.str = [10 x i8] c"tmpfile()\00"
@.str.1 = [3 x i8] c"r+\00"
@EOF = global i8 0
define i32 @convert(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i8
store i8* %0, i8** %3
%7 = call i32* (...) @tmpfile()
store i32* %7, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %53
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %5
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = call i32 @perror(i8* %16)
store i32 -1, i32* %2
br label %53
18: ; preds = %11
br label %19
19: ; preds = %26, %18
%20 = load i32*, i32** %5
%21 = call signext i8 @fgetc(i32* %20)
store i8 %21, i8* %6
%22 = sext i8 %21 to i32
%23 = load i8, i8* @EOF
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %22, %24
br i1 %25, label %26, label %32
26: ; preds = %19
%27 = load i8, i8* %6
%28 = call signext i8 @conv(i8 signext %27)
store i8 %28, i8* %6
%29 = load i8, i8* %6
%30 = load i32*, i32** %4
%31 = call i32 @fputc(i8 signext %29, i32* %30)
br label %19
32: ; preds = %19
%33 = load i32*, i32** %5
%34 = call i32 @rewind(i32* %33)
%35 = load i32*, i32** %4
%36 = call i32 @rewind(i32* %35)
br label %37
37: ; preds = %44, %32
%38 = load i32*, i32** %4
%39 = call signext i8 @fgetc(i32* %38)
store i8 %39, i8* %6
%40 = sext i8 %39 to i32
%41 = load i8, i8* @EOF
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %40, %42
br i1 %43, label %44, label %48
44: ; preds = %37
%45 = load i8, i8* %6
%46 = load i32*, i32** %5
%47 = call i32 @fputc(i8 signext %45, i32* %46)
br label %37
48: ; preds = %37
%49 = load i32*, i32** %5
%50 = call i32 @fclose(i32* %49)
%51 = load i32*, i32** %4
%52 = call i32 @fclose(i32* %51)
store i32 0, i32* %2
br label %53
53: ; preds = %48, %15, %9
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@.str = [10 x i8] c"tmpfile()\00"
@.str.1 = [3 x i8] c"r+\00"
@EOF = global i8 0
define i32 @convert(i8* nocapture %0) {
%2 = tail call i32* (...) @tmpfile()
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %37
6: ; preds = %1
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %13, label %9
9: ; preds = %6
%10 = tail call signext i8 @fgetc(i32* %7)
%11 = load i8, i8* @EOF
%12 = icmp eq i8 %10, %11
br i1 %12, label %22, label %15
13: ; preds = %6
%14 = tail call i32 @perror(i8* %0)
br label %37
15: ; preds = %9, %15
%16 = phi i8 [ %19, %15 ], [ %10, %9 ]
%17 = tail call signext i8 @conv(i8 signext %16)
%18 = tail call i32 @fputc(i8 signext %17, i32* %2)
%19 = tail call signext i8 @fgetc(i32* %7)
%20 = load i8, i8* @EOF
%21 = icmp eq i8 %19, %20
br i1 %21, label %22, label %15
22: ; preds = %15, %9
%23 = tail call i32 @rewind(i32* %7)
%24 = tail call i32 @rewind(i32* %2)
%25 = tail call signext i8 @fgetc(i32* %2)
%26 = load i8, i8* @EOF
%27 = icmp eq i8 %25, %26
br i1 %27, label %34, label %28
28: ; preds = %22, %28
%29 = phi i8 [ %31, %28 ], [ %25, %22 ]
%30 = tail call i32 @fputc(i8 signext %29, i32* %7)
%31 = tail call signext i8 @fgetc(i32* %2)
%32 = load i8, i8* @EOF
%33 = icmp eq i8 %31, %32
br i1 %33, label %34, label %28
34: ; preds = %28, %22
%35 = tail call i32 @fclose(i32* %7)
%36 = tail call i32 @fclose(i32* %2)
br label %37
37: ; preds = %34, %13, %4
%38 = phi i32 [ -1, %4 ], [ -1, %13 ], [ 0, %34 ]
ret i32 %38
}