#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ S_IRWXU;
scalar_t__ close(int);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ unlink(char *);
int main(void) {
  int fd = open("/tmp/test_unlink.tmp", O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU);
  if (fd < 0) {
    perror("open failed");
    return EXIT_FAILURE;
  }
  if (close(fd) < 0) {
    perror("close failed");
    return EXIT_FAILURE;
  }
  if (unlink("/tmp/test_unlink.tmp") < 0) {
    perror("unlink failed");
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
lea    0xe86(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a9 <main+0x69>
lea    0xe82(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <main+0xd7>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
jge    11d8 <main+0x98>
lea    0xe5f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <main+0xd7>
lea    0xe21(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    120b <main+0xcb>
lea    0xe39(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <main+0xd7>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2eb0(%rip),%rax
mov    0x2ea1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
mov    0x2e75(%rip),%rax
mov    (%rax),%edx
lea    0xe94(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     119d <main+0x5d>
mov    %eax,%edi
call   1040 <close@plt>
test   %rax,%rax
js     11a6 <main+0x66>
lea    0xe78(%rip),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
js     11af <main+0x6f>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
lea    0xe71(%rip),%rdi
jmp    11b6 <main+0x76>
lea    0xe74(%rip),%rdi
jmp    11b6 <main+0x76>
lea    0xe78(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e06(%rip),%rax
jmp    1199 <main+0x59>

LLVM IR (-O0):
@.str = [21 x i8] c"/tmp/test_unlink.tmp\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"close failed\00"
@.str.3 = [14 x i8] c"unlink failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRWXU
%9 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
store i32 %14, i32* %1
br label %30
15: ; preds = %0
%16 = load i32, i32* %2
%17 = call i64 @close(i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
store i32 %21, i32* %1
br label %30
22: ; preds = %15
%23 = call i64 @unlink(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %1
br label %30
28: ; preds = %22
%29 = load i32, i32* @EXIT_SUCCESS
store i32 %29, i32* %1
br label %30
30: ; preds = %28, %25, %19, %12
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [21 x i8] c"/tmp/test_unlink.tmp\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRWXU = global i32 0
@.str.1 = [12 x i8] c"open failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"close failed\00"
@.str.3 = [14 x i8] c"unlink failed\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_TRUNC
%3 = or i32 %2, %1
%4 = load i32, i32* @O_WRONLY
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRWXU
%7 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %0
%10 = tail call i64 @close(i32 %7)
%11 = icmp slt i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i64 @unlink(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12, %9, %0
%16 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), %0 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), %12 ]
%17 = tail call i32 @perror(i8* %16)
br label %18
18: ; preds = %15, %12
%19 = phi i32* [ @EXIT_SUCCESS, %12 ], [ @EXIT_FAILURE, %15 ]
%20 = load i32, i32* %19
ret i32 %20
}