#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_interruptserver_to_drcindex(uint32_t int_serv, uint32_t *drc_idx) {
  DIR *dir;
  struct dirent *entry;
  int found = 0, fd;
  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,ppc-interrupt-server#s",
               entry->d_name);
      if ((fd = open(buffer, O_RDONLY)) < 0) {
        fprintf(stderr, "error opening %s\n", buffer);
        goto cleanup;
      }
      while ((read(fd, &temp, 4)) != 0) {
        if (temp == int_serv) {
          close(fd);
          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);
            goto cleanup;
          }
          if ((read(fd, drc_idx, 4)) == 4)
            found = 1;
          close(fd);
          break;
        }
      }
      close(fd);
    }
  }
cleanup:
  closedir(dir);
  return found;
}


x86 assembly (clang, -O0):
<cpu_interruptserver_to_drcindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe50(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     132c <cpu_interruptserver_to_drcindex+0x19c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe39(%rip),%rsi
mov    $0xd,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1327 <cpu_interruptserver_to_drcindex+0x197>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
mov    $0x400,%esi
lea    0xe1a(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1252 <cpu_interruptserver_to_drcindex+0xc2>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    -0x430(%rbp),%rdx
lea    0xe12(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1331 <cpu_interruptserver_to_drcindex+0x1a1>
jmp    1257 <cpu_interruptserver_to_drcindex+0xc7>
mov    -0x28(%rbp),%edi
lea    -0x434(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x0,%eax
je     131f <cpu_interruptserver_to_drcindex+0x18f>
mov    -0x434(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    131a <cpu_interruptserver_to_drcindex+0x18a>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
mov    $0x400,%esi
lea    0xdc7(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    12ec <cpu_interruptserver_to_drcindex+0x15c>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
lea    -0x430(%rbp),%rdx
lea    0xd78(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1331 <cpu_interruptserver_to_drcindex+0x1a1>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
jne    130d <cpu_interruptserver_to_drcindex+0x17d>
movl   $0x1,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
jmp    131f <cpu_interruptserver_to_drcindex+0x18f>
jmp    1257 <cpu_interruptserver_to_drcindex+0xc7>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
jmp    11b9 <cpu_interruptserver_to_drcindex+0x29>
jmp    1331 <cpu_interruptserver_to_drcindex+0x1a1>
mov    -0x18(%rbp),%rdi
call   1070 <closedir@plt>
mov    -0x24(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_interruptserver_to_drcindex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,0x8(%rsp)
mov    %edi,%ebp
lea    0xe51(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   10a0 <readdir@plt>
test   %rax,%rax
je     12f7 <cpu_interruptserver_to_drcindex+0x167>
mov    %rax,%r15
movl   $0x0,(%rsp)
lea    0xe3e(%rip),%rbx
lea    0x4(%rsp),%r14
jmp    1229 <cpu_interruptserver_to_drcindex+0x99>
mov    %eax,%r12d
mov    %eax,%edi
mov    0x8(%rsp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
cmp    $0x4,%eax
mov    (%rsp),%eax
mov    $0x1,%ecx
cmove  %ecx,%eax
mov    %eax,(%rsp)
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r12d,%edi
call   1060 <close@plt>
mov    %r13,%rdi
call   10a0 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     12fe <cpu_interruptserver_to_drcindex+0x16e>
mov    (%r15),%rdi
mov    %rbx,%rsi
mov    $0xd,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1215 <cpu_interruptserver_to_drcindex+0x85>
mov    (%r15),%rcx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x400,%esi
lea    0xdd1(%rip),%rdx
call   1050 <snprintf@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     12db <cpu_interruptserver_to_drcindex+0x14b>
mov    %eax,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
test   %eax,%eax
je     120d <cpu_interruptserver_to_drcindex+0x7d>
cmp    %ebp,0x4(%rsp)
jne    1280 <cpu_interruptserver_to_drcindex+0xf0>
mov    %r12d,%edi
call   1060 <close@plt>
mov    (%r15),%rcx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x400,%esi
lea    0xdaf(%rip),%rdx
call   1050 <snprintf@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10b0 <open@plt>
test   %eax,%eax
jns    11e0 <cpu_interruptserver_to_drcindex+0x50>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
lea    0x10(%rsp),%rdx
call   1090 <fprintf@plt>
jmp    12fe <cpu_interruptserver_to_drcindex+0x16e>
movl   $0x0,(%rsp)
mov    %r13,%rdi
call   1070 <closedir@plt>
mov    (%rsp),%eax
add    $0x418,%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 = [53 x i8] c"/proc/device-tree/cpus/%s/ibm,ppc-interrupt-server#s\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [18 x i8] c"error opening %s\0A\00"
@.str.4 = [43 x i8] c"/proc/device-tree/cpus/%s/ibm,my-drc-index\00"
define i32 @cpu_interruptserver_to_drcindex(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca %struct.dirent*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
store i32 0, i32* %7
%11 = call i32* @opendir(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
br label %12
12: ; preds = %74, %2
%13 = load i32*, i32** %5
%14 = call %struct.dirent* @readdir(i32* %13)
store %struct.dirent* %14, %struct.dirent** %6
%15 = icmp ne %struct.dirent* %14, null
br i1 %15, label %16, label %75
16: ; preds = %12
%17 = load %struct.dirent*, %struct.dirent** %6
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = call i32 @strncmp(i8* %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 13)
%21 = icmp ne i32 %20, 0
br i1 %21, label %74, label %22
22: ; preds = %16
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%24 = load %struct.dirent*, %struct.dirent** %6
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %24, i32 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @snprintf(i8* %23, i32 1024, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%29 = load i32, i32* @O_RDONLY
%30 = call i32 @open(i8* %28, i32 %29)
store i32 %30, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %22
%33 = load i32, i32* @stderr
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %34)
br label %76
36: ; preds = %22
br label %37
37: ; preds = %70, %36
%38 = load i32, i32* %8
%39 = call i32 @read(i32 %38, i32* %10, i32 4)
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %71
41: ; preds = %37
%42 = load i32, i32* %10
%43 = load i32, i32* %3
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %70
45: ; preds = %41
%46 = load i32, i32* %8
%47 = call i32 @close(i32 %46)
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%49 = load %struct.dirent*, %struct.dirent** %6
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 @snprintf(i8* %48, i32 1024, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%54 = load i32, i32* @O_RDONLY
%55 = call i32 @open(i8* %53, i32 %54)
store i32 %55, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %45
%58 = load i32, i32* @stderr
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %59)
br label %76
61: ; preds = %45
%62 = load i32, i32* %8
%63 = load i32*, i32** %4
%64 = call i32 @read(i32 %62, i32* %63, i32 4)
%65 = icmp eq i32 %64, 4
br i1 %65, label %66, label %67
66: ; preds = %61
store i32 1, i32* %7
br label %67
67: ; preds = %66, %61
%68 = load i32, i32* %8
%69 = call i32 @close(i32 %68)
br label %71
70: ; preds = %41
br label %37
71: ; preds = %67, %37
%72 = load i32, i32* %8
%73 = call i32 @close(i32 %72)
br label %74
74: ; preds = %71, %16
br label %12
75: ; preds = %12
br label %76
76: ; preds = %75, %57, %32
%77 = load i32*, i32** %5
%78 = call i32 @closedir(i32* %77)
%79 = load i32, i32* %7
ret i32 %79
}

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 = [53 x i8] c"/proc/device-tree/cpus/%s/ibm,ppc-interrupt-server#s\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.3 = [18 x i8] c"error opening %s\0A\00"
@.str.4 = [43 x i8] c"/proc/device-tree/cpus/%s/ibm,my-drc-index\00"
define i32 @cpu_interruptserver_to_drcindex(i32 %0, i32* nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = tail call i32* @opendir(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%8 = call %struct.dirent* @readdir(i32* %7)
%9 = icmp eq %struct.dirent* %8, null
br i1 %9, label %52, label %10
10: ; preds = %2, %45
%11 = phi %struct.dirent* [ %47, %45 ], [ %8, %2 ]
%12 = phi i32 [ %46, %45 ], [ 0, %2 ]
%13 = getelementptr inbounds %struct.dirent, %struct.dirent* %11, i64 0, i32 0
%14 = load i8*, i8** %13
%15 = call i32 @strncmp(i8* %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 13)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %45
17: ; preds = %10
%18 = load i8*, i8** %13
%19 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %5, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %49, label %23
23: ; preds = %17, %26
%24 = call i32 @read(i32 %21, i32* %4, i32 4)
%25 = icmp eq i32 %24, 0
br i1 %25, label %41, label %26
26: ; preds = %23
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, %0
br i1 %28, label %29, label %23
29: ; preds = %26
%30 = call i32 @close(i32 %21)
%31 = load i8*, i8** %13
%32 = call i32 @snprintf(i8* %5, i32 1024, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* %5, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %49, label %36
36: ; preds = %29
%37 = call i32 @read(i32 %34, i32* %1, i32 4)
%38 = icmp eq i32 %37, 4
%39 = select i1 %38, i32 1, i32 %12
%40 = call i32 @close(i32 %34)
br label %41
41: ; preds = %23, %36
%42 = phi i32 [ %39, %36 ], [ %12, %23 ]
%43 = phi i32 [ %34, %36 ], [ %21, %23 ]
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %41, %10
%46 = phi i32 [ %12, %10 ], [ %42, %41 ]
%47 = call %struct.dirent* @readdir(i32* %7)
%48 = icmp eq %struct.dirent* %47, null
br i1 %48, label %52, label %10
49: ; preds = %29, %17
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %5)
br label %52
52: ; preds = %45, %49, %2
%53 = phi i32 [ 0, %2 ], [ %12, %49 ], [ %46, %45 ]
%54 = call i32 @closedir(i32* %7)
ret i32 %53
}