#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__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
main() {
  int ret;
  FILE *fp_read;
  FILE *fp_write;
  unsigned char buf[2048];
  fp_read = fopen("./yzzfs", "rb");
  if (fp_read == NULL) {
    printf("open yzzfs failed!\n");
    return 1;
  }
  fp_write = fopen("./yzzfs.new", "wb");
  if (fp_write == NULL) {
    printf("open yzzfs.new failed!\n");
    return 2;
  }
  ret = fread(buf, sizeof(unsigned char), 2048, fp_read);
  printf("previous magic number is 0x%02x%02x\n", buf[0x439], buf[0x438]);
  buf[0x439] = 0x04;
  buf[0x438] = 0x0c;
  fwrite(buf, sizeof(unsigned char), 2048, fp_write);
  printf("current magic number is 0x%02x%02x\n", buf[0x439], buf[0x438]);
  while (ret == 2048) {
    ret = fread(buf, sizeof(unsigned char), 2048, fp_read);
    fwrite(buf, sizeof(unsigned char), ret, fp_write);
  }
  if (ret < 2048 && feof(fp_read)) {
    printf("done changing magic number\n");
  }
  fclose(fp_read);
  fclose(fp_write);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe87(%rip),%rdi
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ae <main+0x4e>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f5 <main+0x195>
lea    0xe6a(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ea <main+0x8a>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    12f5 <main+0x195>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x800,%edx
call   1030 <fread@plt>
mov    %eax,-0x8(%rbp)
movzbl -0x3e7(%rbp),%esi
movzbl -0x3e8(%rbp),%edx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movb   $0x4,-0x3e7(%rbp)
movb   $0xc,-0x3e8(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x800,%edx
call   1080 <fwrite@plt>
movzbl -0x3e7(%rbp),%esi
movzbl -0x3e8(%rbp),%edx
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x800,-0x8(%rbp)
jne    12ae <main+0x14e>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x800,%edx
call   1030 <fread@plt>
mov    %eax,-0x8(%rbp)
lea    -0x820(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
jmp    1267 <main+0x107>
cmpl   $0x800,-0x8(%rbp)
jge    12dc <main+0x17c>
mov    -0x10(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     12dc <main+0x17c>
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x808,%rsp
lea    0xe7c(%rip),%rdi
lea    0xe7d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12a6 <main+0x136>
mov    %rax,%r15
lea    0xe68(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12b9 <main+0x149>
mov    %rax,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
movzbl 0x439(%rsp),%esi
movzbl 0x438(%rsp),%edx
lea    0xe2e(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movw   $0x40c,0x438(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
movzbl 0x439(%rsp),%esi
movzbl 0x438(%rsp),%edx
lea    0xe16(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmp    $0x800,%ebp
jne    1271 <main+0x101>
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x800,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   1090 <fwrite@plt>
cmp    $0x800,%ebp
je     1240 <main+0xd0>
cmp    $0x7ff,%ebp
jg     1292 <main+0x122>
mov    %r15,%rdi
call   1070 <feof@plt>
test   %rax,%rax
je     1292 <main+0x122>
lea    0xdd6(%rip),%rdi
call   1030 <puts@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    12ca <main+0x15a>
lea    0xde8(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    12ca <main+0x15a>
lea    0xdbe(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"./yzzfs\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [20 x i8] c"open yzzfs failed!\0A\00"
@.str.3 = [12 x i8] c"./yzzfs.new\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.5 = [24 x i8] c"open yzzfs.new failed!\0A\00"
@.str.6 = [37 x i8] c"previous magic number is 0x%02x%02x\0A\00"
@.str.7 = [36 x i8] c"current magic number is 0x%02x%02x\0A\00"
@.str.8 = [28 x i8] c"done changing magic number\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [2048 x i8]
store i32 0, i32* %1
%6 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %65
11: ; preds = %0
%12 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %12, i32** %4
%13 = load i32*, i32** %4
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
store i32 2, i32* %1
br label %65
17: ; preds = %11
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%19 = load i32*, i32** %3
%20 = call i32 @fread(i8* %18, i32 1, i32 2048, i32* %19)
store i32 %20, i32* %2
%21 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 1081
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 1080
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %23, i32 %26)
%28 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 1081
store i8 4, i8* %28
%29 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 1080
store i8 12, i8* %29
%30 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%31 = load i32*, i32** %4
%32 = call i32 @fwrite(i8* %30, i32 1, i32 2048, i32* %31)
%33 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 1081
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
%36 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 1080
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 %35, i32 %38)
br label %40
40: ; preds = %43, %17
%41 = load i32, i32* %2
%42 = icmp eq i32 %41, 2048
br i1 %42, label %43, label %51
43: ; preds = %40
%44 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%45 = load i32*, i32** %3
%46 = call i32 @fread(i8* %44, i32 1, i32 2048, i32* %45)
store i32 %46, i32* %2
%47 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%48 = load i32, i32* %2
%49 = load i32*, i32** %4
%50 = call i32 @fwrite(i8* %47, i32 1, i32 %48, i32* %49)
br label %40
51: ; preds = %40
%52 = load i32, i32* %2
%53 = icmp slt i32 %52, 2048
br i1 %53, label %54, label %60
54: ; preds = %51
%55 = load i32*, i32** %3
%56 = call i64 @feof(i32* %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %60
60: ; preds = %58, %54, %51
%61 = load i32*, i32** %3
%62 = call i32 @fclose(i32* %61)
%63 = load i32*, i32** %4
%64 = call i32 @fclose(i32* %63)
store i32 0, i32* %1
br label %65
65: ; preds = %60, %15, %9
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [8 x i8] c"./yzzfs\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.3 = [12 x i8] c"./yzzfs.new\00"
@.str.4 = [3 x i8] c"wb\00"
@.str.6 = [37 x i8] c"previous magic number is 0x%02x%02x\0A\00"
@.str.7 = [36 x i8] c"current magic number is 0x%02x%02x\0A\00"
@str = [27 x i8] c"done changing magic number\00"
@str.9 = [23 x i8] c"open yzzfs.new failed!\00"
@str.10 = [19 x i8] c"open yzzfs failed!\00"
define i32 @main() {
%1 = alloca [2048 x i8]
%2 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
br label %43
7: ; preds = %0
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
br label %43
12: ; preds = %7
%13 = call i32 @fread(i8* %2, i32 1, i32 2048, i32* %3)
%14 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 1081
%15 = load i8, i8* %14
%16 = zext i8 %15 to i32
%17 = getelementptr inbounds [2048 x i8], [2048 x i8]* %1, i64 0, i64 1080
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), i32 %16, i32 %19)
store i8 4, i8* %14
store i8 12, i8* %17
%21 = call i32 @fwrite(i8* %2, i32 1, i32 2048, i32* %8)
%22 = load i8, i8* %14
%23 = zext i8 %22 to i32
%24 = load i8, i8* %17
%25 = zext i8 %24 to i32
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.7, i64 0, i64 0), i32 %23, i32 %25)
%27 = icmp eq i32 %13, 2048
br i1 %27, label %28, label %32
28: ; preds = %12, %28
%29 = call i32 @fread(i8* %2, i32 1, i32 2048, i32* %3)
%30 = call i32 @fwrite(i8* %2, i32 1, i32 %29, i32* %8)
%31 = icmp eq i32 %29, 2048
br i1 %31, label %28, label %32
32: ; preds = %28, %12
%33 = phi i32 [ %13, %12 ], [ %29, %28 ]
%34 = icmp slt i32 %33, 2048
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = tail call i64 @feof(i32* %3)
%37 = icmp eq i64 %36, 0
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %38, %35, %32
%41 = tail call i32 @fclose(i32* %3)
%42 = tail call i32 @fclose(i32* %8)
br label %43
43: ; preds = %40, %10, %5
%44 = phi i32 [ 1, %5 ], [ 2, %10 ], [ 0, %40 ]
ret i32 %44
}