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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int unlink(char *);
int main(int argc, char const *argv[]) {
  FILE *fp1;
  FILE *fp2;
  int y, w;
  fp1 = fopen("check_unlink.txt", "w+");
  if (fp1 == NULL)
    printf("fopen1 failed\n");
  else {
    fclose(fp1);
  }
  y = unlink("check_unlink.txt");
  if (y == -1) {
    printf("unlink 1 failed err no. %d\n", errno);
  }
  fp2 = fopen("check_unlink_2.txt", "w+");
  if (fp1 == NULL)
    printf("fopen1 failed\n");
  else {
    fclose(fp2);
  }
  w = unlink("check_unlink_2.txt");
  if (w == -1) {
    printf("unlink 2 failed err no. %d\n", errno);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
lea    0xead(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118b <main+0x4b>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1194 <main+0x54>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe65(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c4 <main+0x84>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe74(%rip),%rdi
lea    0xe3f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f9 <main+0xb9>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1202 <main+0xc2>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe36(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1232 <main+0xf2>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
lea    0xeb2(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1176 <main+0x26>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    1182 <main+0x32>
lea    0xee2(%rip),%rdi
call   1040 <puts@plt>
lea    0xe77(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    11aa <main+0x5a>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe71(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe7f(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1070 <fopen@plt>
test   %rbx,%rbx
je     11cc <main+0x7c>
mov    %rax,%rdi
call   1050 <fclose@plt>
jmp    11d8 <main+0x88>
lea    0xe8c(%rip),%rdi
call   1040 <puts@plt>
lea    0xe51(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0xffffffff,%eax
jne    1200 <main+0xb0>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [17 x i8] c"check_unlink.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [15 x i8] c"fopen1 failed\0A\00"
@.str.3 = [28 x i8] c"unlink 1 failed err no. %d\0A\00"
@errno = global i32 0
@.str.4 = [19 x i8] c"check_unlink_2.txt\00"
@.str.5 = [28 x i8] c"unlink 2 failed err no. %d\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 i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %10, i32** %6
%11 = load i32*, i32** %6
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %18
15: ; preds = %2
%16 = load i32*, i32** %6
%17 = call i32 @fclose(i32* %16)
br label %18
18: ; preds = %15, %13
%19 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @errno
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %18
%26 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %26, i32** %7
%27 = load i32*, i32** %6
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %34
31: ; preds = %25
%32 = load i32*, i32** %7
%33 = call i32 @fclose(i32* %32)
br label %34
34: ; preds = %31, %29
%35 = call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = load i32, i32* @errno
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %39)
br label %41
41: ; preds = %38, %34
ret i32 0
}

LLVM IR (-O2):
@.str = [17 x i8] c"check_unlink.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.3 = [28 x i8] c"unlink 1 failed err no. %d\0A\00"
@errno = global i32 0
@.str.4 = [19 x i8] c"check_unlink_2.txt\00"
@.str.5 = [28 x i8] c"unlink 2 failed err no. %d\0A\00"
@str.6 = [14 x i8] c"fopen1 failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
br label %9
7: ; preds = %2
%8 = tail call i32 @fclose(i32* %3)
br label %9
9: ; preds = %7, %5
%10 = tail call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %12, %9
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br i1 %4, label %17, label %19
17: ; preds = %15
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
br label %21
19: ; preds = %15
%20 = tail call i32 @fclose(i32* %16)
br label %21
21: ; preds = %19, %17
%22 = tail call i32 @unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @errno
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %21
ret i32 0
}