#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 */
struct stat {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ message;

/* Variables and functions */
scalar_t__ ENOENT;
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SEEK_CUR;
int close(int);
scalar_t__ errno;
int fsync(int);
size_t fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int unlink(char const *);
int /*<<< orphan*/ wait_enter();
int write(int, char *, int /*<<< orphan*/);
int main(void) {
  const char *filepath = "rwtest.txt";
  int fdout;
  int fdin;
  char message[80];
  int result;
  struct stat s;
  printf("fatfs_unistd\n"
         "Press Enter to continue...\n");
  wait_enter();
  printf("Write a message:\n");
  scanf("%s", message);
  printf("Writing message to %s...\n", filepath);
  fdout = open(filepath, O_WRONLY | O_TRUNC | O_CREAT);
  if (fdout == -1) {
    perror(filepath);
    return 1;
  } else {
    result = write(fdout, message, strlen(message));
    if (result < 0) {
      perror(filepath);
      return 1;
    }
  }
  result = fsync(fdout);
  if (result != 0) {
    perror(filepath);
    return 1;
  }
  printf("pos = %ld.\n", lseek(fdout, 0, SEEK_CUR));
  result = close(fdout);
  if (result != 0) {
    perror(filepath);
    return 1;
  }
  printf("Done.\n"
         "Reading message from %s...\n",
         filepath);
  fdin = open(filepath, O_RDONLY);
  if (fdout == -1) {
    perror(filepath);
    return 1;
  } else {
    ssize_t nbytes;
    do {
      char *p;
      nbytes = read(fdin, message, sizeof(message));
      if (nbytes == -1) {
        perror(filepath);
        close(fdin);
        return 1;
      }
      if (nbytes == 0) {
        break;
      }
      p = message;
      while (nbytes > 0) {
        size_t nwritten;
        nwritten = fwrite(p, 1, nbytes, stdout);
        if (nwritten > 0) {
          nbytes -= nwritten;
          p += nwritten;
        }
      }
    } while (1);
  }
  printf("\npos = %ld.\n", lseek(fdin, 0, SEEK_CUR));
  result = close(fdin);
  if (result != 0) {
    perror(filepath);
    return 1;
  }
  printf("Done.\n"
         "Removing file...\n");
  result = stat(filepath, &s);
  if (result != 0) {
    perror(filepath);
    return 1;
  }
  result = unlink(filepath);
  if (result != 0) {
    perror(filepath);
    return 1;
  }
  result = stat(filepath, &s);
  if (result != -1 || errno != ENOENT) {
    perror(filepath);
    return 1;
  }
  printf("Done.\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe07(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10f0 <wait_enter@plt>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x70(%rbp),%rsi
lea    0xe1b(%rip),%rdi
call   10b0 <scanf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    0x2d9a(%rip),%rax
or     (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1288 <main+0xa8>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
mov    -0x14(%rbp),%eax
mov    %eax,-0x9c(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x98(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x9c(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    12db <main+0xfb>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
jmp    12e0 <main+0x100>
mov    -0x14(%rbp),%edi
call   10c0 <fsync@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     130a <main+0x12a>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
mov    -0x14(%rbp),%edi
mov    0x2cc4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,%esi
lea    0xd60(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     1357 <main+0x177>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
mov    -0x10(%rbp),%rsi
lea    0xd01(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2c5c(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    139d <main+0x1bd>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
jmp    13a2 <main+0x1c2>
mov    -0x18(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x50,%edx
call   1090 <read@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0xffffffff,-0x7c(%rbp)
jne    13dd <main+0x1fd>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
cmpl   $0x0,-0x7c(%rbp)
jne    13ec <main+0x20c>
jmp    1472 <main+0x292>
lea    -0x70(%rbp),%rax
mov    %rax,-0x88(%rbp)
cmpl   $0x0,-0x7c(%rbp)
jle    145e <main+0x27e>
mov    -0x88(%rbp),%rdi
mov    -0x7c(%rbp),%edx
mov    0x2ba6(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1100 <fwrite@plt>
mov    %rax,-0x90(%rbp)
cmpq   $0x0,-0x90(%rbp)
jbe    1459 <main+0x279>
mov    -0x90(%rbp),%rcx
movslq -0x7c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x7c(%rbp)
mov    -0x90(%rbp),%rax
add    -0x88(%rbp),%rax
mov    %rax,-0x88(%rbp)
jmp    13f7 <main+0x217>
jmp    1463 <main+0x283>
mov    $0x1,%al
test   $0x1,%al
jne    13a2 <main+0x1c2>
jmp    1472 <main+0x292>
jmp    1477 <main+0x297>
mov    -0x18(%rbp),%edi
mov    0x2b57(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,%esi
lea    0xbf2(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     14c4 <main+0x2e4>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
lea    0xbc7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x78(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     1501 <main+0x321>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
je     152c <main+0x34c>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
mov    -0x10(%rbp),%rdi
lea    -0x78(%rbp),%rsi
call   10a0 <stat@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0xffffffff,-0x74(%rbp)
jne    1560 <main+0x380>
mov    0x2a73(%rip),%rax
mov    (%rax),%rax
mov    0x2a51(%rip),%rcx
cmp    (%rcx),%rax
je     1575 <main+0x395>
mov    -0x10(%rbp),%rdi
call   10e0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    158a <main+0x3aa>
lea    0xb2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
lea    0xe52(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1100 <wait_enter@plt>
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
lea    0xde7(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10c0 <scanf@plt>
lea    0xdd9(%rip),%rdi
lea    0xdc4(%rip),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2d53(%rip),%rax
mov    0x2d9c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d99(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   10e0 <open@plt>
cmp    $0xffffffff,%eax
je     13f5 <main+0x205>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1050 <write@plt>
test   %eax,%eax
js     13f5 <main+0x205>
mov    %ebp,%edi
call   10d0 <fsync@plt>
test   %eax,%eax
jne    13f5 <main+0x205>
mov    0x2d2d(%rip),%r12
mov    (%r12),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1080 <lseek@plt>
lea    0xd8c(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   1090 <close@plt>
test   %eax,%eax
jne    13f5 <main+0x205>
lea    0xd4a(%rip),%rdi
lea    0xd1b(%rip),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10e0 <open@plt>
mov    %eax,%r15d
mov    0x2cae(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x50,%edx
call   10a0 <read@plt>
test   %eax,%eax
je     135e <main+0x16e>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1415 <main+0x225>
test   %ebx,%ebx
jle    1310 <main+0x120>
mov    %r14,%rbp
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%r13),%ecx
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
call   1110 <fwrite@plt>
add    %rax,%rbp
sub    %eax,%ebx
test   %ebx,%ebx
jg     1340 <main+0x150>
jmp    1310 <main+0x120>
mov    (%r12),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1080 <lseek@plt>
lea    0xcd7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r15d,%edi
call   1090 <close@plt>
test   %eax,%eax
jne    13f5 <main+0x205>
lea    0xd01(%rip),%rdi
call   1040 <puts@plt>
lea    0xc65(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
test   %eax,%eax
jne    13f5 <main+0x205>
lea    0xc50(%rip),%rdi
call   1030 <unlink@plt>
test   %eax,%eax
jne    13f5 <main+0x205>
lea    0xc40(%rip),%rdi
lea    0x8(%rsp),%rsi
call   10b0 <stat@plt>
cmp    $0xffffffff,%eax
jne    13f5 <main+0x205>
mov    0x2bea(%rip),%rax
mov    (%rax),%rax
mov    0x2bc8(%rip),%rcx
cmp    (%rcx),%rax
jne    13f5 <main+0x205>
lea    0xcbb(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    1406 <main+0x216>
lea    0xc04(%rip),%rdi
call   10f0 <perror@plt>
mov    $0x1,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xbe4(%rip),%rdi
call   10f0 <perror@plt>
mov    %r15d,%edi
call   1090 <close@plt>
jmp    1401 <main+0x211>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [11 x i8] c"rwtest.txt\00"
@.str.1 = [41 x i8] c"fatfs_unistd\0APress Enter to continue...\0A\00"
@.str.2 = [18 x i8] c"Write a message:\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [26 x i8] c"Writing message to %s...\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.5 = [12 x i8] c"pos = %ld.\0A\00"
@SEEK_CUR = global i32 0
@.str.6 = [34 x i8] c"Done.\0AReading message from %s...\0A\00"
@O_RDONLY = global i32 0
@stdout = global i32 0
@.str.7 = [13 x i8] c"\0Apos = %ld.\0A\00"
@.str.8 = [24 x i8] c"Done.\0ARemoving file...\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.9 = [7 x i8] c"Done.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [80 x i8]
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %1
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (...) @wait_enter()
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%16 = load i8*, i8** %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %16)
%18 = load i8*, i8** %2
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %18, i32 %23)
store i32 %24, i32* %3
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %0
%28 = load i8*, i8** %2
%29 = call i32 @perror(i8* %28)
store i32 1, i32* %1
br label %157
30: ; preds = %0
%31 = load i32, i32* %3
%32 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %30
%39 = load i8*, i8** %2
%40 = call i32 @perror(i8* %39)
store i32 1, i32* %1
br label %157
41: ; preds = %30
br label %42
42: ; preds = %41
%43 = load i32, i32* %3
%44 = call i32 @fsync(i32 %43)
store i32 %44, i32* %6
%45 = load i32, i32* %6
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i8*, i8** %2
%49 = call i32 @perror(i8* %48)
store i32 1, i32* %1
br label %157
50: ; preds = %42
%51 = load i32, i32* %3
%52 = load i32, i32* @SEEK_CUR
%53 = call i32 @lseek(i32 %51, i32 0, i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %3
%56 = call i32 @close(i32 %55)
store i32 %56, i32* %6
%57 = load i32, i32* %6
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %50
%60 = load i8*, i8** %2
%61 = call i32 @perror(i8* %60)
store i32 1, i32* %1
br label %157
62: ; preds = %50
%63 = load i8*, i8** %2
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i8* %63)
%65 = load i8*, i8** %2
%66 = load i32, i32* @O_RDONLY
%67 = call i32 @open(i8* %65, i32 %66)
store i32 %67, i32* %4
%68 = load i32, i32* %3
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %62
%71 = load i8*, i8** %2
%72 = call i32 @perror(i8* %71)
store i32 1, i32* %1
br label %157
73: ; preds = %62
br label %74
74: ; preds = %112, %73
%75 = load i32, i32* %4
%76 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%77 = call i32 @read(i32 %75, i8* %76, i32 80)
store i32 %77, i32* %8
%78 = load i32, i32* %8
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %85
80: ; preds = %74
%81 = load i8*, i8** %2
%82 = call i32 @perror(i8* %81)
%83 = load i32, i32* %4
%84 = call i32 @close(i32 %83)
store i32 1, i32* %1
br label %157
85: ; preds = %74
%86 = load i32, i32* %8
%87 = icmp eq i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %85
br label %113
89: ; preds = %85
%90 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
store i8* %90, i8** %9
br label %91
91: ; preds = %110, %89
%92 = load i32, i32* %8
%93 = icmp sgt i32 %92, 0
br i1 %93, label %94, label %111
94: ; preds = %91
%95 = load i8*, i8** %9
%96 = load i32, i32* %8
%97 = load i32, i32* @stdout
%98 = call i64 @fwrite(i8* %95, i32 1, i32 %96, i32 %97)
store i64 %98, i64* %10
%99 = load i64, i64* %10
%100 = icmp ugt i64 %99, 0
br i1 %100, label %101, label %110
101: ; preds = %94
%102 = load i64, i64* %10
%103 = load i32, i32* %8
%104 = sext i32 %103 to i64
%105 = sub i64 %104, %102
%106 = trunc i64 %105 to i32
store i32 %106, i32* %8
%107 = load i64, i64* %10
%108 = load i8*, i8** %9
%109 = getelementptr inbounds i8, i8* %108, i64 %107
store i8* %109, i8** %9
br label %110
110: ; preds = %101, %94
br label %91
111: ; preds = %91
br label %112
112: ; preds = %111
br i1 true, label %74, label %113
113: ; preds = %112, %88
br label %114
114: ; preds = %113
%115 = load i32, i32* %4
%116 = load i32, i32* @SEEK_CUR
%117 = call i32 @lseek(i32 %115, i32 0, i32 %116)
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %117)
%119 = load i32, i32* %4
%120 = call i32 @close(i32 %119)
store i32 %120, i32* %6
%121 = load i32, i32* %6
%122 = icmp ne i32 %121, 0
br i1 %122, label %123, label %126
123: ; preds = %114
%124 = load i8*, i8** %2
%125 = call i32 @perror(i8* %124)
store i32 1, i32* %1
br label %157
126: ; preds = %114
%127 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
%128 = load i8*, i8** %2
%129 = call i32 @stat(i8* %128, %struct.stat* %7)
store i32 %129, i32* %6
%130 = load i32, i32* %6
%131 = icmp ne i32 %130, 0
br i1 %131, label %132, label %135
132: ; preds = %126
%133 = load i8*, i8** %2
%134 = call i32 @perror(i8* %133)
store i32 1, i32* %1
br label %157
135: ; preds = %126
%136 = load i8*, i8** %2
%137 = call i32 @unlink(i8* %136)
store i32 %137, i32* %6
%138 = load i32, i32* %6
%139 = icmp ne i32 %138, 0
br i1 %139, label %140, label %143
140: ; preds = %135
%141 = load i8*, i8** %2
%142 = call i32 @perror(i8* %141)
store i32 1, i32* %1
br label %157
143: ; preds = %135
%144 = load i8*, i8** %2
%145 = call i32 @stat(i8* %144, %struct.stat* %7)
store i32 %145, i32* %6
%146 = load i32, i32* %6
%147 = icmp ne i32 %146, -1
br i1 %147, label %152, label %148
148: ; preds = %143
%149 = load i64, i64* @errno
%150 = load i64, i64* @ENOENT
%151 = icmp ne i64 %149, %150
br i1 %151, label %152, label %155
152: ; preds = %148, %143
%153 = load i8*, i8** %2
%154 = call i32 @perror(i8* %153)
store i32 1, i32* %1
br label %157
155: ; preds = %148
%156 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
store i32 0, i32* %1
br label %157
157: ; preds = %155, %152, %140, %132, %123, %80, %70, %59, %47, %38, %27
%158 = load i32, i32* %1
ret i32 %158
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [11 x i8] c"rwtest.txt\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [26 x i8] c"Writing message to %s...\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.5 = [12 x i8] c"pos = %ld.\0A\00"
@SEEK_CUR = global i32 0
@.str.6 = [34 x i8] c"Done.\0AReading message from %s...\0A\00"
@O_RDONLY = global i32 0
@stdout = global i32 0
@.str.7 = [13 x i8] c"\0Apos = %ld.\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@str = [40 x i8] c"fatfs_unistd\0APress Enter to continue...\00"
@str.10 = [17 x i8] c"Write a message:\00"
@str.11 = [23 x i8] c"Done.\0ARemoving file...\00"
@str.12 = [6 x i8] c"Done.\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = alloca %struct.stat
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.stat* %2 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (...) @wait_enter()
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.10, i64 0, i64 0))
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %11, %10
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %89
19: ; preds = %0
%20 = call i32 @strlen(i8* %3)
%21 = call i32 @write(i32 %15, i8* %3, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %89
25: ; preds = %19
%26 = call i32 @fsync(i32 %15)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %89
30: ; preds = %25
%31 = load i32, i32* @SEEK_CUR
%32 = call i32 @lseek(i32 %15, i32 0, i32 %31)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = call i32 @close(i32 %15)
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %30
%37 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %89
38: ; preds = %30
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%40 = load i32, i32* @O_RDONLY
%41 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %46, %38
%43 = call i32 @read(i32 %41, i8* %3, i32 80)
switch i32 %43, label %44 [
i32 -1, label %56
i32 0, label %59
]
44: ; preds = %42
%45 = icmp sgt i32 %43, 0
br i1 %45, label %47, label %46
46: ; preds = %47, %44
br label %42
47: ; preds = %44, %47
%48 = phi i8* [ %53, %47 ], [ %3, %44 ]
%49 = phi i32 [ %54, %47 ], [ %43, %44 ]
%50 = load i32, i32* @stdout
%51 = call i64 @fwrite(i8* %48, i32 1, i32 %49, i32 %50)
%52 = trunc i64 %51 to i32
%53 = getelementptr i8, i8* %48, i64 %51
%54 = sub i32 %49, %52
%55 = icmp sgt i32 %54, 0
br i1 %55, label %47, label %46
56: ; preds = %42
%57 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%58 = call i32 @close(i32 %41)
br label %89
59: ; preds = %42
%60 = load i32, i32* @SEEK_CUR
%61 = call i32 @lseek(i32 %41, i32 0, i32 %60)
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %61)
%63 = call i32 @close(i32 %41)
%64 = icmp eq i32 %63, 0
br i1 %64, label %67, label %65
65: ; preds = %59
%66 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %89
67: ; preds = %59
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
%69 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%70 = icmp eq i32 %69, 0
br i1 %70, label %73, label %71
71: ; preds = %67
%72 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %89
73: ; preds = %67
%74 = call i32 @unlink(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%75 = icmp eq i32 %74, 0
br i1 %75, label %78, label %76
76: ; preds = %73
%77 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %89
78: ; preds = %73
%79 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %85
81: ; preds = %78
%82 = load i64, i64* @errno
%83 = load i64, i64* @ENOENT
%84 = icmp eq i64 %82, %83
br i1 %84, label %87, label %85
85: ; preds = %81, %78
%86 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %89
87: ; preds = %81
%88 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.12, i64 0, i64 0))
br label %89
89: ; preds = %56, %87, %85, %76, %71, %65, %36, %28, %23, %17
%90 = phi i32 [ 1, %17 ], [ 1, %23 ], [ 1, %28 ], [ 1, %36 ], [ 1, %65 ], [ 1, %71 ], [ 1, %76 ], [ 1, %85 ], [ 0, %87 ], [ 1, %56 ]
ret i32 %90
}