#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_RDONLY;
int S_IRUSR;
int S_IWUSR;
int errno;
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int, int, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
char *strerrno(int);
int system(char *);
int unlink(char *);
int test_unlink() {
  int ret;
  char name[256];
  char sys[512];
  errno = 0;
  ret = unlink("bar.fileio.test");
  printf("unlink 1: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  sprintf(name, "%s/%s", "dir2.fileio.test", "foo.fileio.test");
  errno = 0;
  ret = open(name, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR);
  if (ret >= 0) {
    sprintf(sys, "chmod -w %s", "dir2.fileio.test");
    ret = system(sys);
    if (!ret) {
      errno = 0;
      ret = unlink(name);
      printf("unlink 2: ret = %d, errno = %d %s\n", ret, errno,
             strerrno(errno));
    } else
      printf("unlink 2: ret = %d chmod failed\n", ret, errno);
  } else
    printf("unlink 2: ret = %d, errno = %d\n", ret, errno);
  errno = 0;
  ret = unlink("nofoo.fileio.test");
  printf("unlink 3: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
}


x86 assembly (clang, -O0):
<test_unlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x330,%rsp
mov    0x2e56(%rip),%rax
movl   $0x0,(%rax)
lea    0xe81(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x318(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x314(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x318(%rbp),%esi
mov    -0x314(%rbp),%edx
mov    %rax,%rcx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe5b(%rip),%rsi
lea    0xe5a(%rip),%rdx
lea    0xed6(%rip),%rcx
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    0x2dd4(%rip),%rax
movl   $0x0,(%rax)
lea    -0x110(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
mov    0x2db5(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     12ef <test_unlink+0x18f>
lea    -0x310(%rbp),%rdi
lea    0xe05(%rip),%rsi
lea    0xded(%rip),%rdx
mov    $0x0,%al
call   1080 <sprintf@plt>
lea    -0x310(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    12d0 <test_unlink+0x170>
mov    0x2d55(%rip),%rax
movl   $0x0,(%rax)
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x320(%rbp)
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x31c(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x320(%rbp),%esi
mov    -0x31c(%rbp),%edx
mov    %rax,%rcx
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12ea <test_unlink+0x18a>
mov    -0x8(%rbp),%esi
mov    0x2cee(%rip),%rax
mov    (%rax),%edx
lea    0xd96(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1309 <test_unlink+0x1a9>
mov    -0x8(%rbp),%esi
mov    0x2ccf(%rip),%rax
mov    (%rax),%edx
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2cb8(%rip),%rax
movl   $0x0,(%rax)
lea    0xd9d(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x328(%rbp)
mov    0x2c93(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x324(%rbp)
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x328(%rbp),%esi
mov    -0x324(%rbp),%edx
mov    %rax,%rcx
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x330,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_unlink>:
push   %rbp
push   %r14
push   %rbx
sub    $0x300,%rsp
mov    0x2e56(%rip),%r14
movl   $0x0,(%r14)
lea    0xe80(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,%ebx
mov    (%r14),%ebp
mov    %ebp,%edi
call   1040 <strerrno@plt>
lea    0xe78(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe86(%rip),%rsi
lea    0xe85(%rip),%rdx
lea    0xf01(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <sprintf@plt>
movl   $0x0,(%r14)
mov    0x2e22(%rip),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd8(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1237 <test_unlink+0xd7>
lea    0xe44(%rip),%rsi
lea    0xe2c(%rip),%rdx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
test   %eax,%eax
je     124c <test_unlink+0xec>
mov    (%r14),%edx
lea    0xe44(%rip),%rdi
jmp    1241 <test_unlink+0xe1>
mov    (%r14),%edx
lea    0xe59(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    127c <test_unlink+0x11c>
movl   $0x0,(%r14)
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %eax,%ebx
mov    (%r14),%ebp
mov    %ebp,%edi
call   1040 <strerrno@plt>
lea    0xde8(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
movl   $0x0,(%r14)
lea    0xe30(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,%ebx
mov    (%r14),%ebp
mov    %ebp,%edi
call   1040 <strerrno@plt>
lea    0xe2a(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
add    $0x300,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <printf@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [16 x i8] c"bar.fileio.test\00"
@.str.1 = [35 x i8] c"unlink 1: ret = %d, errno = %d %s\0A\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [17 x i8] c"dir2.fileio.test\00"
@.str.4 = [16 x i8] c"foo.fileio.test\00"
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.5 = [12 x i8] c"chmod -w %s\00"
@.str.6 = [35 x i8] c"unlink 2: ret = %d, errno = %d %s\0A\00"
@.str.7 = [33 x i8] c"unlink 2: ret = %d chmod failed\0A\00"
@.str.8 = [32 x i8] c"unlink 2: ret = %d, errno = %d\0A\00"
@.str.9 = [18 x i8] c"nofoo.fileio.test\00"
@.str.10 = [35 x i8] c"unlink 3: ret = %d, errno = %d %s\0A\00"
define i32 @test_unlink() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [256 x i8]
%4 = alloca [512 x i8]
store i32 0, i32* @errno
%5 = call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 %5, i32* %2
%6 = load i32, i32* %2
%7 = load i32, i32* @errno
%8 = load i32, i32* @errno
%9 = call i8* @strerrno(i32 %8)
%10 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %7, i8* %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%12 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %11, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* @errno
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_RDONLY
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %13, i32 %16, i32 %19)
store i32 %20, i32* %2
%21 = load i32, i32* %2
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %43
23: ; preds = %0
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%25 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%27 = call i32 @system(i8* %26)
store i32 %27, i32* %2
%28 = load i32, i32* %2
%29 = icmp ne i32 %28, 0
br i1 %29, label %38, label %30
30: ; preds = %23
store i32 0, i32* @errno
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%32 = call i32 @unlink(i8* %31)
store i32 %32, i32* %2
%33 = load i32, i32* %2
%34 = load i32, i32* @errno
%35 = load i32, i32* @errno
%36 = call i8* @strerrno(i32 %35)
%37 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %33, i32 %34, i8* %36)
br label %42
38: ; preds = %23
%39 = load i32, i32* %2
%40 = load i32, i32* @errno
%41 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i32 %39, i32 %40)
br label %42
42: ; preds = %38, %30
br label %47
43: ; preds = %0
%44 = load i32, i32* %2
%45 = load i32, i32* @errno
%46 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %44, i32 %45)
br label %47
47: ; preds = %43, %42
store i32 0, i32* @errno
%48 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
store i32 %48, i32* %2
%49 = load i32, i32* %2
%50 = load i32, i32* @errno
%51 = load i32, i32* @errno
%52 = call i8* @strerrno(i32 %51)
%53 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %49, i32 %50, i8* %52)
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [16 x i8] c"bar.fileio.test\00"
@.str.1 = [35 x i8] c"unlink 1: ret = %d, errno = %d %s\0A\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [17 x i8] c"dir2.fileio.test\00"
@.str.4 = [16 x i8] c"foo.fileio.test\00"
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.5 = [12 x i8] c"chmod -w %s\00"
@.str.6 = [35 x i8] c"unlink 2: ret = %d, errno = %d %s\0A\00"
@.str.7 = [33 x i8] c"unlink 2: ret = %d chmod failed\0A\00"
@.str.8 = [32 x i8] c"unlink 2: ret = %d, errno = %d\0A\00"
@.str.9 = [18 x i8] c"nofoo.fileio.test\00"
@.str.10 = [35 x i8] c"unlink 3: ret = %d, errno = %d %s\0A\00"
define i32 @test_unlink() {
%1 = alloca [256 x i8]
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
store i32 0, i32* @errno
%5 = tail call i32 @unlink(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @errno
%7 = tail call i8* @strerrno(i32 %6)
%8 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i32 %5, i32 %6, i8* %7)
%9 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* @errno
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_RDONLY
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %14, %13
%16 = call i32 @open(i8* %3, i32 %12, i32 %15)
%17 = icmp sgt i32 %16, -1
br i1 %17, label %18, label %30
18: ; preds = %0
%19 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 @system(i8* %4)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %18
store i32 0, i32* @errno
%23 = call i32 @unlink(i8* %3)
%24 = load i32, i32* @errno
%25 = tail call i8* @strerrno(i32 %24)
%26 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %23, i32 %24, i8* %25)
br label %33
27: ; preds = %18
%28 = load i32, i32* @errno
%29 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0), i32 %20, i32 %28)
br label %33
30: ; preds = %0
%31 = load i32, i32* @errno
%32 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %16, i32 %31)
br label %33
33: ; preds = %22, %27, %30
store i32 0, i32* @errno
%34 = tail call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%35 = load i32, i32* @errno
%36 = tail call i8* @strerrno(i32 %35)
%37 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %34, i32 %35, i8* %36)
ret i32 undef
}