#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;
};
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ device;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int strlen(char const *);
char const *udev_get_dev_path(struct udev *);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ util_create_path(struct udev *, char *);
int /*<<< orphan*/ util_delete_path(struct udev *, char *);
int /*<<< orphan*/ util_path_encode(char *, int);
size_t util_strlcat(char *, char const *, int);
int /*<<< orphan*/ util_strlcpy(char *, char const *, int);
int name_index(struct udev *udev, const char *devpath, const char *name,
               int add) {
  char device[1024];
  char filename[1024 * 2];
  size_t devlen = strlen(udev_get_dev_path(udev)) + 1;
  size_t start;
  int fd;
  util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
  start = util_strlcat(filename, "/.udev/names/", sizeof(filename));
  util_strlcat(filename, &name[devlen], sizeof(filename));
  util_path_encode(&filename[start], sizeof(filename) - start);
  util_strlcpy(device, devpath, sizeof(device));
  util_path_encode(device, sizeof(device));
  util_strlcat(filename, "/", sizeof(filename));
  util_strlcat(filename, device, sizeof(filename));
  if (add) {
    udev_log_null(udev, "creating index: '%s'\n", filename);
    util_create_path(udev, filename);
    fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
    if (fd > 0)
      close(fd);
  } else {
    udev_log_null(udev, "removing index: '%s'\n", filename);
    unlink(filename);
    util_delete_path(udev, filename);
  }
  return 0;
}


x86 assembly (clang, -O0):
<name_index>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <udev_get_dev_path@plt>
mov    %rax,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0xc28(%rbp)
lea    -0xc20(%rbp),%rax
mov    %rax,-0xc40(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <udev_get_dev_path@plt>
mov    -0xc40(%rbp),%rdi
mov    %rax,%rsi
mov    $0x800,%edx
call   10c0 <util_strlcpy@plt>
lea    -0xc20(%rbp),%rdi
lea    0xde0(%rip),%rsi
mov    $0x800,%edx
call   10d0 <util_strlcat@plt>
mov    %rax,-0xc30(%rbp)
lea    -0xc20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    -0xc28(%rbp),%rsi
mov    $0x800,%edx
call   10d0 <util_strlcat@plt>
lea    -0xc20(%rbp),%rdi
add    -0xc30(%rbp),%rdi
mov    $0x800,%eax
sub    -0xc30(%rbp),%rax
mov    %eax,%esi
call   10a0 <util_path_encode@plt>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   10c0 <util_strlcpy@plt>
lea    -0x420(%rbp),%rdi
mov    $0x400,%esi
call   10a0 <util_path_encode@plt>
lea    -0xc20(%rbp),%rdi
lea    0xd6a(%rip),%rsi
mov    $0x800,%edx
call   10d0 <util_strlcat@plt>
lea    -0xc20(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x800,%edx
call   10d0 <util_strlcat@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1344 <name_index+0x194>
mov    -0x8(%rbp),%rdi
lea    -0xc20(%rbp),%rdx
lea    0xd2e(%rip),%rsi
call   1040 <udev_log_null@plt>
mov    -0x8(%rbp),%rdi
lea    -0xc20(%rbp),%rsi
call   1080 <util_create_path@plt>
lea    -0xc20(%rbp),%rdi
mov    0x2cc5(%rip),%rax
mov    (%rax),%esi
mov    0x2ce4(%rip),%rax
or     (%rax),%esi
mov    0x2ce3(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10b0 <open@plt>
mov    %eax,-0xc34(%rbp)
cmpl   $0x0,-0xc34(%rbp)
jle    133f <name_index+0x18f>
mov    -0xc34(%rbp),%edi
call   1070 <close@plt>
jmp    1377 <name_index+0x1c7>
mov    -0x8(%rbp),%rdi
lea    -0xc20(%rbp),%rdx
lea    0xcce(%rip),%rsi
call   1040 <udev_log_null@plt>
lea    -0xc20(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%rdi
lea    -0xc20(%rbp),%rsi
call   1060 <util_delete_path@plt>
xor    %eax,%eax
add    $0xc40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<name_index>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc18,%rsp
mov    %ecx,0x4(%rsp)
mov    %rdx,%r12
mov    %rsi,0x8(%rsp)
mov    %rdi,%rbx
call   1090 <udev_get_dev_path@plt>
mov    %rax,%rdi
call   1050 <strlen@plt>
movslq %eax,%r15
mov    %rbx,%rdi
call   1090 <udev_get_dev_path@plt>
lea    0x10(%rsp),%r13
mov    $0x800,%ebp
mov    %r13,%rdi
mov    %rax,%rsi
mov    $0x800,%edx
call   10c0 <util_strlcpy@plt>
lea    0xdf7(%rip),%rsi
mov    %r13,%rdi
mov    $0x800,%edx
call   10d0 <util_strlcat@plt>
mov    %rax,%r14
lea    (%r15,%r12,1),%rsi
add    $0x1,%rsi
mov    %r13,%rdi
mov    $0x800,%edx
call   10d0 <util_strlcat@plt>
lea    (%rsp,%r14,1),%rdi
add    $0x10,%rdi
sub    %r14d,%ebp
mov    %ebp,%esi
call   10a0 <util_path_encode@plt>
lea    0x810(%rsp),%rbp
mov    %rbp,%rdi
mov    0x8(%rsp),%rsi
mov    $0x400,%edx
call   10c0 <util_strlcpy@plt>
mov    %rbp,%rdi
mov    $0x400,%esi
call   10a0 <util_path_encode@plt>
lea    0xd9e(%rip),%rsi
mov    %r13,%rdi
mov    $0x800,%edx
call   10d0 <util_strlcat@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    $0x800,%edx
call   10d0 <util_strlcat@plt>
cmpl   $0x0,0x4(%rsp)
je     12e9 <name_index+0x139>
lea    0xd75(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1040 <udev_log_null@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <util_create_path@plt>
mov    0x2d0d(%rip),%rax
mov    0x2d2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d2b(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a4,%edx
call   10b0 <open@plt>
test   %eax,%eax
jle    1313 <name_index+0x163>
mov    %eax,%edi
call   1070 <close@plt>
jmp    1313 <name_index+0x163>
lea    0xd34(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rdx
call   1040 <udev_log_null@plt>
mov    %rbp,%rdi
call   1030 <unlink@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <util_delete_path@plt>
xor    %eax,%eax
add    $0xc18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@.str = [14 x i8] c"/.udev/names/\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [22 x i8] c"creating index: '%s'\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.3 = [22 x i8] c"removing index: '%s'\0A\00"
define i32 @name_index(%struct.udev* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.udev*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca [2048 x i8]
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
store %struct.udev* %0, %struct.udev** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%14 = load %struct.udev*, %struct.udev** %5
%15 = call i8* @udev_get_dev_path(%struct.udev* %14)
%16 = call i32 @strlen(i8* %15)
%17 = add nsw i32 %16, 1
%18 = sext i32 %17 to i64
store i64 %18, i64* %11
%19 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%20 = load %struct.udev*, %struct.udev** %5
%21 = call i8* @udev_get_dev_path(%struct.udev* %20)
%22 = call i32 @util_strlcpy(i8* %19, i8* %21, i32 2048)
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%24 = call i64 @util_strlcat(i8* %23, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 2048)
store i64 %24, i64* %12
%25 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%26 = load i8*, i8** %7
%27 = load i64, i64* %11
%28 = getelementptr inbounds i8, i8* %26, i64 %27
%29 = call i64 @util_strlcat(i8* %25, i8* %28, i32 2048)
%30 = load i64, i64* %12
%31 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 %30
%32 = load i64, i64* %12
%33 = sub i64 2048, %32
%34 = trunc i64 %33 to i32
%35 = call i32 @util_path_encode(i8* %31, i32 %34)
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%37 = load i8*, i8** %6
%38 = call i32 @util_strlcpy(i8* %36, i8* %37, i32 1024)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%40 = call i32 @util_path_encode(i8* %39, i32 1024)
%41 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%42 = call i64 @util_strlcat(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2048)
%43 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%45 = call i64 @util_strlcat(i8* %43, i8* %44, i32 2048)
%46 = load i32, i32* %8
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %68
48: ; preds = %4
%49 = load %struct.udev*, %struct.udev** %5
%50 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%51 = call i32 @udev_log_null(%struct.udev* %49, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = load %struct.udev*, %struct.udev** %5
%53 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%54 = call i32 @util_create_path(%struct.udev* %52, i8* %53)
%55 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%56 = load i32, i32* @O_WRONLY
%57 = load i32, i32* @O_TRUNC
%58 = or i32 %56, %57
%59 = load i32, i32* @O_CREAT
%60 = or i32 %58, %59
%61 = call i32 @open(i8* %55, i32 %60, i32 420)
store i32 %61, i32* %13
%62 = load i32, i32* %13
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %48
%65 = load i32, i32* %13
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %64, %48
br label %77
68: ; preds = %4
%69 = load %struct.udev*, %struct.udev** %5
%70 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%71 = call i32 @udev_log_null(%struct.udev* %69, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%73 = call i32 @unlink(i8* %72)
%74 = load %struct.udev*, %struct.udev** %5
%75 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%76 = call i32 @util_delete_path(%struct.udev* %74, i8* %75)
br label %77
77: ; preds = %68, %67
ret i32 0
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@.str = [14 x i8] c"/.udev/names/\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [22 x i8] c"creating index: '%s'\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.3 = [22 x i8] c"removing index: '%s'\0A\00"
define i32 @name_index(%struct.udev* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [1024 x i8]
%6 = alloca [2048 x i8]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 0
%9 = tail call i8* @udev_get_dev_path(%struct.udev* %0)
%10 = tail call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = tail call i8* @udev_get_dev_path(%struct.udev* %0)
%14 = call i32 @util_strlcpy(i8* %8, i8* %13, i32 2048)
%15 = call i64 @util_strlcat(i8* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 2048)
%16 = getelementptr inbounds i8, i8* %2, i64 %12
%17 = call i64 @util_strlcat(i8* %8, i8* %16, i32 2048)
%18 = getelementptr inbounds [2048 x i8], [2048 x i8]* %6, i64 0, i64 %15
%19 = trunc i64 %15 to i32
%20 = sub i32 2048, %19
%21 = call i32 @util_path_encode(i8* %18, i32 %20)
%22 = call i32 @util_strlcpy(i8* %7, i8* %1, i32 1024)
%23 = call i32 @util_path_encode(i8* %7, i32 1024)
%24 = call i64 @util_strlcat(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 2048)
%25 = call i64 @util_strlcat(i8* %8, i8* %7, i32 2048)
%26 = icmp eq i32 %3, 0
br i1 %26, label %39, label %27
27: ; preds = %4
%28 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%29 = call i32 @util_create_path(%struct.udev* %0, i8* %8)
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %31, %30
%33 = load i32, i32* @O_CREAT
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %8, i32 %34, i32 420)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %27
%38 = call i32 @close(i32 %35)
br label %43
39: ; preds = %4
%40 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i8* %8)
%41 = call i32 @unlink(i8* %8)
%42 = call i32 @util_delete_path(%struct.udev* %0, i8* %8)
br label %43
43: ; preds = %27, %37, %39
ret i32 0
}