#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_CREAT;
int O_RDWR;
int /*<<< orphan*/ errno;
int fsync(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ system(char *);
int unlink(char *);
scalar_t__ write(int, char *, int);
int main(int argc, char **argv) {
  int ret = 0;
  int fd = 0;
  char *cmd = argv[1];
  printf("cmd is: %s\n", cmd);
  fd = open("a.txt", O_CREAT | O_RDWR, 0644);
  if (fd < 0)
    printf("open failed: %s\n", strerror(errno));
  ret = unlink("a.txt");
  if (ret < 0)
    printf("unlink failed: %s\n", strerror(errno));
  if (write(fd, "abc", 3) < 0)
    printf("Not able to print %s\n", strerror(errno));
  system(cmd);
  sleep(1);
  ret = fsync(fd);
  if (ret < 0)
    printf("Not able to fsync %s\n", strerror(errno));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe45(%rip),%rdi
call   1060 <printf@plt>
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
or     (%rax),%esi
lea    0xe33(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    120d <main+0x8d>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe0a(%rip),%rdi
call   1060 <printf@plt>
lea    0xdf8(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1243 <main+0xc3>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xde5(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xde9(%rip),%rsi
mov    $0x3,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    127e <main+0xfe>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xdc1(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x18(%rbp),%edi
call   1070 <fsync@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12c3 <main+0x143>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd92(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rsi),%rbx
lea    0xe72(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    0x2e5b(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5d(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11dc <main+0x5c>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe3e(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
lea    0xe29(%rip),%rdi
call   1030 <unlink@plt>
test   %eax,%eax
jns    1209 <main+0x89>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe22(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
lea    0xe26(%rip),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
call   1040 <write@plt>
test   %rax,%rax
jns    123e <main+0xbe>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe04(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %ebp,%edi
call   1070 <fsync@plt>
test   %eax,%eax
jns    1278 <main+0xf8>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xde0(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [12 x i8] c"cmd is: %s\0A\00"
@.str.1 = [6 x i8] c"a.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.2 = [17 x i8] c"open failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [19 x i8] c"unlink failed: %s\0A\00"
@.str.4 = [4 x i8] c"abc\00"
@.str.5 = [22 x i8] c"Not able to print %s\0A\00"
@.str.6 = [22 x i8] c"Not able to fsync %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 420)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %11, %2
%16 = tail call i32 @unlink(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %18, %15
%23 = tail call i64 @write(i32 %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 3)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %27)
br label %29
29: ; preds = %25, %22
%30 = tail call i32 @system(i8* %4)
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 @fsync(i32 %9)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %34, %29
ret i32 0
}