#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 /*<<< orphan*/ st_rdev;
};

/* Variables and functions */
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int readlink(char *, char *const, int);
int /*<<< orphan*/ sprintf(char *, char *, int, char const *);
scalar_t__ stat(char *const, struct stat *);
int link_name(char *const buf, int maj, int min, int pid, const char *name) {
  struct stat sbuf;
  char path[32];
  int count;
  sprintf(path, "/proc/%d/%s", pid, name);
  count = readlink(path, buf, (sizeof(long) * 1024) - 1);
  if (count == -1)
    return 0;
  buf[count] = '\0';
  if (stat(buf, &sbuf) < 0)
    return 0;
  if (min != minor(sbuf.st_rdev))
    return 0;
  if (maj != major(sbuf.st_rdev))
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<link_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
lea    0xe85(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x1fff,%edx
call   1030 <readlink@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11ab <link_name+0x5b>
movl   $0x0,-0x4(%rbp)
jmp    122f <link_name+0xdf>
mov    -0x10(%rbp),%rax
movslq -0x54(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    11da <link_name+0x8a>
movl   $0x0,-0x4(%rbp)
jmp    122f <link_name+0xdf>
mov    -0x18(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <minor@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
cmp    %ecx,%eax
je     1201 <link_name+0xb1>
movl   $0x0,-0x4(%rbp)
jmp    122f <link_name+0xdf>
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x30(%rbp),%edi
call   1050 <major@plt>
mov    %eax,%ecx
mov    -0x5c(%rbp),%eax
cmp    %ecx,%eax
je     1228 <link_name+0xd8>
movl   $0x0,-0x4(%rbp)
jmp    122f <link_name+0xdf>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<link_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xe96(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %ecx,%edx
mov    %r8,%rcx
call   1070 <sprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1fff,%edx
call   1030 <readlink@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     11cb <link_name+0x7b>
cltq
movb   $0x0,0x0(%rbp,%rax,1)
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <stat@plt>
test   %rax,%rax
js     11cb <link_name+0x7b>
mov    0x8(%rsp),%edi
call   1040 <minor@plt>
cmp    %r15d,%eax
jne    11cb <link_name+0x7b>
mov    0x8(%rsp),%edi
call   1050 <major@plt>
xor    %ebx,%ebx
cmp    %r14d,%eax
sete   %bl
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [12 x i8] c"/proc/%d/%s\00"
define i32 @link_name(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.stat
%13 = alloca [32 x i8]
%14 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%16 = load i32, i32* %10
%17 = load i8*, i8** %11
%18 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %16, i8* %17)
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%20 = load i8*, i8** %7
%21 = call i32 @readlink(i8* %19, i8* %20, i32 8191)
store i32 %21, i32* %14
%22 = load i32, i32* %14
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %5
store i32 0, i32* %6
br label %49
25: ; preds = %5
%26 = load i8*, i8** %7
%27 = load i32, i32* %14
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %7
%31 = call i64 @stat(i8* %30, %struct.stat* %12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 0, i32* %6
br label %49
34: ; preds = %25
%35 = load i32, i32* %9
%36 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @minor(i32 %37)
%39 = icmp ne i32 %35, %38
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 0, i32* %6
br label %49
41: ; preds = %34
%42 = load i32, i32* %8
%43 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @major(i32 %44)
%46 = icmp ne i32 %42, %45
br i1 %46, label %47, label %48
47: ; preds = %41
store i32 0, i32* %6
br label %49
48: ; preds = %41
store i32 1, i32* %6
br label %49
49: ; preds = %48, %47, %40, %33, %24
%50 = load i32, i32* %6
ret i32 %50
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [12 x i8] c"/proc/%d/%s\00"
define i32 @link_name(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca %struct.stat
%7 = alloca [32 x i8]
%8 = bitcast %struct.stat* %6 to i8*
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%10 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3, i8* %4)
%11 = call i32 @readlink(i8* %9, i8* %0, i32 8191)
%12 = icmp eq i32 %11, -1
br i1 %12, label %28, label %13
13: ; preds = %5
%14 = sext i32 %11 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
store i8 0, i8* %15
%16 = call i64 @stat(i8* %0, %struct.stat* %6)
%17 = icmp slt i64 %16, 0
br i1 %17, label %28, label %18
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @minor(i32 %20)
%22 = icmp eq i32 %21, %2
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* %19
%25 = call i32 @major(i32 %24)
%26 = icmp eq i32 %25, %1
%27 = zext i1 %26 to i32
br label %28
28: ; preds = %23, %18, %13, %5
%29 = phi i32 [ 0, %5 ], [ 0, %13 ], [ 0, %18 ], [ %27, %23 ]
ret i32 %29
}