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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int read(int, int /*<<< orphan*/ *, int);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncmp(char *, char *, int);
int cpu_drcindex_to_interruptserver(uint32_t drc_idx, uint32_t *int_servs,
                                    int array_elements) {
  DIR *dir;
  struct dirent *entry;
  int fd, found = 0;
  char buffer[1024];
  uint32_t temp;
  dir = opendir("/proc/device-tree/cpus");
  while ((entry = readdir(dir)) != NULL) {
    if (!strncmp(entry->d_name, "PowerPC,POWER", 13)) {
      snprintf(buffer, 1024,
               "/proc/device-tree/cpus/%s/"
               "ibm,my-drc-index",
               entry->d_name);
      if ((fd = open(buffer, O_RDONLY)) < 0) {
        fprintf(stderr, "error opening %s\n", buffer);
        closedir(dir);
        return 0;
      }
      while ((read(fd, &temp, 4)) != 0) {
        if (temp == drc_idx) {
          close(fd);
          snprintf(buffer, 1024,
                   "/proc/device-"
                   "tree/cpus/%s/"
                   "ibm,ppc-interrupt-server#s",
                   entry->d_name);
          if ((fd = open(buffer, O_RDONLY)) < 0) {
            fprintf(stderr,
                    "error opening"
                    " %s\n",
                    buffer);
            closedir(dir);
            return 0;
          }
          while ((read(fd, &temp, 4)) == 4) {
            if (found <= array_elements)
              int_servs[found] = temp;
            found++;
          }
          break;
        }
      }
      close(fd);
    }
  }
  return found;
}


x86 assembly (clang, -O0):
<cpu_drcindex_to_interruptserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x30(%rbp)
lea    0xe4d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1373 <cpu_drcindex_to_interruptserver+0x1e3>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
lea    0xe36(%rip),%rsi
mov    $0xd,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    136e <cpu_drcindex_to_interruptserver+0x1de>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
mov    $0x400,%esi
lea    0xe17(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1265 <cpu_drcindex_to_interruptserver+0xd5>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    -0x430(%rbp),%rdx
lea    0xe05(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    1379 <cpu_drcindex_to_interruptserver+0x1e9>
jmp    126a <cpu_drcindex_to_interruptserver+0xda>
mov    -0x2c(%rbp),%edi
lea    -0x434(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x0,%eax
je     1366 <cpu_drcindex_to_interruptserver+0x1d6>
mov    -0x434(%rbp),%eax
cmp    -0x8(%rbp),%eax
jne    1361 <cpu_drcindex_to_interruptserver+0x1d1>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%rcx
mov    $0x400,%esi
lea    0xdaa(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    130f <cpu_drcindex_to_interruptserver+0x17f>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
lea    -0x430(%rbp),%rdx
lea    0xd5b(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <closedir@plt>
movl   $0x0,-0x4(%rbp)
jmp    1379 <cpu_drcindex_to_interruptserver+0x1e9>
jmp    1314 <cpu_drcindex_to_interruptserver+0x184>
mov    -0x2c(%rbp),%edi
lea    -0x434(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
jne    135c <cpu_drcindex_to_interruptserver+0x1cc>
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     134e <cpu_drcindex_to_interruptserver+0x1be>
mov    -0x434(%rbp),%edx
mov    -0x10(%rbp),%rax
movslq -0x30(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1314 <cpu_drcindex_to_interruptserver+0x184>
jmp    1366 <cpu_drcindex_to_interruptserver+0x1d6>
jmp    126a <cpu_drcindex_to_interruptserver+0xda>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
jmp    11bc <cpu_drcindex_to_interruptserver+0x2c>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_drcindex_to_interruptserver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
mov    %edx,%ebx
mov    %rsi,0x18(%rsp)
mov    %edi,%r15d
lea    0xe4e(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     1351 <cpu_drcindex_to_interruptserver+0x1c1>
mov    %rax,%r13
movslq %ebx,%rax
mov    %rax,0x10(%rsp)
xor    %ebx,%ebx
lea    0xe38(%rip),%rbp
lea    0x4(%rsp),%r12
mov    %r14,0x8(%rsp)
jmp    1213 <cpu_drcindex_to_interruptserver+0x83>
lea    0xe25(%rip),%rbp
mov    %r14d,%edi
call   1060 <close@plt>
mov    0x8(%rsp),%r14
mov    %r14,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r13
test   %rax,%rax
je     1353 <cpu_drcindex_to_interruptserver+0x1c3>
mov    0x0(%r13),%rdi
mov    %rbp,%rsi
mov    $0xd,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11ff <cpu_drcindex_to_interruptserver+0x6f>
mov    0x0(%r13),%rcx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
lea    0xde5(%rip),%rdx
call   1050 <snprintf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     132d <cpu_drcindex_to_interruptserver+0x19d>
mov    %eax,%r14d
lea    0xdaf(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
test   %eax,%eax
je     11f2 <cpu_drcindex_to_interruptserver+0x62>
cmp    %r15d,0x4(%rsp)
jne    1270 <cpu_drcindex_to_interruptserver+0xe0>
mov    %r14d,%edi
call   1060 <close@plt>
mov    0x0(%r13),%rcx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
lea    0xdb3(%rip),%rdx
call   1050 <snprintf@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     132d <cpu_drcindex_to_interruptserver+0x19d>
mov    %eax,%r14d
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
jne    11eb <cpu_drcindex_to_interruptserver+0x5b>
movslq %ebx,%rbx
mov    0x18(%rsp),%r13
mov    0x10(%rsp),%rbp
jmp    131d <cpu_drcindex_to_interruptserver+0x18d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
jne    11eb <cpu_drcindex_to_interruptserver+0x5b>
cmp    %rbp,%rbx
jg     1300 <cpu_drcindex_to_interruptserver+0x170>
mov    0x4(%rsp),%eax
mov    %eax,0x0(%r13,%rbx,4)
jmp    1300 <cpu_drcindex_to_interruptserver+0x170>
mov    0x2ca4(%rip),%rax
mov    (%rax),%edi
lea    0xd13(%rip),%rsi
lea    0x20(%rsp),%rdx
call   1090 <fprintf@plt>
mov    0x8(%rsp),%rdi
call   1070 <closedir@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [23 x i8] c"/proc/device-tree/cpus\00"
@.str.1 = [14 x i8] c"PowerPC,POWER\00"
@.str.2 = [43 x i8] c"/proc/device-tree/cpus/%s/ibm,my-drc-index\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [18 x i8] c"error opening %s\0A\00"
@.str.4 = [53 x i8] c"/proc/device-tree/cpus/%s/ibm,ppc-interrupt-server#s\00"
define i32 @cpu_drcindex_to_interruptserver(i32 %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca %struct.dirent*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [1024 x i8]
%13 = alloca i32
store i32 %0, i32* %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 0, i32* %11
%14 = call i32* @opendir(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
br label %15
15: ; preds = %91, %3
%16 = load i32*, i32** %8
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %9
%18 = icmp ne %struct.dirent* %17, null
br i1 %18, label %19, label %92
19: ; preds = %15
%20 = load %struct.dirent*, %struct.dirent** %9
%21 = getelementptr inbounds %struct.dirent, %struct.dirent* %20, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strncmp(i8* %22, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 13)
%24 = icmp ne i32 %23, 0
br i1 %24, label %91, label %25
25: ; preds = %19
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%27 = load %struct.dirent*, %struct.dirent** %9
%28 = getelementptr inbounds %struct.dirent, %struct.dirent* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @snprintf(i8* %26, i32 1024, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %10
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %25
%36 = load i32, i32* @stderr
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%39 = load i32*, i32** %8
%40 = call i32 @closedir(i32* %39)
store i32 0, i32* %4
br label %94
41: ; preds = %25
br label %42
42: ; preds = %87, %41
%43 = load i32, i32* %10
%44 = call i32 @read(i32 %43, i32* %13, i32 4)
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %88
46: ; preds = %42
%47 = load i32, i32* %13
%48 = load i32, i32* %5
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %87
50: ; preds = %46
%51 = load i32, i32* %10
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%54 = load %struct.dirent*, %struct.dirent** %9
%55 = getelementptr inbounds %struct.dirent, %struct.dirent* %54, i32 0, i32 0
%56 = load i8*, i8** %55
%57 = call i32 @snprintf(i8* %53, i32 1024, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8* %56)
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%59 = load i32, i32* @O_RDONLY
%60 = call i32 @open(i8* %58, i32 %59)
store i32 %60, i32* %10
%61 = icmp slt i32 %60, 0
br i1 %61, label %62, label %68
62: ; preds = %50
%63 = load i32, i32* @stderr
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%65 = call i32 @fprintf(i32 %63, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = load i32*, i32** %8
%67 = call i32 @closedir(i32* %66)
store i32 0, i32* %4
br label %94
68: ; preds = %50
br label %69
69: ; preds = %83, %68
%70 = load i32, i32* %10
%71 = call i32 @read(i32 %70, i32* %13, i32 4)
%72 = icmp eq i32 %71, 4
br i1 %72, label %73, label %86
73: ; preds = %69
%74 = load i32, i32* %11
%75 = load i32, i32* %7
%76 = icmp sle i32 %74, %75
br i1 %76, label %77, label %83
77: ; preds = %73
%78 = load i32, i32* %13
%79 = load i32*, i32** %6
%80 = load i32, i32* %11
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i32, i32* %79, i64 %81
store i32 %78, i32* %82
br label %83
83: ; preds = %77, %73
%84 = load i32, i32* %11
%85 = add nsw i32 %84, 1
store i32 %85, i32* %11
br label %69
86: ; preds = %69
br label %88
87: ; preds = %46
br label %42
88: ; preds = %86, %42
%89 = load i32, i32* %10
%90 = call i32 @close(i32 %89)
br label %91
91: ; preds = %88, %19
br label %15
92: ; preds = %15
%93 = load i32, i32* %11
store i32 %93, i32* %4
br label %94
94: ; preds = %92, %62, %35
%95 = load i32, i32* %4
ret i32 %95
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [23 x i8] c"/proc/device-tree/cpus\00"
@.str.1 = [14 x i8] c"PowerPC,POWER\00"
@.str.2 = [43 x i8] c"/proc/device-tree/cpus/%s/ibm,my-drc-index\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [18 x i8] c"error opening %s\0A\00"
@.str.4 = [53 x i8] c"/proc/device-tree/cpus/%s/ibm,ppc-interrupt-server#s\00"
define i32 @cpu_drcindex_to_interruptserver(i32 %0, i32* nocapture writeonly %1, i32 %2) {
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = bitcast i32* %5 to i8*
%8 = tail call i32* @opendir(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%9 = call %struct.dirent* @readdir(i32* %8)
%10 = icmp eq %struct.dirent* %9, null
br i1 %10, label %68, label %11
11: ; preds = %3
%12 = sext i32 %2 to i64
br label %13
13: ; preds = %11, %60
%14 = phi %struct.dirent* [ %62, %60 ], [ %9, %11 ]
%15 = phi i32 [ %61, %60 ], [ 0, %11 ]
%16 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%17 = load i8*, i8** %16
%18 = call i32 @strncmp(i8* %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 13)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %60
20: ; preds = %13
%21 = load i8*, i8** %16
%22 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @O_RDONLY
%24 = call i32 @open(i8* %6, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %64, label %26
26: ; preds = %20, %29
%27 = call i32 @read(i32 %24, i32* %5, i32 4)
%28 = icmp eq i32 %27, 0
br i1 %28, label %56, label %29
29: ; preds = %26
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, %0
br i1 %31, label %32, label %26
32: ; preds = %29
%33 = call i32 @close(i32 %24)
%34 = load i8*, i8** %16
%35 = call i32 @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i8* %34)
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* %6, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %64, label %39
39: ; preds = %32
%40 = call i32 @read(i32 %37, i32* %5, i32 4)
%41 = icmp eq i32 %40, 4
br i1 %41, label %42, label %56
42: ; preds = %39
%43 = sext i32 %15 to i64
br label %44
44: ; preds = %42, %50
%45 = phi i64 [ %43, %42 ], [ %51, %50 ]
%46 = icmp sgt i64 %45, %12
br i1 %46, label %50, label %47
47: ; preds = %44
%48 = load i32, i32* %5
%49 = getelementptr inbounds i32, i32* %1, i64 %45
store i32 %48, i32* %49
br label %50
50: ; preds = %47, %44
%51 = add i64 %45, 1
%52 = call i32 @read(i32 %37, i32* %5, i32 4)
%53 = icmp eq i32 %52, 4
br i1 %53, label %44, label %54
54: ; preds = %50
%55 = trunc i64 %51 to i32
br label %56
56: ; preds = %26, %54, %39
%57 = phi i32 [ %37, %39 ], [ %37, %54 ], [ %24, %26 ]
%58 = phi i32 [ %15, %39 ], [ %55, %54 ], [ %15, %26 ]
%59 = call i32 @close(i32 %57)
br label %60
60: ; preds = %56, %13
%61 = phi i32 [ %15, %13 ], [ %58, %56 ]
%62 = call %struct.dirent* @readdir(i32* %8)
%63 = icmp eq %struct.dirent* %62, null
br i1 %63, label %68, label %13
64: ; preds = %32, %20
%65 = load i32, i32* @stderr
%66 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %6)
%67 = call i32 @closedir(i32* %8)
br label %68
68: ; preds = %60, %64, %3
%69 = phi i32 [ 0, %3 ], [ 0, %64 ], [ %61, %60 ]
ret i32 %69
}