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

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ linkname_from_minor(char *, int);
int /*<<< orphan*/ perror(char *);
int readlink(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *lookup_minor(int minor) {
  static char buf[255];
  static char resolved_path[1024 + 1];
  struct stat stat_buf;
  ssize_t len;
  if (minor >= 256 || minor < 0) {
    fprintf(stderr, "register_minor: minor too big (%d).\n", minor);
    return NULL;
  }
  linkname_from_minor(buf, minor);
  if (stat(buf, &stat_buf) < 0) {
    if (errno != ENOENT)
      fprintf(stderr, "stat(%s): %m\n", buf);
    return NULL;
  }
  len = readlink(buf, resolved_path, sizeof(resolved_path) - 1);
  if (len < 0) {
    perror("readlink");
    return NULL;
  }
  if (len >= 1024)
    fprintf(stderr, "readlink(%s): result has probably been truncated\n", buf);
  resolved_path[len] = '\0';
  return resolved_path;
}


x86 assembly (clang, -O0):
<lookup_minor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
cmpl   $0x100,-0xc(%rbp)
jge    1172 <lookup_minor+0x22>
cmpl   $0x0,-0xc(%rbp)
jge    1199 <lookup_minor+0x49>
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xe7b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    128a <lookup_minor+0x13a>
mov    -0xc(%rbp),%esi
lea    0x2ebd(%rip),%rdi
call   1030 <linkname_from_minor@plt>
lea    0x2eb1(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1207 <lookup_minor+0xb7>
mov    0x2e0f(%rip),%rax
mov    (%rax),%rax
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%rax
je     11fa <lookup_minor+0xaa>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
lea    0x2e6d(%rip),%rdx
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    128a <lookup_minor+0x13a>
lea    0x2e52(%rip),%rdi
lea    0x2f4b(%rip),%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1245 <lookup_minor+0xf5>
lea    0xe00(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    128a <lookup_minor+0x13a>
cmpl   $0x400,-0x14(%rbp)
jl     1270 <lookup_minor+0x120>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdda(%rip),%rsi
lea    0x2df7(%rip),%rdx
mov    $0x0,%al
call   1050 <fprintf@plt>
movslq -0x14(%rbp),%rcx
lea    0x2ee5(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2eda(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_minor>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%edx
cmp    $0x100,%edi
jb     117b <lookup_minor+0x2b>
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xe93(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1224 <lookup_minor+0xd4>
lea    0x2ede(%rip),%rbx
mov    %rbx,%rdi
mov    %edx,%esi
call   1030 <linkname_from_minor@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
js     11ea <lookup_minor+0x9a>
lea    0x2ebd(%rip),%rdi
lea    0x2fb6(%rip),%rbx
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
test   %eax,%eax
js     122e <lookup_minor+0xde>
mov    %eax,%ebp
cmp    $0x400,%eax
jb     11e2 <lookup_minor+0x92>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe68(%rip),%rsi
lea    0x2e85(%rip),%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%eax
movb   $0x0,(%rax,%rbx,1)
jmp    1224 <lookup_minor+0xd4>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%rax
jne    1204 <lookup_minor+0xb4>
xor    %ebx,%ebx
jmp    1224 <lookup_minor+0xd4>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
lea    0x2e45(%rip),%rdx
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
xor    %ebx,%ebx
jmp    1224 <lookup_minor+0xd4>

LLVM IR (-O0):
%struct.stat = type { i32 }
@lookup_minor.buf = internal global [255 x i8] zeroinitializer
@lookup_minor.resolved_path = internal global [1025 x i8] zeroinitializer
@stderr = global i32 0
@.str = [37 x i8] c"register_minor: minor too big (%d).\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [14 x i8] c"stat(%s): %m\0A\00"
@.str.2 = [9 x i8] c"readlink\00"
@.str.3 = [50 x i8] c"readlink(%s): result has probably been truncated\0A\00"
define i8* @lookup_minor(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = icmp sge i32 %6, 256
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8, %1
%12 = load i32, i32* @stderr
%13 = load i32, i32* %3
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %13)
store i8* null, i8** %2
br label %44
15: ; preds = %8
%16 = load i32, i32* %3
%17 = call i32 @linkname_from_minor(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0), i32 %16)
%18 = call i64 @stat(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0), %struct.stat* %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0))
br label %27
27: ; preds = %24, %20
store i8* null, i8** %2
br label %44
28: ; preds = %15
%29 = call i32 @readlink(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0), i8* getelementptr inbounds ([1025 x i8], [1025 x i8]* @lookup_minor.resolved_path, i64 0, i64 0), i32 1024)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %44
34: ; preds = %28
%35 = load i32, i32* %5
%36 = icmp sge i32 %35, 1024
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0))
br label %40
40: ; preds = %37, %34
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1025 x i8], [1025 x i8]* @lookup_minor.resolved_path, i64 0, i64 %42
store i8 0, i8* %43
store i8* getelementptr inbounds ([1025 x i8], [1025 x i8]* @lookup_minor.resolved_path, i64 0, i64 0), i8** %2
br label %44
44: ; preds = %40, %32, %27, %11
%45 = load i8*, i8** %2
ret i8* %45
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@lookup_minor.buf = internal global [255 x i8] zeroinitializer
@lookup_minor.resolved_path = internal global [1025 x i8] zeroinitializer
@stderr = global i32 0
@.str = [37 x i8] c"register_minor: minor too big (%d).\0A\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [14 x i8] c"stat(%s): %m\0A\00"
@.str.2 = [9 x i8] c"readlink\00"
@.str.3 = [50 x i8] c"readlink(%s): result has probably been truncated\0A\00"
define i8* @lookup_minor(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp ugt i32 %0, 255
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %32
8: ; preds = %1
%9 = tail call i32 @linkname_from_minor(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0), i32 %0)
%10 = call i64 @stat(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0), %struct.stat* %2)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %8
%13 = load i64, i64* @errno
%14 = load i64, i64* @ENOENT
%15 = icmp eq i64 %13, %14
br i1 %15, label %32, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0))
br label %32
19: ; preds = %8
%20 = call i32 @readlink(i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0), i8* getelementptr inbounds ([1025 x i8], [1025 x i8]* @lookup_minor.resolved_path, i64 0, i64 0), i32 1024)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %32
24: ; preds = %19
%25 = icmp ugt i32 %20, 1023
br i1 %25, label %26, label %29
26: ; preds = %24
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([255 x i8], [255 x i8]* @lookup_minor.buf, i64 0, i64 0))
br label %29
29: ; preds = %26, %24
%30 = zext i32 %20 to i64
%31 = getelementptr inbounds [1025 x i8], [1025 x i8]* @lookup_minor.resolved_path, i64 0, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %12, %16, %29, %22, %5
%33 = phi i8* [ null, %5 ], [ null, %22 ], [ getelementptr inbounds ([1025 x i8], [1025 x i8]* @lookup_minor.resolved_path, i64 0, i64 0), %29 ], [ null, %16 ], [ null, %12 ]
ret i8* %33
}