#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 stat {
  int dummy;
};

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ linkname_from_minor(char *, int);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
scalar_t__ symlink(char const *, char *);
scalar_t__ unlink(char *);
int register_minor(int minor, const char *path) {
  char buf[255];
  struct stat stat_buf;
  int err = -1;
  if (minor >= 256 || minor < 0) {
    fprintf(stderr, "register_minor: minor too big (%d).\n", minor);
    return -1;
  }
  linkname_from_minor(buf, minor);
  if (!path || !path[0])
    fprintf(stderr, "Cannot register an empty path.\n");
  else if (path[0] != '/')
    fprintf(stderr,
            "Absolute path expected, "
            "won't register relative path (%s).\n",
            path);
  else if (strlen(path) >= 1024)
    fprintf(stderr,
            "path (%s):\ntoo long to be registered, "
            "max path len supported: %u\n",
            path, 1024 - 1);
  else if (stat(path, &stat_buf) < 0)
    fprintf(stderr, "stat(%s): %m\n", path);
  else if (unlink(buf) < 0 && errno != ENOENT)
    fprintf(stderr, "unlink(%s): %m\n", buf);
  else if (symlink(path, buf) < 0)
    fprintf(stderr, "symlink(%s, %s): %m\n", path, buf);
  else
    err = 0;
  return err;
}


x86 assembly (clang, -O0):
<register_minor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x11c(%rbp)
ff ff ff
cmpl   $0x100,-0x8(%rbp)
jge    1193 <register_minor+0x33>
cmpl   $0x0,-0x8(%rbp)
jge    11b9 <register_minor+0x59>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <register_minor+0x1ff>
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1040 <linkname_from_minor@plt>
cmpq   $0x0,-0x10(%rbp)
je     11e0 <register_minor+0x80>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11fc <register_minor+0x9c>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1356 <register_minor+0x1f6>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     122c <register_minor+0xcc>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1351 <register_minor+0x1f1>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x400,%eax
jl     1265 <register_minor+0x105>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2d(%rip),%rsi
mov    $0x3ff,%ecx
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    134c <register_minor+0x1ec>
mov    -0x10(%rbp),%rdi
lea    -0x118(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    129f <register_minor+0x13f>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1347 <register_minor+0x1e7>
lea    -0x110(%rbp),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    12f2 <register_minor+0x192>
mov    0x2d1c(%rip),%rax
mov    (%rax),%rax
mov    0x2d02(%rip),%rcx
cmp    (%rcx),%rax
je     12f2 <register_minor+0x192>
mov    0x2d0a(%rip),%rax
mov    (%rax),%edi
lea    -0x110(%rbp),%rdx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1342 <register_minor+0x1e2>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rsi
call   1060 <symlink@plt>
cmp    $0x0,%rax
jge    1333 <register_minor+0x1d3>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    -0x110(%rbp),%rcx
lea    0xdba(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    133d <register_minor+0x1dd>
movl   $0x0,-0x11c(%rbp)
jmp    1342 <register_minor+0x1e2>
jmp    1347 <register_minor+0x1e7>
jmp    134c <register_minor+0x1ec>
jmp    1351 <register_minor+0x1f1>
jmp    1356 <register_minor+0x1f6>
mov    -0x11c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<register_minor>:
push   %rbx
sub    $0x110,%rsp
mov    %edi,%edx
cmp    $0x100,%edi
jb     118e <register_minor+0x2e>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    120f <register_minor+0xaf>
mov    %rsi,%rbx
lea    0x10(%rsp),%rdi
mov    %edx,%esi
call   1040 <linkname_from_minor@plt>
test   %rbx,%rbx
je     11ac <register_minor+0x4c>
mov    (%rbx),%al
cmp    $0x2f,%al
je     11c5 <register_minor+0x65>
test   %al,%al
jne    11f5 <register_minor+0x95>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    120f <register_minor+0xaf>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmp    $0x400,%eax
jl     121d <register_minor+0xbd>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe9d(%rip),%rsi
mov    %rbx,%rdx
mov    $0x3ff,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    120f <register_minor+0xaf>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x110,%rsp
pop    %rbx
ret
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
js     128c <register_minor+0x12c>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    1254 <register_minor+0xf4>
mov    0x2d93(%rip),%rax
mov    (%rax),%rax
mov    0x2d79(%rip),%rcx
cmp    (%rcx),%rax
jne    12a1 <register_minor+0x141>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <symlink@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    1214 <register_minor+0xb4>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
lea    0xe66(%rip),%rsi
lea    0x10(%rsp),%rcx
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    120f <register_minor+0xaf>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
jmp    1205 <register_minor+0xa5>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
lea    0x10(%rsp),%rdx
jmp    1208 <register_minor+0xa8>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [37 x i8] c"register_minor: minor too big (%d).\0A\00"
@.str.1 = [32 x i8] c"Cannot register an empty path.\0A\00"
@.str.2 = [60 x i8] c"Absolute path expected, won't register relative path (%s).\0A\00"
@.str.3 = [66 x i8] c"path (%s):\0Atoo long to be registered, max path len supported: %u\0A\00"
@.str.4 = [14 x i8] c"stat(%s): %m\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [16 x i8] c"unlink(%s): %m\0A\00"
@.str.6 = [21 x i8] c"symlink(%s, %s): %m\0A\00"
define i32 @register_minor(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [255 x i8]
%7 = alloca %struct.stat
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 -1, i32* %8
%9 = load i32, i32* %4
%10 = icmp sge i32 %9, 256
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %11, %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* %4
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 -1, i32* %3
br label %88
18: ; preds = %11
%19 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%20 = load i32, i32* %4
%21 = call i32 @linkname_from_minor(i8* %19, i32 %20)
%22 = load i8*, i8** %5
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
%28 = icmp ne i8 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %24, %18
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %86
32: ; preds = %24
%33 = load i8*, i8** %5
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 47
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %5
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %85
42: ; preds = %32
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
%45 = icmp sge i32 %44, 1024
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %5
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i8* %48, i32 1023)
br label %84
50: ; preds = %42
%51 = load i8*, i8** %5
%52 = call i64 @stat(i8* %51, %struct.stat* %7)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = load i8*, i8** %5
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %56)
br label %83
58: ; preds = %50
%59 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%60 = call i64 @unlink(i8* %59)
%61 = icmp slt i64 %60, 0
br i1 %61, label %62, label %70
62: ; preds = %58
%63 = load i64, i64* @errno
%64 = load i64, i64* @ENOENT
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %70
66: ; preds = %62
%67 = load i32, i32* @stderr
%68 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %68)
br label %82
70: ; preds = %62, %58
%71 = load i8*, i8** %5
%72 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%73 = call i64 @symlink(i8* %71, i8* %72)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %80
75: ; preds = %70
%76 = load i32, i32* @stderr
%77 = load i8*, i8** %5
%78 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %77, i8* %78)
br label %81
80: ; preds = %70
store i32 0, i32* %8
br label %81
81: ; preds = %80, %75
br label %82
82: ; preds = %81, %66
br label %83
83: ; preds = %82, %54
br label %84
84: ; preds = %83, %46
br label %85
85: ; preds = %84, %38
br label %86
86: ; preds = %85, %29
%87 = load i32, i32* %8
store i32 %87, i32* %3
br label %88
88: ; preds = %86, %14
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [37 x i8] c"register_minor: minor too big (%d).\0A\00"
@.str.1 = [32 x i8] c"Cannot register an empty path.\0A\00"
@.str.2 = [60 x i8] c"Absolute path expected, won't register relative path (%s).\0A\00"
@.str.3 = [66 x i8] c"path (%s):\0Atoo long to be registered, max path len supported: %u\0A\00"
@.str.4 = [14 x i8] c"stat(%s): %m\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [16 x i8] c"unlink(%s): %m\0A\00"
@.str.6 = [21 x i8] c"symlink(%s, %s): %m\0A\00"
define i32 @register_minor(i32 %0, i8* %1) {
%3 = alloca [255 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp ugt i32 %0, 255
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %50
11: ; preds = %2
%12 = call i32 @linkname_from_minor(i8* %5, i32 %0)
%13 = icmp eq i8* %1, null
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = load i8, i8* %1
switch i8 %15, label %19 [
i8 0, label %16
i8 47, label %22
]
16: ; preds = %14, %11
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %50
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %50
22: ; preds = %14
%23 = call i32 @strlen(i8* %1)
%24 = icmp sgt i32 %23, 1023
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.3, i64 0, i64 0), i8* %1, i32 1023)
br label %50
28: ; preds = %22
%29 = call i64 @stat(i8* %1, %struct.stat* %4)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %1)
br label %50
34: ; preds = %28
%35 = call i64 @unlink(i8* %5)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i64, i64* @errno
%39 = load i64, i64* @ENOENT
%40 = icmp eq i64 %38, %39
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = load i32, i32* @stderr
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* %5)
br label %50
44: ; preds = %37, %34
%45 = call i64 @symlink(i8* %1, i8* %5)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i8* %1, i8* %5)
br label %50
50: ; preds = %16, %25, %41, %47, %31, %19, %44, %8
%51 = phi i32 [ -1, %8 ], [ -1, %19 ], [ -1, %25 ], [ -1, %31 ], [ -1, %41 ], [ -1, %47 ], [ -1, %16 ], [ 0, %44 ]
ret i32 %51
}