#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 chmod(char const *, int);
int 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 retval;
  retval = chown(filename, 0, 0);
  if (retval)
    udev_log_null(udev, "chown(%s, 0, 0) failed: %m\n", filename);
  retval = chmod(filename, 0000);
  if (retval)
    udev_log_null(udev, "chmod(%s, 0000) failed: %m\n", filename);
  retval = unlink(filename);
  if (errno == ENOENT)
    retval = 0;
  if (retval)
    udev_log_null(udev, "unlink(%s) failed: %m\n", filename);
  return retval;
}


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    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     117e <util_unlink_secure+0x3e>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe87(%rip),%rsi
call   1040 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <chmod@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11aa <util_unlink_secure+0x6a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe77(%rip),%rsi
call   1040 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
jne    11d7 <util_unlink_secure+0x97>
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11f5 <util_unlink_secure+0xb5>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe48(%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
mov    %rsi,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <chown@plt>
test   %eax,%eax
je     116f <util_unlink_secure+0x2f>
lea    0xe9c(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <udev_log_null@plt>
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <chmod@plt>
test   %eax,%eax
je     1192 <util_unlink_secure+0x52>
lea    0xe95(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <udev_log_null@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2e25(%rip),%rdx
cmp    (%rdx),%rcx
mov    %eax,%ebp
cmove  %r15d,%ebp
test   %ebp,%ebp
je     11ca <util_unlink_secure+0x8a>
lea    0xe79(%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 }
@.str = [28 x i8] c"chown(%s, 0, 0) failed: %m\0A\00"
@.str.1 = [28 x i8] c"chmod(%s, 0000) failed: %m\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [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)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load %struct.udev*, %struct.udev** %3
%12 = load i8*, i8** %4
%13 = call i32 @udev_log_null(%struct.udev* %11, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %10, %2
%15 = load i8*, i8** %4
%16 = call i32 @chmod(i8* %15, i32 0)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load %struct.udev*, %struct.udev** %3
%21 = load i8*, i8** %4
%22 = call i32 @udev_log_null(%struct.udev* %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %19, %14
%24 = load i8*, i8** %4
%25 = call i32 @unlink(i8* %24)
store i32 %25, i32* %5
%26 = load i64, i64* @errno
%27 = load i64, i64* @ENOENT
%28 = icmp eq i64 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 0, i32* %5
br label %30
30: ; preds = %29, %23
%31 = load i32, i32* %5
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load %struct.udev*, %struct.udev** %3
%35 = load i8*, i8** %4
%36 = call i32 @udev_log_null(%struct.udev* %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %33, %30
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@.str = [28 x i8] c"chown(%s, 0, 0) failed: %m\0A\00"
@.str.1 = [28 x i8] c"chmod(%s, 0000) failed: %m\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.2 = [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 = icmp eq i32 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %2
%6 = tail call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %7
7: ; preds = %5, %2
%8 = tail call i32 @chmod(i8* %1, i32 0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %12
12: ; preds = %10, %7
%13 = tail call i32 @unlink(i8* %1)
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
%17 = select i1 %16, i32 0, i32 %13
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %12
%20 = tail call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %21
21: ; preds = %19, %12
ret i32 %17
}