#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_WRONLY;
int close(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ system(char *);
int unlink(char *);
int write(int, char *, int);
int main(void) {
  char tmp_file[] = "XXXXXX";
  char buf[8 * 1024] = {0};
  int fd = 0;
  int rval = 0;
  printf("This program demostrates system removes file as soon as"
         "close() was call atfer unlink()\n");
  printf("We do not have new file: XXXXXX at beginning\n");
  fd = open(tmp_file, O_CREAT | O_WRONLY);
  if (fd == -1) {
    perror("open: ");
  }
  printf("\nCreate new file: XXXXXX\n");
  system("ls");
  printf("File space in K\n");
  system("df `pwd`");
  rval = unlink(tmp_file);
  if (rval == -1) {
    perror("unlink: ");
  }
  printf("\nAfter unlink, we can still manage file: XXXXXX\n");
  system("ls");
  rval = write(fd, buf, 8 * 1024);
  if (rval == -1) {
    perror("write: ");
  }
  printf("\nWrite 8k, disk space in K now increased\n");
  system("df `pwd`");
  rval = close(fd);
  if (rval == -1) {
    perror("close: ");
  }
  printf("\nAfter close() file: XXXXXX is removed\n");
  system("ls");
  printf("File space in K now\n");
  system("df `pwd`");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2020,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe68(%rip),%eax
mov    %eax,-0xb(%rbp)
mov    0xe62(%rip),%ax
mov    %ax,-0x7(%rbp)
mov    0xe5a(%rip),%al
mov    %al,-0x5(%rbp)
lea    -0x2010(%rbp),%rdi
xor    %esi,%esi
mov    $0x2000,%edx
call   1070 <memset@plt>
movl   $0x0,-0x2014(%rbp)
movl   $0x0,-0x2018(%rbp)
lea    0xe2a(%rip),%rdi
call   1060 <printf@plt>
lea    0xe76(%rip),%rdi
call   1060 <printf@plt>
lea    -0xb(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2dce(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x2014(%rbp)
cmpl   $0xffffffff,-0x2014(%rbp)
jne    1228 <main+0xa8>
lea    0xe6a(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe65(%rip),%rdi
call   1060 <printf@plt>
lea    0xe73(%rip),%rdi
call   1050 <system@plt>
lea    0xe6a(%rip),%rdi
call   1060 <printf@plt>
lea    0xe6f(%rip),%rdi
call   1050 <system@plt>
lea    -0xb(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x2018(%rbp)
cmpl   $0xffffffff,-0x2018(%rbp)
jne    1280 <main+0x100>
lea    0xe50(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe4d(%rip),%rdi
call   1060 <printf@plt>
lea    0xe1b(%rip),%rdi
call   1050 <system@plt>
mov    -0x2014(%rbp),%edi
lea    -0x2010(%rbp),%rsi
mov    $0x2000,%edx
call   1040 <write@plt>
mov    %eax,-0x2018(%rbp)
cmpl   $0xffffffff,-0x2018(%rbp)
jne    12ce <main+0x14e>
lea    0xe3c(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe38(%rip),%rdi
call   1060 <printf@plt>
lea    0xde1(%rip),%rdi
call   1050 <system@plt>
mov    -0x2014(%rbp),%edi
call   1080 <close@plt>
mov    %eax,-0x2018(%rbp)
cmpl   $0xffffffff,-0x2018(%rbp)
jne    1310 <main+0x190>
lea    0xe2c(%rip),%rdi
call   10a0 <perror@plt>
lea    0xe28(%rip),%rdi
call   1060 <printf@plt>
lea    0xd8b(%rip),%rdi
call   1050 <system@plt>
lea    0xe38(%rip),%rdi
call   1060 <printf@plt>
lea    0xd87(%rip),%rdi
call   1050 <system@plt>
xor    %eax,%eax
add    $0x2020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x2010,%rsp
movl   $0x585858,0xb(%rsp)
movl   $0x58585858,0x8(%rsp)
58
lea    0x10(%rsp),%rdi
mov    $0x2000,%edx
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xe7c(%rip),%rdi
call   1040 <puts@plt>
lea    0xec7(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e30(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x8(%rsp),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12bc <main+0x13c>
lea    0xec1(%rip),%rdi
call   1040 <puts@plt>
lea    0xe0c(%rip),%rdi
call   1060 <system@plt>
lea    0xec2(%rip),%rdi
call   1040 <puts@plt>
lea    0xdf7(%rip),%rdi
call   1060 <system@plt>
lea    0x8(%rsp),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
je     12cd <main+0x14d>
lea    0xea7(%rip),%rdi
call   1040 <puts@plt>
lea    0xdc9(%rip),%rdi
call   1060 <system@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x2000,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     12de <main+0x15e>
lea    0xea5(%rip),%rdi
call   1040 <puts@plt>
lea    0xd9a(%rip),%rdi
call   1060 <system@plt>
mov    %ebx,%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
je     12ef <main+0x16f>
lea    0xeaa(%rip),%rdi
call   1040 <puts@plt>
lea    0xd73(%rip),%rdi
call   1060 <system@plt>
lea    0xeb9(%rip),%rdi
call   1040 <puts@plt>
lea    0xd5e(%rip),%rdi
call   1060 <system@plt>
xor    %eax,%eax
add    $0x2010,%rsp
pop    %rbx
ret
lea    0xd3d(%rip),%rdi
call   10a0 <perror@plt>
jmp    11e8 <main+0x68>
lea    0xd3f(%rip),%rdi
call   10a0 <perror@plt>
jmp    122b <main+0xab>
lea    0xd37(%rip),%rdi
call   10a0 <perror@plt>
jmp    125d <main+0xdd>
lea    0xd2e(%rip),%rdi
call   10a0 <perror@plt>
jmp    1281 <main+0x101>

LLVM IR (-O0):
@__const.main.tmp_file = [7 x i8] c"XXXXXX\00"
@.str = [88 x i8] c"This program demostrates system removes file as soon asclose() was call atfer unlink()\0A\00"
@.str.1 = [46 x i8] c"We do not have new file: XXXXXX at beginning\0A\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [7 x i8] c"open: \00"
@.str.3 = [26 x i8] c"\0ACreate new file: XXXXXX\0A\00"
@.str.4 = [3 x i8] c"ls\00"
@.str.5 = [17 x i8] c"File space in K\0A\00"
@.str.6 = [9 x i8] c"df `pwd`\00"
@.str.7 = [9 x i8] c"unlink: \00"
@.str.8 = [49 x i8] c"\0AAfter unlink, we can still manage file: XXXXXX\0A\00"
@.str.9 = [8 x i8] c"write: \00"
@.str.10 = [42 x i8] c"\0AWrite 8k, disk space in K now increased\0A\00"
@.str.11 = [8 x i8] c"close: \00"
@.str.12 = [40 x i8] c"\0AAfter close() file: XXXXXX is removed\0A\00"
@.str.13 = [21 x i8] c"File space in K now\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [7 x i8]
%3 = alloca [8192 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [7 x i8]* %2 to i8*
%7 = bitcast [8192 x i8]* %3 to i8*
store i32 0, i32* %4
store i32 0, i32* %5
%8 = call i32 @printf(i8* getelementptr inbounds ([88 x i8], [88 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
%10 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %10, i32 %13)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %0
%20 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
%23 = call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%24 = getelementptr inbounds [7 x i8], [7 x i8]* %2, i64 0, i64 0
%25 = call i32 @unlink(i8* %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %19
%29 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %30
30: ; preds = %28, %19
%31 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.8, i64 0, i64 0))
%32 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%33 = load i32, i32* %4
%34 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%35 = call i32 @write(i32 %33, i8* %34, i32 8192)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %40
40: ; preds = %38, %30
%41 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.10, i64 0, i64 0))
%42 = call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
store i32 %44, i32* %5
%45 = load i32, i32* %5
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0))
br label %49
49: ; preds = %47, %40
%50 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.12, i64 0, i64 0))
%51 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%52 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.13, i64 0, i64 0))
%53 = call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@__const.main.tmp_file = [7 x i8] c"XXXXXX\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [7 x i8] c"open: \00"
@.str.4 = [3 x i8] c"ls\00"
@.str.6 = [9 x i8] c"df `pwd`\00"
@.str.7 = [9 x i8] c"unlink: \00"
@.str.9 = [8 x i8] c"write: \00"
@.str.11 = [8 x i8] c"close: \00"
@str = [87 x i8] c"This program demostrates system removes file as soon asclose() was call atfer unlink()\00"
@str.14 = [45 x i8] c"We do not have new file: XXXXXX at beginning\00"
@str.15 = [25 x i8] c"\0ACreate new file: XXXXXX\00"
@str.16 = [16 x i8] c"File space in K\00"
@str.17 = [48 x i8] c"\0AAfter unlink, we can still manage file: XXXXXX\00"
@str.18 = [41 x i8] c"\0AWrite 8k, disk space in K now increased\00"
@str.19 = [39 x i8] c"\0AAfter close() file: XXXXXX is removed\00"
@str.20 = [20 x i8] c"File space in K now\00"
define i32 @main() {
%1 = alloca [7 x i8]
%2 = alloca [8192 x i8]
%3 = getelementptr inbounds [7 x i8], [7 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %2, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([87 x i8], [87 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @str.14, i64 0, i64 0))
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_WRONLY
%9 = or i32 %8, %7
%10 = call i32 @open(i8* %3, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.15, i64 0, i64 0))
%16 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.16, i64 0, i64 0))
%18 = tail call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%19 = call i32 @unlink(i8* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %23
23: ; preds = %21, %14
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.17, i64 0, i64 0))
%25 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @write(i32 %10, i8* %4, i32 8192)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.18, i64 0, i64 0))
%32 = tail call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%33 = tail call i32 @close(i32 %10)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.11, i64 0, i64 0))
br label %37
37: ; preds = %35, %30
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.19, i64 0, i64 0))
%39 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.20, i64 0, i64 0))
%41 = tail call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}