#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 uint8_t;
struct fnode {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ fatfs_close(struct fnode *);
int /*<<< orphan*/ printf(char *, ...);
struct fnode *vfs_open(char *, int);
int vfs_read(struct fnode *, int *, int);
int vfs_seek(struct fnode *, int, int /*<<< orphan*/);
int vfs_write(struct fnode *, int *, int);
int edit_file(char *path) {
  if (!path)
    return -1;
  printf("C:\\VI %s\n", path);
  struct fnode *fno = vfs_open(path, 0x02);
  if (!fno)
    return -2;
  uint8_t buf[4] = {0xDE, 0xAD, 0xBE, 0xEF};
  uint8_t buf2[4] = {0x42, 0x42, 0x42, 0x42};
  int res;
  res = vfs_seek(fno, (5096 + 256), 0);
  res = vfs_write(fno, buf, 4);
  res = vfs_seek(fno, (5096 + 256), 0);
  res = vfs_read(fno, buf2, 4);
  printf("Found 0x%02X%02X%02X%02X\n", buf2[0], buf2[1], buf2[2], buf2[3]);
  fatfs_close(fno);
  return res;
}


x86 assembly (clang, -O0):
<edit_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1183 <edit_file+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <edit_file+0x104>
mov    -0x10(%rbp),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1080 <vfs_open@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11be <edit_file+0x5e>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1264 <edit_file+0x104>
mov    0xe6b(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe68(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe65(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe62(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x14e8,%esi
xor    %edx,%edx
call   1050 <vfs_seek@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1060 <vfs_write@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x14e8,%esi
xor    %edx,%edx
call   1050 <vfs_seek@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x4,%edx
call   1030 <vfs_read@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%esi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1070 <fatfs_close@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<edit_file>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
test   %rdi,%rdi
je     121d <edit_file+0xbd>
mov    %rdi,%rbx
lea    0xe87(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    $0x2,%esi
call   1080 <vfs_open@plt>
test   %rax,%rax
je     1224 <edit_file+0xc4>
mov    %rax,%rbx
movaps 0xe8d(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe91(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rax,%rdi
mov    $0x14e8,%esi
xor    %edx,%edx
call   1050 <vfs_seek@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1060 <vfs_write@plt>
mov    %rbx,%rdi
mov    $0x14e8,%esi
xor    %edx,%edx
call   1050 <vfs_seek@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <vfs_read@plt>
mov    %eax,%ebp
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
lea    0xdfe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1070 <fatfs_close@plt>
jmp    1229 <edit_file+0xc9>
mov    $0xffffffff,%ebp
jmp    1229 <edit_file+0xc9>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.fnode = type { i32 }
@.str = [10 x i8] c"C:\\VI %s\0A\00"
@__const.edit_file.buf = [4 x i32] [i32 222, i32 173, i32 190, i32 239]
@__const.edit_file.buf2 = [4 x i32] [i32 66, i32 66, i32 66, i32 66]
@.str.1 = [26 x i8] c"Found 0x%02X%02X%02X%02X\0A\00"
define i32 @edit_file(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.fnode*
%5 = alloca [4 x i32]
%6 = alloca [4 x i32]
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %1
store i32 -1, i32* %2
br label %44
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i8*, i8** %3
%15 = call %struct.fnode* @vfs_open(i8* %14, i32 2)
store %struct.fnode* %15, %struct.fnode** %4
%16 = load %struct.fnode*, %struct.fnode** %4
%17 = icmp ne %struct.fnode* %16, null
br i1 %17, label %19, label %18
18: ; preds = %11
store i32 -2, i32* %2
br label %44
19: ; preds = %11
%20 = bitcast [4 x i32]* %5 to i8*
%21 = bitcast [4 x i32]* %6 to i8*
%22 = load %struct.fnode*, %struct.fnode** %4
%23 = call i32 @vfs_seek(%struct.fnode* %22, i32 5352, i32 0)
store i32 %23, i32* %7
%24 = load %struct.fnode*, %struct.fnode** %4
%25 = getelementptr inbounds [4 x i32], [4 x i32]* %5, i64 0, i64 0
%26 = call i32 @vfs_write(%struct.fnode* %24, i32* %25, i32 4)
store i32 %26, i32* %7
%27 = load %struct.fnode*, %struct.fnode** %4
%28 = call i32 @vfs_seek(%struct.fnode* %27, i32 5352, i32 0)
store i32 %28, i32* %7
%29 = load %struct.fnode*, %struct.fnode** %4
%30 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%31 = call i32 @vfs_read(%struct.fnode* %29, i32* %30, i32 4)
store i32 %31, i32* %7
%32 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%33 = load i32, i32* %32
%34 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 2
%37 = load i32, i32* %36
%38 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 3
%39 = load i32, i32* %38
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %33, i32 %35, i32 %37, i32 %39)
%41 = load %struct.fnode*, %struct.fnode** %4
%42 = call i32 @fatfs_close(%struct.fnode* %41)
%43 = load i32, i32* %7
store i32 %43, i32* %2
br label %44
44: ; preds = %19, %18, %10
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
%struct.fnode = type { i32 }
@.str = [10 x i8] c"C:\\VI %s\0A\00"
@__const.edit_file.buf = [4 x i32] [i32 222, i32 173, i32 190, i32 239]
@__const.edit_file.buf2 = [4 x i32] [i32 66, i32 66, i32 66, i32 66]
@.str.1 = [26 x i8] c"Found 0x%02X%02X%02X%02X\0A\00"
define i32 @edit_file(i8* %0) {
%2 = alloca [4 x i32]
%3 = alloca [4 x i32]
%4 = icmp eq i8* %0, null
br i1 %4, label %27, label %5
5: ; preds = %1
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = tail call %struct.fnode* @vfs_open(i8* %0, i32 2)
%8 = icmp eq %struct.fnode* %7, null
br i1 %8, label %27, label %9
9: ; preds = %5
%10 = bitcast [4 x i32]* %2 to i8*
%11 = bitcast [4 x i32]* %3 to i8*
%12 = tail call i32 @vfs_seek(%struct.fnode* %7, i32 5352, i32 0)
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %2, i64 0, i64 0
%14 = call i32 @vfs_write(%struct.fnode* %7, i32* %13, i32 4)
%15 = call i32 @vfs_seek(%struct.fnode* %7, i32 5352, i32 0)
%16 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%17 = call i32 @vfs_read(%struct.fnode* %7, i32* %16, i32 4)
%18 = load i32, i32* %16
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%22 = load i32, i32* %21
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
%24 = load i32, i32* %23
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 %20, i32 %22, i32 %24)
%26 = call i32 @fatfs_close(%struct.fnode* %7)
br label %27
27: ; preds = %9, %5, %1
%28 = phi i32 [ -1, %1 ], [ %17, %9 ], [ -2, %5 ]
ret i32 %28
}