#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 *, ...);
int /*<<< orphan*/ stop();
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));
  stop();
  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, errno= %d\n", ret, errno);
  } else
    printf("unlink 2: ret = %d, errno = %d\n", ret, errno);
  stop();
  errno = 0;
  ret = unlink("nofoo.fileio.test");
  printf("unlink 3: ret = %d, errno = %d %s\n", ret, errno, strerrno(errno));
  stop();
}


x86 assembly (clang, -O0):
<test_unlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x330,%rsp
mov    0x2e46(%rip),%rax
movl   $0x0,(%rax)
lea    0xe71(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x318(%rbp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x314(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x318(%rbp),%esi
mov    -0x314(%rbp),%edx
mov    %rax,%rcx
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <stop@plt>
lea    -0x110(%rbp),%rdi
lea    0xe44(%rip),%rsi
lea    0xe43(%rip),%rdx
lea    0xeca(%rip),%rcx
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    0x2dbd(%rip),%rax
movl   $0x0,(%rax)
lea    -0x110(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2db8(%rip),%rax
or     (%rax),%esi
mov    0x2d8f(%rip),%rax
mov    (%rax),%edx
mov    0x2d9e(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     1306 <test_unlink+0x196>
lea    -0x310(%rbp),%rdi
lea    0xdee(%rip),%rsi
lea    0xdd6(%rip),%rdx
mov    $0x0,%al
call   1090 <sprintf@plt>
lea    -0x310(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    12e7 <test_unlink+0x177>
mov    0x2d3e(%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    0x2d19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x31c(%rbp)
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x320(%rbp),%esi
mov    -0x31c(%rbp),%edx
mov    %rax,%rcx
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1301 <test_unlink+0x191>
mov    -0x8(%rbp),%esi
mov    0x2cd7(%rip),%rax
mov    (%rax),%edx
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    1320 <test_unlink+0x1b0>
mov    -0x8(%rbp),%esi
mov    0x2cb8(%rip),%rax
mov    (%rax),%edx
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <stop@plt>
mov    0x2c9a(%rip),%rax
movl   $0x0,(%rax)
lea    0xd8a(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x328(%rbp)
mov    0x2c75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x324(%rbp)
mov    0x2c66(%rip),%rax
mov    (%rax),%edi
call   1040 <strerrno@plt>
mov    -0x328(%rbp),%esi
mov    -0x324(%rbp),%edx
mov    %rax,%rcx
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <stop@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    0x2e46(%rip),%r14
movl   $0x0,(%r14)
lea    0xe70(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,%ebx
mov    (%r14),%ebp
mov    %ebp,%edi
call   1040 <strerrno@plt>
lea    0xe68(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1070 <stop@plt>
lea    0xe6f(%rip),%rsi
lea    0xe6e(%rip),%rdx
lea    0xef5(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <sprintf@plt>
movl   $0x0,(%r14)
mov    0x2e0b(%rip),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc1(%rip),%rax
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     124e <test_unlink+0xde>
lea    0xe2d(%rip),%rsi
lea    0xe15(%rip),%rdx
lea    0x100(%rsp),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <system@plt>
test   %eax,%eax
je     1263 <test_unlink+0xf3>
mov    (%r14),%edx
lea    0xe2d(%rip),%rdi
jmp    1258 <test_unlink+0xe8>
mov    (%r14),%edx
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1293 <test_unlink+0x123>
movl   $0x0,(%r14)
mov    %rsp,%rdi
call   1030 <unlink@plt>
mov    %eax,%ebx
mov    (%r14),%ebp
mov    %ebp,%edi
call   1040 <strerrno@plt>
lea    0xdd1(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1070 <stop@plt>
movl   $0x0,(%r14)
lea    0xe1d(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,%ebx
mov    (%r14),%ebp
mov    %ebp,%edi
call   1040 <strerrno@plt>
lea    0xe17(%rip),%rdi
mov    %ebx,%esi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x300,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <stop@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 = [44 x i8] c"unlink 2: ret = %d chmod failed, errno= %d\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 = call i32 (...) @stop()
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%13 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %12, 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
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_RDONLY
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %14, i32 %17, i32 %20)
store i32 %21, i32* %2
%22 = load i32, i32* %2
%23 = icmp sge i32 %22, 0
br i1 %23, label %24, label %44
24: ; preds = %0
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%26 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %25, 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))
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%28 = call i32 @system(i8* %27)
store i32 %28, i32* %2
%29 = load i32, i32* %2
%30 = icmp ne i32 %29, 0
br i1 %30, label %39, label %31
31: ; preds = %24
store i32 0, i32* @errno
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%33 = call i32 @unlink(i8* %32)
store i32 %33, i32* %2
%34 = load i32, i32* %2
%35 = load i32, i32* @errno
%36 = load i32, i32* @errno
%37 = call i8* @strerrno(i32 %36)
%38 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %34, i32 %35, i8* %37)
br label %43
39: ; preds = %24
%40 = load i32, i32* %2
%41 = load i32, i32* @errno
%42 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %40, i32 %41)
br label %43
43: ; preds = %39, %31
br label %48
44: ; preds = %0
%45 = load i32, i32* %2
%46 = load i32, i32* @errno
%47 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %45, i32 %46)
br label %48
48: ; preds = %44, %43
%49 = call i32 (...) @stop()
store i32 0, i32* @errno
%50 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
store i32 %50, i32* %2
%51 = load i32, i32* %2
%52 = load i32, i32* @errno
%53 = load i32, i32* @errno
%54 = call i8* @strerrno(i32 %53)
%55 = call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %51, i32 %52, i8* %54)
%56 = call i32 (...) @stop()
%57 = load i32, i32* %1
ret i32 %57
}

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 = [44 x i8] c"unlink 2: ret = %d chmod failed, errno= %d\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 = tail call i32 (...) @stop()
%10 = 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
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDONLY
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = call i32 @open(i8* %3, i32 %13, i32 %16)
%18 = icmp sgt i32 %17, -1
br i1 %18, label %19, label %31
19: ; preds = %0
%20 = 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))
%21 = call i32 @system(i8* %4)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
store i32 0, i32* @errno
%24 = call i32 @unlink(i8* %3)
%25 = load i32, i32* @errno
%26 = tail call i8* @strerrno(i32 %25)
%27 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i32 %24, i32 %25, i8* %26)
br label %34
28: ; preds = %19
%29 = load i32, i32* @errno
%30 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i32 %21, i32 %29)
br label %34
31: ; preds = %0
%32 = load i32, i32* @errno
%33 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i32 %17, i32 %32)
br label %34
34: ; preds = %23, %28, %31
%35 = tail call i32 (...) @stop()
store i32 0, i32* @errno
%36 = tail call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0))
%37 = load i32, i32* @errno
%38 = tail call i8* @strerrno(i32 %37)
%39 = tail call i32 (i8*, i32, i32, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.10, i64 0, i64 0), i32 %36, i32 %37, i8* %38)
%40 = tail call i32 (...) @stop()
ret i32 undef
}