#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 */
struct udev {
  int dummy;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ chmod(char const *, int);
int /*<<< orphan*/ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char const *);
int unlink(char const *);
int util_unlink_secure(struct udev *udev, const char *filename) {
  int err;
  chown(filename, 0, 0);
  chmod(filename, 0000);
  err = unlink(filename);
  if (errno == ENOENT)
    err = 0;
  if (err)
    udev_log_null(udev, "unlink(%s) failed: %m\n", filename);
  return err;
}


x86 assembly (clang, -O0):
<util_unlink_secure>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <chown@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <chmod@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
jne    1195 <util_unlink_secure+0x55>
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b3 <util_unlink_secure+0x73>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe52(%rip),%rsi
call   1040 <udev_log_null@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_unlink_secure>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %r15d,%r15d
mov    %rsi,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <chown@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <chmod@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e6b(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e51(%rip),%rdx
cmp    (%rdx),%rcx
mov    %eax,%ebp
cmove  %r15d,%ebp
test   %ebp,%ebp
je     119e <util_unlink_secure+0x5e>
lea    0xe6d(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <udev_log_null@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [23 x i8] c"unlink(%s) failed: %m\0A\00"
define i32 @util_unlink_secure(%struct.udev* %0, i8* %1) {
%3 = alloca %struct.udev*
%4 = alloca i8*
%5 = alloca i32
store %struct.udev* %0, %struct.udev** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = call i32 @chown(i8* %6, i32 0, i32 0)
%8 = load i8*, i8** %4
%9 = call i32 @chmod(i8* %8, i32 0)
%10 = load i8*, i8** %4
%11 = call i32 @unlink(i8* %10)
store i32 %11, i32* %5
%12 = load i64, i64* @errno
%13 = load i64, i64* @ENOENT
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %5
br label %16
16: ; preds = %15, %2
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load %struct.udev*, %struct.udev** %3
%21 = load i8*, i8** %4
%22 = call i32 @udev_log_null(%struct.udev* %20, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %19, %16
%24 = load i32, i32* %5
ret i32 %24
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [23 x i8] c"unlink(%s) failed: %m\0A\00"
define i32 @util_unlink_secure(%struct.udev* %0, i8* %1) {
%3 = tail call i32 @chown(i8* %1, i32 0, i32 0)
%4 = tail call i32 @chmod(i8* %1, i32 0)
%5 = tail call i32 @unlink(i8* %1)
%6 = load i64, i64* @errno
%7 = load i64, i64* @ENOENT
%8 = icmp eq i64 %6, %7
%9 = select i1 %8, i32 0, i32 %5
%10 = icmp eq i32 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = tail call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %13
13: ; preds = %11, %2
ret i32 %9
}