#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 */
typedef int /*<<< orphan*/ target;
struct udev {
  int dummy;
};
struct stat {
  int st_mode;
  scalar_t__ st_rdev;
};
typedef int /*<<< orphan*/ slink_tmp;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ S_IFLNK;
int S_IFMT;
scalar_t__ S_ISBLK(int);
scalar_t__ S_ISCHR(int);
scalar_t__ S_ISLNK(int);
scalar_t__ lstat(char const *, struct stat *);
int readlink(char const *, char *, int);
int rename(char *, char const *);
scalar_t__ strcmp(char *, char *);
int symlink(char *, char const *);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char const *, ...);
int /*<<< orphan*/ udev_selinux_lsetfilecon(struct udev *, char const *,
                                            int /*<<< orphan*/);
int /*<<< orphan*/ udev_selinux_resetfscreatecon(struct udev *);
int /*<<< orphan*/ udev_selinux_setfscreatecon(struct udev *, char const *,
                                               int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ util_strlcat(char *, char const *, int);
int /*<<< orphan*/ util_strlcpy(char *, char const *, int);
int node_symlink(struct udev *udev, const char *node, const char *slink) {
  struct stat stats;
  char target[1024];
  char slink_tmp[1024 + sizeof(".udev-tmp")];
  int i = 0;
  int tail = 0;
  int len;
  int err = 0;
  target[0] = '\0';
  while (node[i] && (node[i] == slink[i])) {
    if (node[i] == '/')
      tail = i + 1;
    i++;
  }
  while (slink[i] != '\0') {
    if (slink[i] == '/')
      util_strlcat(target, "../", sizeof(target));
    i++;
  }
  util_strlcat(target, &node[tail], sizeof(target));
  if (lstat(slink, &stats) == 0) {
    if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
      struct stat stats2;
      udev_log_null(udev, "found existing node instead of symlink '%s'\n",
                    slink);
      if (lstat(node, &stats2) == 0) {
        if ((stats.st_mode & S_IFMT) == (stats2.st_mode & S_IFMT) &&
            stats.st_rdev == stats2.st_rdev) {
          udev_log_null(
              udev, "replace device node '%s' with symlink to our node '%s'\n",
              slink, node);
        } else {
          udev_log_null(udev,
                        "device node '%s' already exists, "
                        "link to '%s' will not overwrite it\n",
                        slink, node);
          goto exit;
        }
      }
    } else if (S_ISLNK(stats.st_mode)) {
      char buf[1024];
      udev_log_null(udev, "found existing symlink '%s'\n", slink);
      len = readlink(slink, buf, sizeof(buf));
      if (len > 0) {
        buf[len] = '\0';
        if (strcmp(target, buf) == 0) {
          udev_log_null(udev,
                        "preserve already existing symlink '%s' to '%s'\n",
                        slink, target);
          udev_selinux_lsetfilecon(udev, slink, S_IFLNK);
          goto exit;
        }
      }
    }
  } else {
    udev_log_null(udev, "creating symlink '%s' to '%s'\n", slink, target);
    udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
    err = symlink(target, slink);
    udev_selinux_resetfscreatecon(udev);
    if (err == 0)
      goto exit;
  }
  udev_log_null(udev, "atomically replace '%s'\n", slink);
  util_strlcpy(slink_tmp, slink, sizeof(slink_tmp));
  util_strlcat(slink_tmp, ".udev-tmp", sizeof(slink_tmp));
  unlink(slink_tmp);
  udev_selinux_setfscreatecon(udev, slink, S_IFLNK);
  err = symlink(target, slink_tmp);
  udev_selinux_resetfscreatecon(udev);
  if (err != 0) {
    udev_log_null(udev, "symlink(%s, %s) failed: %m\n", target, slink_tmp);
    goto exit;
  }
  err = rename(slink_tmp, slink);
  if (err != 0) {
    udev_log_null(udev, "rename(%s, %s) failed: %m\n", slink_tmp, slink);
    unlink(slink_tmp);
    goto exit;
  }
exit:
  return err;
}


x86 assembly (clang, -O0):
<node_symlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x844(%rbp)
movl   $0x0,-0x848(%rbp)
movl   $0x0,-0x850(%rbp)
movb   $0x0,-0x430(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x844(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0xc61(%rbp)
je     1275 <node_symlink+0x85>
mov    -0x10(%rbp),%rax
movslq -0x844(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x18(%rbp),%rcx
movslq -0x844(%rbp),%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
sete   %al
mov    %al,-0xc61(%rbp)
mov    -0xc61(%rbp),%al
test   $0x1,%al
jne    1288 <node_symlink+0x98>
jmp    12c3 <node_symlink+0xd3>
mov    -0x10(%rbp),%rax
movslq -0x844(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    12af <node_symlink+0xbf>
mov    -0x844(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x848(%rbp)
mov    -0x844(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x844(%rbp)
jmp    122c <node_symlink+0x3c>
jmp    12c8 <node_symlink+0xd8>
mov    -0x18(%rbp),%rax
movslq -0x844(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     1324 <node_symlink+0x134>
mov    -0x18(%rbp),%rax
movslq -0x844(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    1310 <node_symlink+0x120>
lea    -0x430(%rbp),%rdi
lea    0xcfa(%rip),%rsi
mov    $0x400,%edx
call   1110 <util_strlcat@plt>
mov    -0x844(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x844(%rbp)
jmp    12c8 <node_symlink+0xd8>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x848(%rbp),%rax
add    %rax,%rsi
mov    $0x400,%edx
call   1110 <util_strlcat@plt>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0x0,%rax
jne    14f2 <node_symlink+0x302>
mov    -0x28(%rbp),%edi
call   10c0 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    137e <node_symlink+0x18e>
mov    -0x28(%rbp),%edi
call   10b0 <S_ISCHR@plt>
cmp    $0x0,%rax
je     142a <node_symlink+0x23a>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xc77(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
mov    -0x10(%rbp),%rdi
lea    -0x860(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0x0,%rax
jne    1425 <node_symlink+0x235>
mov    -0x28(%rbp),%eax
mov    0x2c20(%rip),%rcx
and    (%rcx),%eax
mov    -0x860(%rbp),%ecx
mov    0x2c11(%rip),%rdx
and    (%rdx),%ecx
cmp    %ecx,%eax
jne    1401 <node_symlink+0x211>
mov    -0x20(%rbp),%rax
cmp    -0x858(%rbp),%rax
jne    1401 <node_symlink+0x211>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xc3c(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
jmp    1420 <node_symlink+0x230>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xc55(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
jmp    166a <node_symlink+0x47a>
jmp    1425 <node_symlink+0x235>
jmp    14ed <node_symlink+0x2fd>
mov    -0x28(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%rax
je     14e8 <node_symlink+0x2f8>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xc63(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
mov    -0x18(%rbp),%rdi
lea    -0xc60(%rbp),%rsi
mov    $0x400,%edx
call   1050 <readlink@plt>
mov    %eax,-0x84c(%rbp)
cmpl   $0x0,-0x84c(%rbp)
jle    14e3 <node_symlink+0x2f3>
movslq -0x84c(%rbp),%rax
movb   $0x0,-0xc60(%rbp,%rax,1)
lea    -0x430(%rbp),%rdi
lea    -0xc60(%rbp),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
jne    14de <node_symlink+0x2ee>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x430(%rbp),%rcx
lea    0xc0f(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2b16(%rip),%rax
mov    (%rax),%edx
call   1100 <udev_selinux_lsetfilecon@plt>
jmp    166a <node_symlink+0x47a>
jmp    14e3 <node_symlink+0x2f3>
jmp    14e8 <node_symlink+0x2f8>
jmp    14ed <node_symlink+0x2fd>
jmp    155b <node_symlink+0x36b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x430(%rbp),%rcx
lea    0xbf3(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2aca(%rip),%rax
mov    (%rax),%edx
call   10d0 <udev_selinux_setfscreatecon@plt>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <symlink@plt>
mov    %eax,-0x850(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <udev_selinux_resetfscreatecon@plt>
cmpl   $0x0,-0x850(%rbp)
jne    1556 <node_symlink+0x366>
jmp    166a <node_symlink+0x47a>
jmp    155b <node_symlink+0x36b>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xbb0(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
lea    -0x840(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x40a,%edx
call   10f0 <util_strlcpy@plt>
lea    -0x840(%rbp),%rdi
lea    0xb9f(%rip),%rsi
mov    $0x40a,%edx
call   1110 <util_strlcat@plt>
lea    -0x840(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2a2f(%rip),%rax
mov    (%rax),%edx
call   10d0 <udev_selinux_setfscreatecon@plt>
lea    -0x430(%rbp),%rdi
lea    -0x840(%rbp),%rsi
call   1070 <symlink@plt>
mov    %eax,-0x850(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <udev_selinux_resetfscreatecon@plt>
cmpl   $0x0,-0x850(%rbp)
je     1614 <node_symlink+0x424>
mov    -0x8(%rbp),%rdi
lea    -0x430(%rbp),%rdx
lea    -0x840(%rbp),%rcx
lea    0xb35(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
jmp    166a <node_symlink+0x47a>
lea    -0x840(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10e0 <rename@plt>
mov    %eax,-0x850(%rbp)
cmpl   $0x0,-0x850(%rbp)
je     1665 <node_symlink+0x475>
mov    -0x8(%rbp),%rdi
lea    -0x840(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xb0c(%rip),%rsi
mov    $0x0,%al
call   1060 <udev_log_null@plt>
lea    -0x840(%rbp),%rdi
call   1030 <unlink@plt>
jmp    166a <node_symlink+0x47a>
jmp    166a <node_symlink+0x47a>
mov    -0x850(%rbp),%eax
add    $0xc70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<node_symlink>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x838,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
movb   $0x0,0x430(%rsp)
mov    (%rsi),%cl
test   %cl,%cl
mov    %rdi,0x8(%rsp)
je     1241 <node_symlink+0x51>
xor    %eax,%eax
xor    %ebp,%ebp
xchg   %ax,%ax
cmp    (%r15,%rax,1),%cl
jne    1245 <node_symlink+0x55>
lea    0x1(%rax),%rdx
cmp    $0x2f,%cl
cmove  %edx,%ebp
movzbl 0x1(%r12,%rax,1),%ecx
mov    %rdx,%rax
test   %cl,%cl
jne    1220 <node_symlink+0x30>
mov    %edx,%eax
jmp    1245 <node_symlink+0x55>
xor    %eax,%eax
xor    %ebp,%ebp
mov    %eax,%r14d
add    %r15,%r14
lea    0xdae(%rip),%r13
lea    0x430(%rsp),%rbx
jmp    1274 <node_symlink+0x84>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x400,%edx
call   1110 <util_strlcat@plt>
add    $0x1,%r14
movzbl (%r14),%eax
cmp    $0x2f,%al
je     1260 <node_symlink+0x70>
test   %al,%al
jne    1270 <node_symlink+0x80>
movslq %ebp,%rsi
add    %r12,%rsi
lea    0x430(%rsp),%rdi
mov    $0x400,%edx
call   1110 <util_strlcat@plt>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1080 <lstat@plt>
test   %rax,%rax
je     1303 <node_symlink+0x113>
lea    0xe4a(%rip),%rsi
xor    %r14d,%r14d
lea    0x430(%rsp),%rbx
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1060 <udev_log_null@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   10d0 <udev_selinux_setfscreatecon@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1070 <symlink@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   10a0 <udev_selinux_resetfscreatecon@plt>
test   %ebx,%ebx
jne    1352 <node_symlink+0x162>
jmp    1426 <node_symlink+0x236>
mov    0x10(%rsp),%ebx
mov    %ebx,%edi
call   10c0 <S_ISBLK@plt>
test   %rax,%rax
mov    0x8(%rsp),%rbp
jne    1328 <node_symlink+0x138>
mov    %ebx,%edi
call   10b0 <S_ISCHR@plt>
test   %rax,%rax
je     148e <node_symlink+0x29e>
lea    0xcd5(%rip),%rsi
mov    %rbp,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <udev_log_null@plt>
lea    0x20(%rsp),%rsi
mov    %r12,%rdi
call   1080 <lstat@plt>
test   %rax,%rax
je     143b <node_symlink+0x24b>
lea    0xdc1(%rip),%rsi
xor    %r14d,%r14d
mov    %rbp,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <udev_log_null@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x40a,%edx
call   10f0 <util_strlcpy@plt>
lea    0xdae(%rip),%rsi
mov    %rbx,%rdi
mov    $0x40a,%edx
call   1110 <util_strlcat@plt>
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2c47(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   10d0 <udev_selinux_setfscreatecon@plt>
lea    0x430(%rsp),%rdi
mov    %rbx,%rsi
call   1070 <symlink@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   10a0 <udev_selinux_resetfscreatecon@plt>
test   %ebx,%ebx
je     13ec <node_symlink+0x1fc>
lea    0xd6a(%rip),%rsi
lea    0x430(%rsp),%rdx
lea    0x20(%rsp),%rcx
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <udev_log_null@plt>
jmp    1423 <node_symlink+0x233>
lea    0x20(%rsp),%rdi
mov    %r15,%rsi
call   10e0 <rename@plt>
test   %eax,%eax
je     1426 <node_symlink+0x236>
mov    %eax,%ebx
lea    0xd53(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %rbp,%rdi
mov    %r14,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1060 <udev_log_null@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x838,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2b96(%rip),%rax
xor    0x20(%rsp),%ebx
test   %ebx,(%rax)
jne    1472 <node_symlink+0x282>
mov    0x18(%rsp),%rax
cmp    0x28(%rsp),%rax
jne    1472 <node_symlink+0x282>
lea    0xbd4(%rip),%rsi
mov    %rbp,%rdi
mov    %r15,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1060 <udev_log_null@plt>
jmp    1352 <node_symlink+0x162>
lea    0xbf0(%rip),%rsi
xor    %r14d,%r14d
mov    %rbp,%rdi
mov    %r15,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1060 <udev_log_null@plt>
jmp    1426 <node_symlink+0x236>
mov    %ebx,%edi
call   1040 <S_ISLNK@plt>
test   %rax,%rax
je     1352 <node_symlink+0x162>
lea    0xc09(%rip),%rsi
mov    %rbp,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <udev_log_null@plt>
lea    0x20(%rsp),%rsi
mov    %r15,%rdi
mov    $0x400,%edx
call   1050 <readlink@plt>
test   %eax,%eax
jle    1352 <node_symlink+0x162>
mov    %eax,%eax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0x430(%rsp),%rdi
lea    0x20(%rsp),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1352 <node_symlink+0x162>
lea    0xbd6(%rip),%rsi
xor    %r14d,%r14d
lea    0x430(%rsp),%rcx
mov    %rbp,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <udev_log_null@plt>
mov    0x2ad4(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1100 <udev_selinux_lsetfilecon@plt>
jmp    1426 <node_symlink+0x236>

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.stat = type { i32, i64 }
@.str = [4 x i8] c"../\00"
@.str.1 = [45 x i8] c"found existing node instead of symlink '%s'\0A\00"
@S_IFMT = global i32 0
@.str.2 = [56 x i8] c"replace device node '%s' with symlink to our node '%s'\0A\00"
@.str.3 = [69 x i8] c"device node '%s' already exists, link to '%s' will not overwrite it\0A\00"
@.str.4 = [29 x i8] c"found existing symlink '%s'\0A\00"
@.str.5 = [48 x i8] c"preserve already existing symlink '%s' to '%s'\0A\00"
@S_IFLNK = global i32 0
@.str.6 = [31 x i8] c"creating symlink '%s' to '%s'\0A\00"
@.str.7 = [25 x i8] c"atomically replace '%s'\0A\00"
@.str.8 = [10 x i8] c".udev-tmp\00"
@.str.9 = [28 x i8] c"symlink(%s, %s) failed: %m\0A\00"
@.str.10 = [27 x i8] c"rename(%s, %s) failed: %m\0A\00"
define i32 @node_symlink(%struct.udev* %0, i8* %1, i8* %2) {
%4 = alloca %struct.udev*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca [1024 x i8]
%9 = alloca [1034 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.stat
%15 = alloca [1024 x i8]
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
store i32 0, i32* %10
store i32 0, i32* %11
store i32 0, i32* %13
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
store i8 0, i8* %16
br label %17
17: ; preds = %52, %3
%18 = load i8*, i8** %5
%19 = load i32, i32* %10
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %17
%26 = load i8*, i8** %5
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = load i8*, i8** %6
%33 = load i32, i32* %10
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %31, %37
br label %39
39: ; preds = %25, %17
%40 = phi i1 [ false, %17 ], [ %38, %25 ]
br i1 %40, label %41, label %55
41: ; preds = %39
%42 = load i8*, i8** %5
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp eq i32 %47, 47
br i1 %48, label %49, label %52
49: ; preds = %41
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %11
br label %52
52: ; preds = %49, %41
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %17
55: ; preds = %39
br label %56
56: ; preds = %75, %55
%57 = load i8*, i8** %6
%58 = load i32, i32* %10
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %78
64: ; preds = %56
%65 = load i8*, i8** %6
%66 = load i32, i32* %10
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8, i8* %65, i64 %67
%69 = load i8, i8* %68
%70 = sext i8 %69 to i32
%71 = icmp eq i32 %70, 47
br i1 %71, label %72, label %75
72: ; preds = %64
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%74 = call i32 @util_strlcat(i8* %73, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 1024)
br label %75
75: ; preds = %72, %64
%76 = load i32, i32* %10
%77 = add nsw i32 %76, 1
store i32 %77, i32* %10
br label %56
78: ; preds = %56
%79 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%80 = load i8*, i8** %5
%81 = load i32, i32* %11
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds i8, i8* %80, i64 %82
%84 = call i32 @util_strlcat(i8* %79, i8* %83, i32 1024)
%85 = load i8*, i8** %6
%86 = call i64 @lstat(i8* %85, %struct.stat* %7)
%87 = icmp eq i64 %86, 0
br i1 %87, label %88, label %168
88: ; preds = %78
%89 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%90 = load i32, i32* %89
%91 = call i64 @S_ISBLK(i32 %90)
%92 = icmp ne i64 %91, 0
br i1 %92, label %98, label %93
93: ; preds = %88
%94 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%95 = load i32, i32* %94
%96 = call i64 @S_ISCHR(i32 %95)
%97 = icmp ne i64 %96, 0
br i1 %97, label %98, label %133
98: ; preds = %93, %88
%99 = load %struct.udev*, %struct.udev** %4
%100 = load i8*, i8** %6
%101 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %99, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %100)
%102 = load i8*, i8** %5
%103 = call i64 @lstat(i8* %102, %struct.stat* %14)
%104 = icmp eq i64 %103, 0
br i1 %104, label %105, label %132
105: ; preds = %98
%106 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%107 = load i32, i32* %106
%108 = load i32, i32* @S_IFMT
%109 = and i32 %107, %108
%110 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%111 = load i32, i32* %110
%112 = load i32, i32* @S_IFMT
%113 = and i32 %111, %112
%114 = icmp eq i32 %109, %113
br i1 %114, label %115, label %126
115: ; preds = %105
%116 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%117 = load i64, i64* %116
%118 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 1
%119 = load i64, i64* %118
%120 = icmp eq i64 %117, %119
br i1 %120, label %121, label %126
121: ; preds = %115
%122 = load %struct.udev*, %struct.udev** %4
%123 = load i8*, i8** %6
%124 = load i8*, i8** %5
%125 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %122, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %123, i8* %124)
br label %131
126: ; preds = %115, %105
%127 = load %struct.udev*, %struct.udev** %4
%128 = load i8*, i8** %6
%129 = load i8*, i8** %5
%130 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %127, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i8* %128, i8* %129)
br label %227
131: ; preds = %121
br label %132
132: ; preds = %131, %98
br label %167
133: ; preds = %93
%134 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%135 = load i32, i32* %134
%136 = call i64 @S_ISLNK(i32 %135)
%137 = icmp ne i64 %136, 0
br i1 %137, label %138, label %166
138: ; preds = %133
%139 = load %struct.udev*, %struct.udev** %4
%140 = load i8*, i8** %6
%141 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %139, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %140)
%142 = load i8*, i8** %6
%143 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%144 = call i32 @readlink(i8* %142, i8* %143, i32 1024)
store i32 %144, i32* %12
%145 = load i32, i32* %12
%146 = icmp sgt i32 %145, 0
br i1 %146, label %147, label %165
147: ; preds = %138
%148 = load i32, i32* %12
%149 = sext i32 %148 to i64
%150 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 %149
store i8 0, i8* %150
%151 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%152 = getelementptr inbounds [1024 x i8], [1024 x i8]* %15, i64 0, i64 0
%153 = call i64 @strcmp(i8* %151, i8* %152)
%154 = icmp eq i64 %153, 0
br i1 %154, label %155, label %164
155: ; preds = %147
%156 = load %struct.udev*, %struct.udev** %4
%157 = load i8*, i8** %6
%158 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%159 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %156, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0), i8* %157, i8* %158)
%160 = load %struct.udev*, %struct.udev** %4
%161 = load i8*, i8** %6
%162 = load i32, i32* @S_IFLNK
%163 = call i32 @udev_selinux_lsetfilecon(%struct.udev* %160, i8* %161, i32 %162)
br label %227
164: ; preds = %147
br label %165
165: ; preds = %164, %138
br label %166
166: ; preds = %165, %133
br label %167
167: ; preds = %166, %132
br label %186
168: ; preds = %78
%169 = load %struct.udev*, %struct.udev** %4
%170 = load i8*, i8** %6
%171 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%172 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %169, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %170, i8* %171)
%173 = load %struct.udev*, %struct.udev** %4
%174 = load i8*, i8** %6
%175 = load i32, i32* @S_IFLNK
%176 = call i32 @udev_selinux_setfscreatecon(%struct.udev* %173, i8* %174, i32 %175)
%177 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%178 = load i8*, i8** %6
%179 = call i32 @symlink(i8* %177, i8* %178)
store i32 %179, i32* %13
%180 = load %struct.udev*, %struct.udev** %4
%181 = call i32 @udev_selinux_resetfscreatecon(%struct.udev* %180)
%182 = load i32, i32* %13
%183 = icmp eq i32 %182, 0
br i1 %183, label %184, label %185
184: ; preds = %168
br label %227
185: ; preds = %168
br label %186
186: ; preds = %185, %167
%187 = load %struct.udev*, %struct.udev** %4
%188 = load i8*, i8** %6
%189 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %187, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i8* %188)
%190 = getelementptr inbounds [1034 x i8], [1034 x i8]* %9, i64 0, i64 0
%191 = load i8*, i8** %6
%192 = call i32 @util_strlcpy(i8* %190, i8* %191, i32 1034)
%193 = getelementptr inbounds [1034 x i8], [1034 x i8]* %9, i64 0, i64 0
%194 = call i32 @util_strlcat(i8* %193, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 1034)
%195 = getelementptr inbounds [1034 x i8], [1034 x i8]* %9, i64 0, i64 0
%196 = call i32 @unlink(i8* %195)
%197 = load %struct.udev*, %struct.udev** %4
%198 = load i8*, i8** %6
%199 = load i32, i32* @S_IFLNK
%200 = call i32 @udev_selinux_setfscreatecon(%struct.udev* %197, i8* %198, i32 %199)
%201 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%202 = getelementptr inbounds [1034 x i8], [1034 x i8]* %9, i64 0, i64 0
%203 = call i32 @symlink(i8* %201, i8* %202)
store i32 %203, i32* %13
%204 = load %struct.udev*, %struct.udev** %4
%205 = call i32 @udev_selinux_resetfscreatecon(%struct.udev* %204)
%206 = load i32, i32* %13
%207 = icmp ne i32 %206, 0
br i1 %207, label %208, label %213
208: ; preds = %186
%209 = load %struct.udev*, %struct.udev** %4
%210 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%211 = getelementptr inbounds [1034 x i8], [1034 x i8]* %9, i64 0, i64 0
%212 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %209, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8* %210, i8* %211)
br label %227
213: ; preds = %186
%214 = getelementptr inbounds [1034 x i8], [1034 x i8]* %9, i64 0, i64 0
%215 = load i8*, i8** %6
%216 = call i32 @rename(i8* %214, i8* %215)
store i32 %216, i32* %13
%217 = load i32, i32* %13
%218 = icmp ne i32 %217, 0
br i1 %218, label %219, label %226
219: ; preds = %213
%220 = load %struct.udev*, %struct.udev** %4
%221 = getelementptr inbounds [1034 x i8], [1034 x i8]* %9, i64 0, i64 0
%222 = load i8*, i8** %6
%223 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %220, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0), i8* %221, i8* %222)
%224 = getelementptr inbounds [1034 x i8], [1034 x i8]* %9, i64 0, i64 0
%225 = call i32 @unlink(i8* %224)
br label %227
226: ; preds = %213
br label %227
227: ; preds = %226, %219, %208, %184, %155, %126
%228 = load i32, i32* %13
ret i32 %228
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.stat = type { i32, i64 }
@.str = [4 x i8] c"../\00"
@.str.1 = [45 x i8] c"found existing node instead of symlink '%s'\0A\00"
@S_IFMT = global i32 0
@.str.2 = [56 x i8] c"replace device node '%s' with symlink to our node '%s'\0A\00"
@.str.3 = [69 x i8] c"device node '%s' already exists, link to '%s' will not overwrite it\0A\00"
@.str.4 = [29 x i8] c"found existing symlink '%s'\0A\00"
@.str.5 = [48 x i8] c"preserve already existing symlink '%s' to '%s'\0A\00"
@S_IFLNK = global i32 0
@.str.6 = [31 x i8] c"creating symlink '%s' to '%s'\0A\00"
@.str.7 = [25 x i8] c"atomically replace '%s'\0A\00"
@.str.8 = [10 x i8] c".udev-tmp\00"
@.str.9 = [28 x i8] c"symlink(%s, %s) failed: %m\0A\00"
@.str.10 = [27 x i8] c"rename(%s, %s) failed: %m\0A\00"
define i32 @node_symlink(%struct.udev* %0, i8* %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = alloca [1034 x i8]
%7 = alloca %struct.stat
%8 = alloca [1024 x i8]
%9 = bitcast %struct.stat* %4 to i8*
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [1034 x i8], [1034 x i8]* %6, i64 0, i64 0
store i8 0, i8* %10
%12 = load i8, i8* %1
%13 = icmp eq i8 %12, 0
br i1 %13, label %33, label %14
14: ; preds = %3, %22
%15 = phi i64 [ %24, %22 ], [ 0, %3 ]
%16 = phi i8 [ %29, %22 ], [ %12, %3 ]
%17 = phi i32 [ %27, %22 ], [ 0, %3 ]
%18 = phi i32 [ %25, %22 ], [ 0, %3 ]
%19 = getelementptr inbounds i8, i8* %2, i64 %15
%20 = load i8, i8* %19
%21 = icmp eq i8 %16, %20
br i1 %21, label %22, label %31
22: ; preds = %14
%23 = icmp eq i8 %16, 47
%24 = add nuw i64 %15, 1
%25 = add nuw nsw i32 %18, 1
%26 = trunc i64 %24 to i32
%27 = select i1 %23, i32 %26, i32 %17
%28 = getelementptr inbounds i8, i8* %1, i64 %24
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %33, label %14
31: ; preds = %14
%32 = trunc i64 %15 to i32
br label %33
33: ; preds = %22, %31, %3
%34 = phi i32 [ 0, %3 ], [ %32, %31 ], [ %25, %22 ]
%35 = phi i32 [ 0, %3 ], [ %17, %31 ], [ %27, %22 ]
%36 = zext i32 %34 to i64
br label %37
37: ; preds = %43, %33
%38 = phi i64 [ %44, %43 ], [ %36, %33 ]
%39 = getelementptr inbounds i8, i8* %2, i64 %38
%40 = load i8, i8* %39
switch i8 %40, label %43 [
i8 0, label %45
i8 47, label %41
]
41: ; preds = %37
%42 = call i32 @util_strlcat(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 1024)
br label %43
43: ; preds = %37, %41
%44 = add nuw i64 %38, 1
br label %37
45: ; preds = %37
%46 = sext i32 %35 to i64
%47 = getelementptr inbounds i8, i8* %1, i64 %46
%48 = call i32 @util_strlcat(i8* %10, i8* %47, i32 1024)
%49 = call i64 @lstat(i8* %2, %struct.stat* %4)
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %100
51: ; preds = %45
%52 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%53 = load i32, i32* %52
%54 = call i64 @S_ISBLK(i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = call i64 @S_ISCHR(i32 %53)
%58 = icmp eq i64 %57, 0
br i1 %58, label %82, label %59
59: ; preds = %56, %51
%60 = bitcast %struct.stat* %7 to i8*
%61 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%62 = call i64 @lstat(i8* %1, %struct.stat* %7)
%63 = icmp eq i64 %62, 0
br i1 %63, label %64, label %79
64: ; preds = %59
%65 = load i32, i32* @S_IFMT
%66 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
%67 = load i32, i32* %66
%68 = xor i32 %67, %53
%69 = and i32 %68, %65
%70 = icmp eq i32 %69, 0
br i1 %70, label %71, label %80
71: ; preds = %64
%72 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%73 = load i64, i64* %72
%74 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 1
%75 = load i64, i64* %74
%76 = icmp eq i64 %73, %75
br i1 %76, label %77, label %80
77: ; preds = %71
%78 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %1)
br label %79
79: ; preds = %77, %59
br label %107
80: ; preds = %64, %71
%81 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.3, i64 0, i64 0), i8* %2, i8* %1)
br label %125
82: ; preds = %56
%83 = call i64 @S_ISLNK(i32 %53)
%84 = icmp eq i64 %83, 0
br i1 %84, label %107, label %85
85: ; preds = %82
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%87 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%88 = call i32 @readlink(i8* %2, i8* %86, i32 1024)
%89 = icmp sgt i32 %88, 0
br i1 %89, label %90, label %95
90: ; preds = %85
%91 = zext i32 %88 to i64
%92 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %91
store i8 0, i8* %92
%93 = call i64 @strcmp(i8* %10, i8* %86)
%94 = icmp eq i64 %93, 0
br i1 %94, label %96, label %95
95: ; preds = %90, %85
br label %107
96: ; preds = %90
%97 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.5, i64 0, i64 0), i8* %2, i8* %10)
%98 = load i32, i32* @S_IFLNK
%99 = call i32 @udev_selinux_lsetfilecon(%struct.udev* %0, i8* %2, i32 %98)
br label %125
100: ; preds = %45
%101 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i8* %2, i8* %10)
%102 = load i32, i32* @S_IFLNK
%103 = call i32 @udev_selinux_setfscreatecon(%struct.udev* %0, i8* %2, i32 %102)
%104 = call i32 @symlink(i8* %10, i8* %2)
%105 = call i32 @udev_selinux_resetfscreatecon(%struct.udev* %0)
%106 = icmp eq i32 %104, 0
br i1 %106, label %125, label %107
107: ; preds = %95, %79, %100, %82
%108 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%109 = call i32 @util_strlcpy(i8* %11, i8* %2, i32 1034)
%110 = call i32 @util_strlcat(i8* %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0), i32 1034)
%111 = call i32 @unlink(i8* %11)
%112 = load i32, i32* @S_IFLNK
%113 = call i32 @udev_selinux_setfscreatecon(%struct.udev* %0, i8* %2, i32 %112)
%114 = call i32 @symlink(i8* %10, i8* %11)
%115 = call i32 @udev_selinux_resetfscreatecon(%struct.udev* %0)
%116 = icmp eq i32 %114, 0
br i1 %116, label %119, label %117
117: ; preds = %107
%118 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), i8* %10, i8* %11)
br label %125
119: ; preds = %107
%120 = call i32 @rename(i8* %11, i8* %2)
%121 = icmp eq i32 %120, 0
br i1 %121, label %125, label %122
122: ; preds = %119
%123 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0), i8* %11, i8* %2)
%124 = call i32 @unlink(i8* %11)
br label %125
125: ; preds = %96, %80, %117, %122, %100, %119
%126 = phi i32 [ 0, %80 ], [ %114, %117 ], [ %120, %122 ], [ 0, %119 ], [ 0, %96 ], [ 0, %100 ]
ret i32 %126
}