#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ remove(char *);
int main(int argc, char **argv) {
  char *fname = argv[1];
  FILE *f;
  char mydata_start[10] = "abcDEFghi";
  char mydata_end[10];
  int num;
  f = fopen(fname, "w");
  fwrite(mydata_start, 1, 10, f);
  fclose(f);
  remove(fname);
  f = fopen(fname, "r");
  if (f == NULL) {
    printf("could not open file\n");
  } else {
    num = fread(mydata_end, 1, 10, f);
    printf("Read %i bytes: \n", num);
    for (int i = 0; i < num; i++) {
      printf("%c\n", mydata_end[i]);
    }
  }
  f = fopen(fname, "w");
  fwrite(mydata_start, 1, 10, f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0xe77(%rip),%rax
mov    %rax,-0x2a(%rbp)
mov    0xe74(%rip),%ax
mov    %ax,-0x22(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x2a(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0xa,%edx
call   1080 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x18(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1207 <main+0xa7>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    126f <main+0x10f>
lea    -0x34(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0xa,%edx
call   1040 <fread@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%esi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x38(%rbp),%eax
jge    126a <main+0x10a>
movslq -0x3c(%rbp),%rax
movsbl -0x34(%rbp,%rax,1),%esi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1239 <main+0xd9>
jmp    126f <main+0x10f>
mov    -0x18(%rbp),%rdi
lea    0xd90(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x2a(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0xa,%edx
call   1080 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x8(%rsi),%r14
movabs $0x6867464544636261,%rax
46 67 68
mov    %rax,(%rsp)
movw   $0x69,0x8(%rsp)
lea    0xe6f(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0xa,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1030 <remove@plt>
lea    0xe3a(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     123a <main+0xca>
lea    0xe(%rsp),%rdi
mov    $0x1,%esi
mov    $0xa,%edx
mov    %rax,%rcx
call   1050 <fread@plt>
mov    %eax,%ebx
lea    0xe0f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebx,%ebx
jle    1246 <main+0xd6>
mov    %ebx,%r12d
lea    0xe09(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl 0xe(%rsp,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1220 <main+0xb0>
jmp    1246 <main+0xd6>
lea    0xde2(%rip),%rdi
call   1040 <puts@plt>
lea    0xdbd(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0xa,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.mydata_start = [10 x i8] c"abcDEFghi\00"
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"could not open file\0A\00"
@.str.3 = [17 x i8] c"Read %i bytes: \0A\00"
@.str.4 = [4 x i8] c"%c\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca [10 x i8]
%9 = alloca [10 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %6
%15 = bitcast [10 x i8]* %8 to i8*
%16 = load i8*, i8** %6
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %7
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%19 = load i32*, i32** %7
%20 = call i32 @fwrite(i8* %18, i32 1, i32 10, i32* %19)
%21 = load i32*, i32** %7
%22 = call i32 @fclose(i32* %21)
%23 = load i8*, i8** %6
%24 = call i32 @remove(i8* %23)
%25 = load i8*, i8** %6
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %7
%27 = load i32*, i32** %7
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %2
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %52
31: ; preds = %2
%32 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 0
%33 = load i32*, i32** %7
%34 = call i32 @fread(i8* %32, i32 1, i32 10, i32* %33)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %35)
store i32 0, i32* %11
br label %37
37: ; preds = %48, %31
%38 = load i32, i32* %11
%39 = load i32, i32* %10
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %51
41: ; preds = %37
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [10 x i8], [10 x i8]* %9, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %41
%49 = load i32, i32* %11
%50 = add nsw i32 %49, 1
store i32 %50, i32* %11
br label %37
51: ; preds = %37
br label %52
52: ; preds = %51, %29
%53 = load i8*, i8** %6
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %54, i32** %7
%55 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%56 = load i32*, i32** %7
%57 = call i32 @fwrite(i8* %55, i32 1, i32 10, i32* %56)
%58 = load i32*, i32** %7
%59 = call i32 @fclose(i32* %58)
ret i32 0
}

LLVM IR (-O2):
@__const.main.mydata_start = [10 x i8] c"abcDEFghi\00"
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@.str.3 = [17 x i8] c"Read %i bytes: \0A\00"
@.str.4 = [4 x i8] c"%c\0A\00"
@str = [20 x i8] c"could not open file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = alloca [10 x i8]
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%9 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @fwrite(i8* %7, i32 1, i32 10, i32* %9)
%11 = tail call i32 @fclose(i32* %9)
%12 = tail call i32 @remove(i8* %6)
%13 = tail call i32* @fopen(i8* %6, 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 = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %31
17: ; preds = %2
%18 = call i32 @fread(i8* %8, i32 1, i32 10, i32* %13)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = icmp sgt i32 %18, 0
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = zext i32 %18 to i64
br label %23
23: ; preds = %21, %23
%24 = phi i64 [ 0, %21 ], [ %29, %23 ]
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 %24
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = add nuw nsw i64 %24, 1
%30 = icmp eq i64 %29, %22
br i1 %30, label %31, label %23
31: ; preds = %23, %17, %15
%32 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @fwrite(i8* %7, i32 1, i32 10, i32* %32)
%34 = tail call i32 @fclose(i32* %32)
ret i32 0
}