#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 {
  scalar_t__ st_rdev;
};

/* Variables and functions */
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 *restrict const buf, unsigned maj, unsigned min, int pid,
              const char *restrict name) {
  struct stat sbuf;
  char path[32];
  int count;
  sprintf(path, "/proc/%d/%s", pid, name);
  count = readlink(path, buf, 128 - 1);
  if (count == -1)
    return 0;
  buf[count] = '\0';
  if (stat(buf, &sbuf) < 0)
    return 0;
  if (min != (((unsigned)(sbuf.st_rdev) & 0xffu) |
              (((unsigned)(sbuf.st_rdev) & 0xfff00000u) >> 12u)))
    return 0;
  if (maj != (((unsigned)(sbuf.st_rdev) >> 8u) & 0xfffu))
    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    0xea5(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x7f,%edx
call   1030 <readlink@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    118b <link_name+0x5b>
movl   $0x0,-0x4(%rbp)
jmp    1215 <link_name+0xe5>
mov    -0x10(%rbp),%rax
movslq -0x54(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11ba <link_name+0x8a>
movl   $0x0,-0x4(%rbp)
jmp    1215 <link_name+0xe5>
mov    -0x18(%rbp),%eax
mov    -0x30(%rbp),%rcx
and    $0xff,%ecx
mov    -0x30(%rbp),%rdx
and    $0xfff00000,%edx
shr    $0xc,%edx
or     %edx,%ecx
cmp    %ecx,%eax
je     11ea <link_name+0xba>
movl   $0x0,-0x4(%rbp)
jmp    1215 <link_name+0xe5>
mov    -0x14(%rbp),%eax
mov    -0x30(%rbp),%rcx
shr    $0x8,%ecx
and    $0xfff,%ecx
cmp    %ecx,%eax
je     120e <link_name+0xde>
movl   $0x0,-0x4(%rbp)
jmp    1215 <link_name+0xe5>
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    0xeb6(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %ecx,%edx
mov    %r8,%rcx
call   1050 <sprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x7f,%edx
call   1030 <readlink@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     11b5 <link_name+0x85>
cltq
movb   $0x0,0x0(%rbp,%rax,1)
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1040 <stat@plt>
test   %rax,%rax
js     11b5 <link_name+0x85>
mov    0x8(%rsp),%eax
movzbl %al,%ecx
mov    %eax,%edx
shr    $0xc,%edx
and    $0xfff00,%edx
or     %ecx,%edx
cmp    %r15d,%edx
jne    11b5 <link_name+0x85>
shr    $0x8,%eax
and    $0xfff,%eax
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 { i64 }
@.str = [12 x i8] c"/proc/%d/%s\00"
define i32 @link_name(i8* noalias %0, i32 %1, i32 %2, i32 %3, i8* noalias %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 127)
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 %58
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 %58
34: ; preds = %25
%35 = load i32, i32* %9
%36 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%37 = load i64, i64* %36
%38 = trunc i64 %37 to i32
%39 = and i32 %38, 255
%40 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%41 = load i64, i64* %40
%42 = trunc i64 %41 to i32
%43 = and i32 %42, -1048576
%44 = lshr i32 %43, 12
%45 = or i32 %39, %44
%46 = icmp ne i32 %35, %45
br i1 %46, label %47, label %48
47: ; preds = %34
store i32 0, i32* %6
br label %58
48: ; preds = %34
%49 = load i32, i32* %8
%50 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%51 = load i64, i64* %50
%52 = trunc i64 %51 to i32
%53 = lshr i32 %52, 8
%54 = and i32 %53, 4095
%55 = icmp ne i32 %49, %54
br i1 %55, label %56, label %57
56: ; preds = %48
store i32 0, i32* %6
br label %58
57: ; preds = %48
store i32 1, i32* %6
br label %58
58: ; preds = %57, %56, %47, %33, %24
%59 = load i32, i32* %6
ret i32 %59
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [12 x i8] c"/proc/%d/%s\00"
define i32 @link_name(i8* noalias %0, i32 %1, i32 %2, i32 %3, i8* noalias %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 127)
%12 = icmp eq i32 %11, -1
br i1 %12, label %32, 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 %32, label %18
18: ; preds = %13
%19 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%20 = load i64, i64* %19
%21 = trunc i64 %20 to i32
%22 = and i32 %21, 255
%23 = lshr i32 %21, 12
%24 = and i32 %23, 1048320
%25 = or i32 %24, %22
%26 = icmp eq i32 %25, %2
br i1 %26, label %27, label %32
27: ; preds = %18
%28 = lshr i32 %21, 8
%29 = and i32 %28, 4095
%30 = icmp eq i32 %29, %1
%31 = zext i1 %30 to i32
br label %32
32: ; preds = %27, %18, %13, %5
%33 = phi i32 [ 0, %5 ], [ 0, %13 ], [ 0, %18 ], [ %31, %27 ]
ret i32 %33
}