#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 */

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_WRONLY;
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fsync(int);
int open(char *, int, int);
int rename(char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  fd = open("dfs1.txt", O_CREAT | O_WRONLY | O_APPEND, 0644);
  write(fd, "0123456789", 10);
  if (rename("dfs1.txt", "dfs1_bak.txt") == -1) {
    fprintf(stderr, "rename() failed: %s\n", strerror(errno));
    return 1;
  }
  if (fsync(fd) == -1) {
    fprintf(stderr, "fsync() failed: %s\n", strerror(errno));
    return 1;
  }
  if (unlink("dfs1_bak.txt") == -1) {
    fprintf(stderr, "unlink() failed: %s\n", strerror(errno));
    return 1;
  }
  if (close(fd) == -1) {
    fprintf(stderr, "close() failed: %s\n", strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
lea    0xe48(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xe3a(%rip),%rsi
mov    $0xa,%edx
call   1040 <write@plt>
lea    0xe20(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   1090 <rename@plt>
cmp    $0xffffffff,%eax
jne    122d <main+0xad>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe05(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1313 <main+0x193>
mov    -0x14(%rbp),%edi
call   1070 <fsync@plt>
cmp    $0xffffffff,%eax
jne    1276 <main+0xf6>
mov    0x2d93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xdd1(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1313 <main+0x193>
lea    0xd97(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    12c3 <main+0x143>
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xd98(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1313 <main+0x193>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    130c <main+0x18c>
mov    0x2cfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd64(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1313 <main+0x193>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e6d(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
or     (%rax),%esi
lea    0xe5a(%rip),%r14
mov    %r14,%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%ebx
lea    0xe4d(%rip),%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1040 <write@plt>
lea    0xe45(%rip),%rsi
mov    %r14,%rdi
call   1090 <rename@plt>
cmp    $0xffffffff,%eax
je     1212 <main+0x92>
mov    %ebx,%edi
call   1070 <fsync@plt>
cmp    $0xffffffff,%eax
je     121b <main+0x9b>
lea    0xe25(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
je     1224 <main+0xa4>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    1254 <main+0xd4>
lea    0xe4f(%rip),%rbx
jmp    122b <main+0xab>
lea    0xe08(%rip),%rbx
jmp    122b <main+0xab>
lea    0xe14(%rip),%rbx
jmp    122b <main+0xab>
lea    0xe1f(%rip),%rbx
mov    0x2da6(%rip),%rax
mov    (%rax),%ebp
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"dfs1.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [11 x i8] c"0123456789\00"
@.str.2 = [13 x i8] c"dfs1_bak.txt\00"
@stderr = global i32 0
@.str.3 = [21 x i8] c"rename() failed: %s\0A\00"
@errno = global i32 0
@.str.4 = [20 x i8] c"fsync() failed: %s\0A\00"
@.str.5 = [21 x i8] c"unlink() failed: %s\0A\00"
@.str.6 = [20 x i8] c"close() failed: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %7, %8
%10 = load i32, i32* @O_APPEND
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11, i32 420)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 10)
%15 = call i32 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %49
22: ; preds = %2
%23 = load i32, i32* %6
%24 = call i32 @fsync(i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %29)
store i32 1, i32* %3
br label %49
31: ; preds = %22
%32 = call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %37)
store i32 1, i32* %3
br label %49
39: ; preds = %31
%40 = load i32, i32* %6
%41 = call i32 @close(i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %46)
store i32 1, i32* %3
br label %49
48: ; preds = %39
store i32 0, i32* %3
br label %49
49: ; preds = %48, %43, %34, %26, %17
%50 = load i32, i32* %3
ret i32 %50
}

LLVM IR (-O2):
@.str = [9 x i8] c"dfs1.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@.str.1 = [11 x i8] c"0123456789\00"
@.str.2 = [13 x i8] c"dfs1_bak.txt\00"
@stderr = global i32 0
@.str.3 = [21 x i8] c"rename() failed: %s\0A\00"
@errno = global i32 0
@.str.4 = [20 x i8] c"fsync() failed: %s\0A\00"
@.str.5 = [21 x i8] c"unlink() failed: %s\0A\00"
@.str.6 = [20 x i8] c"close() failed: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %4, %3
%6 = load i32, i32* @O_APPEND
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 420)
%9 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 10)
%10 = tail call i32 @rename(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32 %10, -1
br i1 %11, label %21, label %12
12: ; preds = %2
%13 = tail call i32 @fsync(i32 %8)
%14 = icmp eq i32 %13, -1
br i1 %14, label %21, label %15
15: ; preds = %12
%16 = tail call i32 @unlink(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32 %16, -1
br i1 %17, label %21, label %18
18: ; preds = %15
%19 = tail call i32 @close(i32 %8)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %27
21: ; preds = %18, %15, %12, %2
%22 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), %12 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), %15 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), %18 ]
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i8* @strerror(i32 %24)
%26 = tail call i32 @fprintf(i32 %23, i8* %22, i8* %25)
br label %27
27: ; preds = %21, %18
%28 = phi i32 [ 0, %18 ], [ 1, %21 ]
ret i32 %28
}