#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*/ be32toh(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
int read(int, int /*<<< orphan*/ *, int);
scalar_t__ read_uint32(int, int /*<<< orphan*/ *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
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 (!found && (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: error opening %s:\n"
                "%s\n",
                buffer, strerror(errno));
        goto cleanup;
      }
      while (read_uint32(fd, &temp) == 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%s\n",
                    buffer, strerror(errno));
            goto cleanup;
          }
          if ((read(fd, &temp, 4)) == 4) {
            *drc_idx = be32toh(temp);
            found = 1;
          }
          break;
        }
      }
      close(fd);
    }
  }
cleanup:
  closedir(dir);
  return found;
}


x86 assembly (clang, -O0):
<cpu_interruptserver_to_drcindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
lea    0xe20(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x435(%rbp)
jne    1215 <cpu_interruptserver_to_drcindex+0x55>
mov    -0x18(%rbp),%rdi
call   10b0 <readdir@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
setne  %al
mov    %al,-0x435(%rbp)
mov    -0x435(%rbp),%al
test   $0x1,%al
jne    1228 <cpu_interruptserver_to_drcindex+0x68>
jmp    13e4 <cpu_interruptserver_to_drcindex+0x224>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xde1(%rip),%rsi
mov    $0xd,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    13df <cpu_interruptserver_to_drcindex+0x21f>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
mov    $0x400,%esi
lea    0xdc2(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    12d5 <cpu_interruptserver_to_drcindex+0x115>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x444(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rdx
mov    %rax,%rcx
lea    0xd8f(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    13e9 <cpu_interruptserver_to_drcindex+0x229>
jmp    12da <cpu_interruptserver_to_drcindex+0x11a>
mov    -0x28(%rbp),%edi
lea    -0x434(%rbp),%rsi
call   1040 <read_uint32@plt>
cmp    $0x0,%rax
jne    13d7 <cpu_interruptserver_to_drcindex+0x217>
mov    -0x434(%rbp),%eax
cmp    -0x4(%rbp),%eax
jne    13d2 <cpu_interruptserver_to_drcindex+0x212>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
mov    $0x400,%esi
lea    0xd53(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2cb1(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1396 <cpu_interruptserver_to_drcindex+0x1d6>
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x454(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rdx
mov    %rax,%rcx
lea    0xd16(%rip),%rsi
call   10a0 <fprintf@plt>
jmp    13e9 <cpu_interruptserver_to_drcindex+0x229>
mov    -0x28(%rbp),%edi
lea    -0x434(%rbp),%rsi
mov    $0x4,%edx
call   1090 <read@plt>
cmp    $0x4,%eax
jne    13cd <cpu_interruptserver_to_drcindex+0x20d>
mov    -0x434(%rbp),%edi
call   10c0 <be32toh@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x24(%rbp)
jmp    13d7 <cpu_interruptserver_to_drcindex+0x217>
jmp    12da <cpu_interruptserver_to_drcindex+0x11a>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
jmp    11e9 <cpu_interruptserver_to_drcindex+0x29>
jmp    13e9 <cpu_interruptserver_to_drcindex+0x229>
mov    -0x18(%rbp),%rdi
call   1080 <closedir@plt>
mov    -0x24(%rbp),%eax
add    $0x460,%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    $0x428,%rsp
mov    %rsi,0x18(%rsp)
mov    %edi,%ebp
lea    0xe21(%rip),%rdi
call   1050 <opendir@plt>
mov    %rax,%r13
lea    0xe29(%rip),%rbx
lea    0xc(%rsp),%r15
mov    %rax,0x10(%rsp)
jmp    1216 <cpu_interruptserver_to_drcindex+0x56>
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
call   1070 <close@plt>
mov    0x10(%rsp),%r13
test   %r14d,%r14d
jne    1311 <cpu_interruptserver_to_drcindex+0x151>
mov    %r13,%rdi
call   10b0 <readdir@plt>
test   %rax,%rax
je     130d <cpu_interruptserver_to_drcindex+0x14d>
mov    %rax,%r12
mov    (%rax),%rdi
mov    %rbx,%rsi
mov    $0xd,%edx
call   1030 <strncmp@plt>
xor    %r14d,%r14d
test   %eax,%eax
jne    120d <cpu_interruptserver_to_drcindex+0x4d>
mov    (%r12),%rcx
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x400,%esi
lea    0xdcc(%rip),%rdx
call   1060 <snprintf@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10d0 <open@plt>
test   %eax,%eax
js     1318 <cpu_interruptserver_to_drcindex+0x158>
mov    %eax,%r13d
nopw   0x0(%rax,%rax,1)
mov    %r13d,%edi
mov    %r15,%rsi
call   1040 <read_uint32@plt>
xor    %r14d,%r14d
test   %rax,%rax
jne    1200 <cpu_interruptserver_to_drcindex+0x40>
cmp    %ebp,0xc(%rsp)
jne    1280 <cpu_interruptserver_to_drcindex+0xc0>
mov    %r13d,%edi
call   1070 <close@plt>
mov    (%r12),%rcx
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    $0x400,%esi
lea    0xdba(%rip),%rdx
call   1060 <snprintf@plt>
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10d0 <open@plt>
test   %eax,%eax
js     1321 <cpu_interruptserver_to_drcindex+0x161>
mov    %eax,%r13d
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1090 <read@plt>
cmp    $0x4,%eax
jne    1200 <cpu_interruptserver_to_drcindex+0x40>
mov    0xc(%rsp),%edi
call   10c0 <be32toh@plt>
mov    0x18(%rsp),%rcx
mov    %eax,(%rcx)
mov    $0x1,%r14d
jmp    1200 <cpu_interruptserver_to_drcindex+0x40>
xor    %ebp,%ebp
jmp    1358 <cpu_interruptserver_to_drcindex+0x198>
mov    $0x1,%ebp
jmp    1358 <cpu_interruptserver_to_drcindex+0x198>
lea    0xd3b(%rip),%r14
jmp    1328 <cpu_interruptserver_to_drcindex+0x168>
lea    0xd7a(%rip),%r14
mov    0x2ca9(%rip),%rax
mov    (%rax),%ebp
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0x20(%rsp),%rdx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %rax,%rcx
call   10a0 <fprintf@plt>
xor    %ebp,%ebp
mov    0x10(%rsp),%r13
mov    %r13,%rdi
call   1080 <closedir@plt>
mov    %ebp,%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 = [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 = [29 x i8] c"Error: error opening %s:\0A%s\0A\00"
@errno = global i32 0
@.str.4 = [43 x i8] c"/proc/device-tree/cpus/%s/ibm,my-drc-index\00"
@.str.5 = [22 x i8] c"Error opening %s:\0A%s\0A\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 = %83, %2
%13 = load i32, i32* %7
%14 = icmp ne i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = load i32*, i32** %5
%17 = call %struct.dirent* @readdir(i32* %16)
store %struct.dirent* %17, %struct.dirent** %6
%18 = icmp ne %struct.dirent* %17, null
br label %19
19: ; preds = %15, %12
%20 = phi i1 [ false, %12 ], [ %18, %15 ]
br i1 %20, label %21, label %84
21: ; preds = %19
%22 = load %struct.dirent*, %struct.dirent** %6
%23 = getelementptr inbounds %struct.dirent, %struct.dirent* %22, i32 0, i32 0
%24 = load i8*, i8** %23
%25 = call i32 @strncmp(i8* %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 13)
%26 = icmp ne i32 %25, 0
br i1 %26, label %83, label %27
27: ; preds = %21
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%29 = load %struct.dirent*, %struct.dirent** %6
%30 = getelementptr inbounds %struct.dirent, %struct.dirent* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @snprintf(i8* %28, i32 1024, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %27
%38 = load i32, i32* @stderr
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %39, i8* %41)
br label %85
43: ; preds = %27
br label %44
44: ; preds = %79, %43
%45 = load i32, i32* %8
%46 = call i64 @read_uint32(i32 %45, i32* %10)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %80
48: ; preds = %44
%49 = load i32, i32* %10
%50 = load i32, i32* %3
%51 = icmp eq i32 %49, %50
br i1 %51, label %52, label %79
52: ; preds = %48
%53 = load i32, i32* %8
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%56 = load %struct.dirent*, %struct.dirent** %6
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 @snprintf(i8* %55, i32 1024, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.4, i64 0, i64 0), i8* %58)
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%61 = load i32, i32* @O_RDONLY
%62 = call i32 @open(i8* %60, i32 %61)
store i32 %62, i32* %8
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %70
64: ; preds = %52
%65 = load i32, i32* @stderr
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%67 = load i32, i32* @errno
%68 = call i8* @strerror(i32 %67)
%69 = call i32 @fprintf(i32 %65, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i8* %66, i8* %68)
br label %85
70: ; preds = %52
%71 = load i32, i32* %8
%72 = call i32 @read(i32 %71, i32* %10, i32 4)
%73 = icmp eq i32 %72, 4
br i1 %73, label %74, label %78
74: ; preds = %70
%75 = load i32, i32* %10
%76 = call i32 @be32toh(i32 %75)
%77 = load i32*, i32** %4
store i32 %76, i32* %77
store i32 1, i32* %7
br label %78
78: ; preds = %74, %70
br label %80
79: ; preds = %48
br label %44
80: ; preds = %78, %44
%81 = load i32, i32* %8
%82 = call i32 @close(i32 %81)
br label %83
83: ; preds = %80, %21
br label %12
84: ; preds = %19
br label %85
85: ; preds = %84, %64, %37
%86 = load i32*, i32** %5
%87 = call i32 @closedir(i32* %86)
%88 = load i32, i32* %7
ret i32 %88
}

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