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

/* Variables and functions */
int O_CLOEXEC;
int O_CREAT;
int O_NOFOLLOW;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
char *udev_device_get_id_filename(struct udev_device *);
struct udev *udev_device_get_udev(struct udev_device *);
int /*<<< orphan*/ udev_get_run_path(struct udev *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ util_create_path(struct udev *, char *);
int /*<<< orphan*/ util_strscpyl(char *, int, int /*<<< orphan*/, char *,
                                 char const *, char *, char const *,
                                 int /*<<< orphan*/ *);
void udev_device_tag(struct udev_device *dev, const char *tag, bool add) {
  const char *id;
  struct udev *udev = udev_device_get_udev(dev);
  char filename[1024];
  id = udev_device_get_id_filename(dev);
  if (id == NULL)
    return;
  util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/tags/",
                tag, "/", id, NULL);
  if (add) {
    int fd;
    util_create_path(udev, filename);
    fd = open(filename, O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC | O_NOFOLLOW,
              0444);
    if (fd >= 0)
      close(fd);
  } else {
    unlink(filename);
  }
}


x86 assembly (clang, -O0):
<udev_device_tag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <udev_device_get_udev@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <udev_device_get_id_filename@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c0 <udev_device_tag+0x40>
jmp    1297 <udev_device_tag+0x117>
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    -0x28(%rbp),%rdi
call   10a0 <udev_get_run_path@plt>
mov    -0x440(%rbp),%rdi
mov    %eax,%edx
mov    -0x10(%rbp),%r8
mov    -0x20(%rbp),%rax
mov    $0x400,%esi
lea    0xe0c(%rip),%rcx
lea    0xe0a(%rip),%r9
xor    %r10d,%r10d
mov    %rax,(%rsp)
movq   $0x0,0x8(%rsp)
call   1080 <util_strscpyl@plt>
cmpl   $0x0,-0x14(%rbp)
je     128b <udev_device_tag+0x10b>
mov    -0x28(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   1060 <util_create_path@plt>
lea    -0x430(%rbp),%rdi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    0x2db7(%rip),%rax
or     (%rax),%esi
mov    0x2d6e(%rip),%rax
or     (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
mov    0x2d94(%rip),%rax
or     (%rax),%esi
mov    $0x124,%edx
call   1090 <open@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0x0,-0x434(%rbp)
jl     1286 <udev_device_tag+0x106>
mov    -0x434(%rbp),%edi
call   1050 <close@plt>
jmp    1297 <udev_device_tag+0x117>
lea    -0x430(%rbp),%rdi
call   1040 <unlink@plt>
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udev_device_tag>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
call   1030 <udev_device_get_udev@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1070 <udev_device_get_id_filename@plt>
test   %rax,%rax
je     1243 <udev_device_tag+0xc3>
mov    %rax,%rbp
mov    %rbx,%rdi
call   10a0 <udev_get_run_path@plt>
lea    0xe3f(%rip),%rcx
lea    0xe3d(%rip),%r9
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %eax,%edx
mov    %r15,%r8
push   $0x0
push   %rbp
call   1080 <util_strscpyl@plt>
add    $0x10,%rsp
test   %r14d,%r14d
je     123b <udev_device_tag+0xbb>
mov    %rsp,%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <util_create_path@plt>
mov    0x2dc5(%rip),%rax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
mov    0x2dd1(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x124,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1243 <udev_device_tag+0xc3>
mov    %eax,%edi
call   1050 <close@plt>
jmp    1243 <udev_device_tag+0xc3>
mov    %rsp,%rdi
call   1040 <unlink@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev_device = type { i32 }
%struct.udev = type { i32 }
@.str = [7 x i8] c"/tags/\00"
@.str.1 = [2 x i8] c"/\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_CLOEXEC = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
define void @udev_device_tag(%struct.udev_device* %0, i8* %1, i32 %2) {
%4 = alloca %struct.udev_device*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.udev*
%9 = alloca [1024 x i8]
%10 = alloca i32
store %struct.udev_device* %0, %struct.udev_device** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%11 = load %struct.udev_device*, %struct.udev_device** %4
%12 = call %struct.udev* @udev_device_get_udev(%struct.udev_device* %11)
store %struct.udev* %12, %struct.udev** %8
%13 = load %struct.udev_device*, %struct.udev_device** %4
%14 = call i8* @udev_device_get_id_filename(%struct.udev_device* %13)
store i8* %14, i8** %7
%15 = load i8*, i8** %7
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %18
17: ; preds = %3
br label %51
18: ; preds = %3
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%20 = load %struct.udev*, %struct.udev** %8
%21 = call i32 @udev_get_run_path(%struct.udev* %20)
%22 = load i8*, i8** %5
%23 = load i8*, i8** %7
%24 = call i32 @util_strscpyl(i8* %19, i32 1024, i32 %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %23, i32* null)
%25 = load i32, i32* %6
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %48
27: ; preds = %18
%28 = load %struct.udev*, %struct.udev** %8
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%30 = call i32 @util_create_path(%struct.udev* %28, i8* %29)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%32 = load i32, i32* @O_WRONLY
%33 = load i32, i32* @O_CREAT
%34 = or i32 %32, %33
%35 = load i32, i32* @O_CLOEXEC
%36 = or i32 %34, %35
%37 = load i32, i32* @O_TRUNC
%38 = or i32 %36, %37
%39 = load i32, i32* @O_NOFOLLOW
%40 = or i32 %38, %39
%41 = call i32 @open(i8* %31, i32 %40, i32 292)
store i32 %41, i32* %10
%42 = load i32, i32* %10
%43 = icmp sge i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %27
%45 = load i32, i32* %10
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %44, %27
br label %51
48: ; preds = %18
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%50 = call i32 @unlink(i8* %49)
br label %51
51: ; preds = %17, %48, %47
ret void
}

LLVM IR (-O2):
%struct.udev_device = type { i32 }
%struct.udev = type { i32 }
@.str = [7 x i8] c"/tags/\00"
@.str.1 = [2 x i8] c"/\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_CLOEXEC = global i32 0
@O_TRUNC = global i32 0
@O_NOFOLLOW = global i32 0
define void @udev_device_tag(%struct.udev_device* %0, i8* %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = tail call %struct.udev* @udev_device_get_udev(%struct.udev_device* %0)
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = tail call i8* @udev_device_get_id_filename(%struct.udev_device* %0)
%8 = icmp eq i8* %7, null
br i1 %8, label %30, label %9
9: ; preds = %3
%10 = tail call i32 @udev_get_run_path(%struct.udev* %5)
%11 = call i32 @util_strscpyl(i8* %6, i32 1024, i32 %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %7, i32* null)
%12 = icmp eq i32 %2, 0
br i1 %12, label %28, label %13
13: ; preds = %9
%14 = call i32 @util_create_path(%struct.udev* %5, i8* %6)
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_CREAT
%17 = or i32 %16, %15
%18 = load i32, i32* @O_CLOEXEC
%19 = or i32 %17, %18
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = load i32, i32* @O_NOFOLLOW
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %6, i32 %23, i32 292)
%25 = icmp sgt i32 %24, -1
br i1 %25, label %26, label %30
26: ; preds = %13
%27 = call i32 @close(i32 %24)
br label %30
28: ; preds = %9
%29 = call i32 @unlink(i8* %6)
br label %30
30: ; preds = %28, %26, %13, %3
ret void
}