#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 dirent {
  scalar_t__ d_type;
  char *d_name;
};
typedef int /*<<< orphan*/ linktarget;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ DT_LNK;
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int check_process(char *dirname, char *match) {
  char linkpath[256];
  char linktarget[1024];
  DIR *dirp;
  struct dirent *directory_entry;
  int len;
  strcpy(linkpath, dirname);
  strcat(linkpath, "/fd");
  if ((dirp = opendir(linkpath)) == NULL) {
    return 1;
  }
  do {
    if ((directory_entry = readdir(dirp)) != NULL) {
      if (directory_entry->d_type == DT_LNK) {
        strcpy(linkpath, dirname);
        strcat(linkpath, "/fd/");
        strcat(linkpath, directory_entry->d_name);
        len = readlink(linkpath, linktarget, sizeof(linktarget) - 1);
        if (len > 0) {
          linktarget[len] = '\0';
          if (strcmp(linktarget, match) == 0) {
            return 0;
          }
        }
      }
    }
  } while (directory_entry != NULL);
  return 1;
}


x86 assembly (clang, -O0):
<check_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x540,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x120(%rbp),%rdi
lea    0xe6f(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x120(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x528(%rbp)
cmp    $0x0,%rax
jne    11bf <check_process+0x5f>
movl   $0x1,-0x4(%rbp)
jmp    12be <check_process+0x15e>
jmp    11c4 <check_process+0x64>
mov    -0x528(%rbp),%rdi
call   1070 <readdir@plt>
mov    %rax,-0x530(%rbp)
cmp    $0x0,%rax
je     12a4 <check_process+0x144>
mov    -0x530(%rbp),%rax
mov    (%rax),%rax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
jne    129f <check_process+0x13f>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x120(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x120(%rbp),%rdi
mov    -0x530(%rbp),%rax
mov    0x8(%rax),%rsi
call   1080 <strcat@plt>
lea    -0x120(%rbp),%rdi
lea    -0x520(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x534(%rbp)
cmpl   $0x0,-0x534(%rbp)
jle    129a <check_process+0x13a>
movslq -0x534(%rbp),%rax
movb   $0x0,-0x520(%rbp,%rax,1)
lea    -0x520(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1295 <check_process+0x135>
movl   $0x0,-0x4(%rbp)
jmp    12be <check_process+0x15e>
jmp    129a <check_process+0x13a>
jmp    129f <check_process+0x13f>
jmp    12a4 <check_process+0x144>
jmp    12a9 <check_process+0x149>
cmpq   $0x0,-0x530(%rbp)
jne    11c4 <check_process+0x64>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x540,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_process>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x518,%rsp
mov    %rsi,0x8(%rsp)
mov    %rdi,%r12
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
lea    0xe70(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
call   1050 <opendir@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     1258 <check_process+0xf8>
mov    %rax,%rbp
mov    %rax,%rdi
call   1070 <readdir@plt>
test   %rax,%rax
je     1258 <check_process+0xf8>
mov    %rax,%r13
mov    0x2e14(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0x110(%rsp),%r15
jmp    11f0 <check_process+0x90>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1070 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1253 <check_process+0xf3>
mov    0x0(%r13),%rax
cmp    (%r14),%rax
jne    11e0 <check_process+0x80>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
lea    0xdf6(%rip),%rsi
call   1080 <strcat@plt>
mov    0x8(%r13),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x3ff,%edx
call   1040 <readlink@plt>
test   %eax,%eax
jle    11e0 <check_process+0x80>
mov    %eax,%eax
movb   $0x0,0x110(%rsp,%rax,1)
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11e0 <check_process+0x80>
xor    %ebx,%ebx
jmp    1258 <check_process+0xf8>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x518,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i64, i8* }
@.str = [4 x i8] c"/fd\00"
@DT_LNK = global i64 0
@.str.1 = [5 x i8] c"/fd/\00"
define i32 @check_process(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%12 = load i8*, i8** %4
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%15 = call i32 @strcat(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%17 = call i32* @opendir(i8* %16)
store i32* %17, i32** %8
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 1, i32* %3
br label %64
20: ; preds = %2
br label %21
21: ; preds = %60, %20
%22 = load i32*, i32** %8
%23 = call %struct.dirent* @readdir(i32* %22)
store %struct.dirent* %23, %struct.dirent** %9
%24 = icmp ne %struct.dirent* %23, null
br i1 %24, label %25, label %59
25: ; preds = %21
%26 = load %struct.dirent*, %struct.dirent** %9
%27 = getelementptr inbounds %struct.dirent, %struct.dirent* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = load i64, i64* @DT_LNK
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %58
31: ; preds = %25
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%33 = load i8*, i8** %4
%34 = call i32 @strcpy(i8* %32, i8* %33)
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%36 = call i32 @strcat(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%38 = load %struct.dirent*, %struct.dirent** %9
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i32 0, i32 1
%40 = load i8*, i8** %39
%41 = call i32 @strcat(i8* %37, i8* %40)
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%44 = call i32 @readlink(i8* %42, i8* %43, i32 1023)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %57
47: ; preds = %31
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %49
store i8 0, i8* %50
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%52 = load i8*, i8** %5
%53 = call i64 @strcmp(i8* %51, i8* %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %47
store i32 0, i32* %3
br label %64
56: ; preds = %47
br label %57
57: ; preds = %56, %31
br label %58
58: ; preds = %57, %25
br label %59
59: ; preds = %58, %21
br label %60
60: ; preds = %59
%61 = load %struct.dirent*, %struct.dirent** %9
%62 = icmp ne %struct.dirent* %61, null
br i1 %62, label %21, label %63
63: ; preds = %60
store i32 1, i32* %3
br label %64
64: ; preds = %63, %55, %19
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.dirent = type { i64, i8* }
@.str = [4 x i8] c"/fd\00"
@DT_LNK = global i64 0
@.str.1 = [5 x i8] c"/fd/\00"
define i32 @check_process(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = call i32 @strcpy(i8* %5, i8* %0)
%8 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%9 = call i32* @opendir(i8* %5)
%10 = icmp eq i32* %9, null
br i1 %10, label %36, label %11
11: ; preds = %2
%12 = call %struct.dirent* @readdir(i32* %9)
%13 = icmp eq %struct.dirent* %12, null
br i1 %13, label %36, label %14
14: ; preds = %11, %33
%15 = phi %struct.dirent* [ %34, %33 ], [ %12, %11 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 0
%17 = load i64, i64* %16
%18 = load i64, i64* @DT_LNK
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %33
20: ; preds = %14
%21 = call i32 @strcpy(i8* %5, i8* %0)
%22 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %15, i64 0, i32 1
%24 = load i8*, i8** %23
%25 = call i32 @strcat(i8* %5, i8* %24)
%26 = call i32 @readlink(i8* %5, i8* %6, i32 1023)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %20
%29 = zext i32 %26 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i64 @strcmp(i8* %6, i8* %1)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %14, %20, %28
%34 = call %struct.dirent* @readdir(i32* %9)
%35 = icmp eq %struct.dirent* %34, null
br i1 %35, label %36, label %14
36: ; preds = %28, %33, %11, %2
%37 = phi i32 [ 1, %2 ], [ 1, %11 ], [ 0, %28 ], [ 1, %33 ]
ret i32 %37
}