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

/* Variables and functions */
int /*<<< orphan*/ fatfs_close(struct fnode *);
int /*<<< orphan*/ printf(char *, char *);
struct fnode *vfs_open(char *, int);
int vfs_write(struct fnode *, int /*<<< orphan*/ *, int);
int mk_file(char *path, uint8_t *buf, int len) {
  if (!path)
    return -1;
  printf("C:\\CP MEM %s\n", path);
  struct fnode *fno = vfs_open(path, 0x00);
  if (!fno)
    return -2;
  int res = vfs_write(fno, buf, len);
  fatfs_close(fno);
  return res;
}


x86 assembly (clang, -O0):
<mk_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116a <mk_file+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <mk_file+0x82>
mov    -0x10(%rbp),%rsi
lea    0xe8b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <vfs_open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a0 <mk_file+0x60>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11c2 <mk_file+0x82>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <vfs_write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fatfs_close@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mk_file>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     118f <mk_file+0x4f>
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xea7(%rip),%rdi
mov    %rbp,%rsi
call   1030 <printf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
call   1060 <vfs_open@plt>
test   %rax,%rax
je     1196 <mk_file+0x56>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <vfs_write@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1050 <fatfs_close@plt>
mov    %ebx,%eax
jmp    119b <mk_file+0x5b>
mov    $0xffffffff,%eax
jmp    119b <mk_file+0x5b>
mov    $0xfffffffe,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.fnode = type { i32 }
@.str = [14 x i8] c"C:\\CP MEM %s\0A\00"
define i32 @mk_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca %struct.fnode*
%9 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %3
store i32 -1, i32* %4
br label %29
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i8*, i8** %5
%17 = call %struct.fnode* @vfs_open(i8* %16, i32 0)
store %struct.fnode* %17, %struct.fnode** %8
%18 = load %struct.fnode*, %struct.fnode** %8
%19 = icmp ne %struct.fnode* %18, null
br i1 %19, label %21, label %20
20: ; preds = %13
store i32 -2, i32* %4
br label %29
21: ; preds = %13
%22 = load %struct.fnode*, %struct.fnode** %8
%23 = load i32*, i32** %6
%24 = load i32, i32* %7
%25 = call i32 @vfs_write(%struct.fnode* %22, i32* %23, i32 %24)
store i32 %25, i32* %9
%26 = load %struct.fnode*, %struct.fnode** %8
%27 = call i32 @fatfs_close(%struct.fnode* %26)
%28 = load i32, i32* %9
store i32 %28, i32* %4
br label %29
29: ; preds = %21, %20, %12
%30 = load i32, i32* %4
ret i32 %30
}

LLVM IR (-O2):
%struct.fnode = type { i32 }
@.str = [14 x i8] c"C:\\CP MEM %s\0A\00"
define i32 @mk_file(i8* %0, i32* %1, i32 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %12, label %5
5: ; preds = %3
%6 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = tail call %struct.fnode* @vfs_open(i8* %0, i32 0)
%8 = icmp eq %struct.fnode* %7, null
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = tail call i32 @vfs_write(%struct.fnode* %7, i32* %1, i32 %2)
%11 = tail call i32 @fatfs_close(%struct.fnode* %7)
br label %12
12: ; preds = %9, %5, %3
%13 = phi i32 [ -1, %3 ], [ %10, %9 ], [ -2, %5 ]
ret i32 %13
}