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

/* Variables and functions */
#define EINVAL 130
#define EIO 129
#define ENOENT 128
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, char *);
int errno;
int /*<<< orphan*/ finish();
int /*<<< orphan*/ killdir();
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ startup();
int /*<<< orphan*/ warnx(char *);
int /*<<< orphan*/ write(int, char *, int);
void test4(void) {
  char buf[4096];
  int fd;
  printf("Checking if creating files works after rmdir...\n");
  startup();
  killdir();
  fd = open("newfile", O_WRONLY | O_CREAT | O_TRUNC, 0664);
  if (fd < 0) {
    switch (errno) {
    case EINVAL:
    case EIO:
    case ENOENT:
      break;
    default:
      err(1, "%s", buf);
      break;
    }
  } else {
    warnx("newfile: creating files after rmdir works");
    warnx("(this is only ok if the space gets reclaimed)");
    memset(buf, 'J', sizeof(buf));
    write(fd, buf, sizeof(buf));
    write(fd, buf, sizeof(buf));
    write(fd, buf, sizeof(buf));
    write(fd, buf, sizeof(buf));
    close(fd);
  }
  finish();
}


x86 assembly (clang, -O0):
<test4>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1010,%rsp
lea    0xe4e(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   10b0 <startup@plt>
mov    $0x0,%al
call   1090 <killdir@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2e23(%rip),%rax
or     (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
lea    0xe4a(%rip),%rdi
mov    $0x1b4,%edx
call   10c0 <open@plt>
mov    %eax,-0x1004(%rbp)
cmpl   $0x0,-0x1004(%rbp)
jge    1240 <test4+0xa0>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x2,%eax
ja     1223 <test4+0x83>
jmp    121e <test4+0x7e>
jmp    123b <test4+0x9b>
lea    -0x1000(%rbp),%rdx
mov    $0x1,%edi
lea    0xe03(%rip),%rsi
call   10a0 <err@plt>
jmp    12d5 <test4+0x135>
lea    0xdf5(%rip),%rdi
call   1060 <warnx@plt>
lea    0xe13(%rip),%rdi
call   1060 <warnx@plt>
lea    -0x1000(%rbp),%rdi
mov    $0x4a,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    -0x1004(%rbp),%edi
lea    -0x1000(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    -0x1004(%rbp),%edi
lea    -0x1000(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    -0x1004(%rbp),%edi
lea    -0x1000(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    -0x1004(%rbp),%edi
lea    -0x1000(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    -0x1004(%rbp),%edi
call   1080 <close@plt>
mov    $0x0,%al
call   1030 <finish@plt>
add    $0x1010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test4>:
push   %r14
push   %rbx
sub    $0x1008,%rsp
lea    0xeb2(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10b0 <startup@plt>
xor    %eax,%eax
call   1090 <killdir@plt>
mov    0x2df5(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e13(%rip),%rax
or     (%rax),%esi
lea    0xe1a(%rip),%rdi
mov    $0x1b4,%edx
call   10c0 <open@plt>
test   %eax,%eax
js     1268 <test4+0xc8>
mov    %eax,%ebx
lea    0xe0e(%rip),%rdi
call   1060 <warnx@plt>
lea    0xe2c(%rip),%rdi
call   1060 <warnx@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x4a,%esi
mov    $0x1000,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <write@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    128d <test4+0xed>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
cmp    $0x3,%eax
jb     128d <test4+0xed>
lea    0xd88(%rip),%rsi
mov    %rsp,%rdx
mov    $0x1,%edi
call   10a0 <err@plt>
xor    %eax,%eax
call   1030 <finish@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [49 x i8] c"Checking if creating files works after rmdir...\0A\00"
@.str.1 = [8 x i8] c"newfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [42 x i8] c"newfile: creating files after rmdir works\00"
@.str.4 = [46 x i8] c"(this is only ok if the space gets reclaimed)\00"
define void @test4() {
%1 = alloca [4096 x i8]
%2 = alloca i32
%3 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @startup()
%5 = call i32 (...) @killdir()
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 436)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %0
%15 = load i32, i32* @errno
switch i32 %15, label %17 [
i32 130, label %16
i32 129, label %16
i32 128, label %16
]
16: ; preds = %14, %14, %14
br label %20
17: ; preds = %14
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%19 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %16
br label %40
21: ; preds = %0
%22 = call i32 @warnx(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @warnx(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%25 = call i32 @memset(i8* %24, i8 signext 74, i32 4096)
%26 = load i32, i32* %2
%27 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%28 = call i32 @write(i32 %26, i8* %27, i32 4096)
%29 = load i32, i32* %2
%30 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%31 = call i32 @write(i32 %29, i8* %30, i32 4096)
%32 = load i32, i32* %2
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%34 = call i32 @write(i32 %32, i8* %33, i32 4096)
%35 = load i32, i32* %2
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%37 = call i32 @write(i32 %35, i8* %36, i32 4096)
%38 = load i32, i32* %2
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %21, %20
%41 = call i32 (...) @finish()
ret void
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"newfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [42 x i8] c"newfile: creating files after rmdir works\00"
@.str.4 = [46 x i8] c"(this is only ok if the space gets reclaimed)\00"
@str = [48 x i8] c"Checking if creating files works after rmdir...\00"
define void @test4() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @startup()
%5 = tail call i32 (...) @killdir()
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %10, i32 436)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %0
%14 = load i32, i32* @errno
%15 = add i32 %14, -128
%16 = icmp ult i32 %15, 3
br i1 %16, label %28, label %17
17: ; preds = %13
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %28
19: ; preds = %0
%20 = tail call i32 @warnx(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
%21 = tail call i32 @warnx(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
%22 = call i32 @memset(i8* %2, i8 signext 74, i32 4096)
%23 = call i32 @write(i32 %11, i8* %2, i32 4096)
%24 = call i32 @write(i32 %11, i8* %2, i32 4096)
%25 = call i32 @write(i32 %11, i8* %2, i32 4096)
%26 = call i32 @write(i32 %11, i8* %2, i32 4096)
%27 = call i32 @close(i32 %11)
br label %28
28: ; preds = %13, %17, %19
%29 = call i32 (...) @finish()
ret void
}