#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 char uint8_t;
typedef char uint32_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int cpu_drcname_to_drcindex(char *drc_name, uint32_t *drc_idx) {
  int fd, offset = 0, found = 0, i, rc;
  uint32_t index;
  uint8_t ch;
  char buffer[64];
  if ((fd = open("/proc/device-tree/cpus/ibm,drc-names", O_RDONLY)) < 0) {
    fprintf(stderr, "error opening /proc/device-tree/cpus/"
                    "ibm,drc-names");
    return 0;
  }
  read(fd, &index, 4);
  do {
    i = 0;
    while ((rc = read(fd, &ch, 1)) == 1) {
      buffer[i++] = ch;
      if (ch == 0)
        break;
    }
    if (!strcmp(buffer, drc_name)) {
      found = 1;
      break;
    }
    offset++;
  } while (rc);
  close(fd);
  if (found) {
    if ((fd = open("/proc/device-tree/cpus/ibm,drc-indexes", O_RDONLY)) < 0) {
      fprintf(stderr, "error opening /proc/device-tree/cpus/"
                      "ibm,drc-indexes");
      return 0;
    }
    read(fd, &index, 4);
    while (offset > 0) {
      if ((read(fd, &index, 4)) != 4) {
        close(fd);
        return 0;
      }
      offset--;
    }
    if ((read(fd, drc_idx, 4)) != 4)
      found = 0;
  }
  close(fd);
  return found;
}


x86 assembly (clang, -O0):
<cpu_drcname_to_drcindex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11b0 <cpu_drcname_to_drcindex+0x60>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1324 <cpu_drcname_to_drcindex+0x1d4>
mov    -0x1c(%rbp),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x2e(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x1,%eax
jne    1210 <cpu_drcname_to_drcindex+0xc0>
mov    -0x2e(%rbp),%cl
mov    -0x28(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x28(%rbp)
cltq
mov    %cl,-0x70(%rbp,%rax,1)
movsbl -0x2e(%rbp),%eax
cmp    $0x0,%eax
jne    120b <cpu_drcname_to_drcindex+0xbb>
jmp    1210 <cpu_drcname_to_drcindex+0xc0>
jmp    11c8 <cpu_drcname_to_drcindex+0x78>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1232 <cpu_drcname_to_drcindex+0xe2>
movl   $0x1,-0x24(%rbp)
jmp    1245 <cpu_drcname_to_drcindex+0xf5>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11c1 <cpu_drcname_to_drcindex+0x71>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x24(%rbp)
je     1316 <cpu_drcname_to_drcindex+0x1c6>
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
lea    0xdda(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1299 <cpu_drcname_to_drcindex+0x149>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1324 <cpu_drcname_to_drcindex+0x1d4>
mov    -0x1c(%rbp),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmpl   $0x0,-0x20(%rbp)
jle    12f0 <cpu_drcname_to_drcindex+0x1a0>
mov    -0x1c(%rbp),%edi
lea    -0x2d(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
je     12e2 <cpu_drcname_to_drcindex+0x192>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1324 <cpu_drcname_to_drcindex+0x1d4>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    12aa <cpu_drcname_to_drcindex+0x15a>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
je     1311 <cpu_drcname_to_drcindex+0x1c1>
movl   $0x0,-0x24(%rbp)
jmp    1316 <cpu_drcname_to_drcindex+0x1c6>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_drcname_to_drcindex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%rbx
mov    %rdi,0x18(%rsp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1208 <cpu_drcname_to_drcindex+0xb8>
mov    %eax,%r13d
mov    %rbx,0x10(%rsp)
lea    0xe(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
xor    %r15d,%r15d
mov    $0x1,%ebx
lea    0x20(%rsp),%r12
lea    0xf(%rsp),%r14
xchg   %ax,%ax
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ebp
cmp    $0x1,%eax
jne    11d8 <cpu_drcname_to_drcindex+0x88>
movzbl 0xf(%rsp),%eax
mov    %al,(%r12)
add    $0x1,%r12
test   %al,%al
jne    11b0 <cpu_drcname_to_drcindex+0x60>
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    0x18(%rsp),%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
je     121d <cpu_drcname_to_drcindex+0xcd>
add    $0x1,%r15d
add    $0x1,%ebx
test   %ebp,%ebp
jne    11b0 <cpu_drcname_to_drcindex+0x60>
mov    %r13d,%edi
call   1030 <close@plt>
xor    %ebx,%ebx
jmp    129b <cpu_drcname_to_drcindex+0x14b>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
jmp    12b5 <cpu_drcname_to_drcindex+0x165>
mov    %r13d,%edi
call   1030 <close@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    0xe0c(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     12a5 <cpu_drcname_to_drcindex+0x155>
mov    %eax,%r13d
mov    %ebx,%ebp
lea    0xe(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
test   %r15d,%r15d
je     1281 <cpu_drcname_to_drcindex+0x131>
lea    0xe(%rsp),%rbx
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jne    12cd <cpu_drcname_to_drcindex+0x17d>
mov    %ebp,%eax
add    $0xffffffff,%eax
mov    %eax,%ebp
cmp    $0x2,%eax
jge    1260 <cpu_drcname_to_drcindex+0x110>
mov    %r13d,%edi
mov    0x10(%rsp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
xor    %ebx,%ebx
cmp    $0x4,%eax
sete   %bl
mov    %r13d,%edi
call   1030 <close@plt>
jmp    12bc <cpu_drcname_to_drcindex+0x16c>
mov    0x2d2c(%rip),%rax
mov    (%rax),%edi
lea    0xd7e(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13d,%edi
call   1030 <close@plt>
jmp    12ba <cpu_drcname_to_drcindex+0x16a>

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

LLVM IR (-O2):
@.str = [37 x i8] c"/proc/device-tree/cpus/ibm,drc-names\00"
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str.1 = [51 x i8] c"error opening /proc/device-tree/cpus/ibm,drc-names\00"
@.str.2 = [39 x i8] c"/proc/device-tree/cpus/ibm,drc-indexes\00"
@.str.3 = [53 x i8] c"error opening /proc/device-tree/cpus/ibm,drc-indexes\00"
define i32 @cpu_drcname_to_drcindex(i8* nocapture %0, i8* nocapture %1) {
%3 = alloca i8
%4 = alloca i8
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0))
br label %62
13: ; preds = %2
%14 = call i32 @read(i32 %8, i8* %3, i32 4)
br label %15
15: ; preds = %29, %13
%16 = phi i32 [ 0, %13 ], [ %30, %29 ]
br label %17
17: ; preds = %21, %15
%18 = phi i64 [ %23, %21 ], [ 0, %15 ]
%19 = call i32 @read(i32 %8, i8* %4, i32 1)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = load i8, i8* %4
%23 = add nuw i64 %18, 1
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 %18
store i8 %22, i8* %24
%25 = icmp eq i8 %22, 0
br i1 %25, label %26, label %17
26: ; preds = %21, %17
%27 = call i32 @strcmp(i8* %6, i8* dereferenceable(1) %0)
%28 = icmp eq i32 %27, 0
br i1 %28, label %34, label %29
29: ; preds = %26
%30 = add nuw nsw i32 %16, 1
%31 = icmp eq i32 %19, 0
br i1 %31, label %32, label %15
32: ; preds = %29
%33 = tail call i32 @close(i32 %8)
br label %58
34: ; preds = %26
%35 = tail call i32 @close(i32 %8)
%36 = load i32, i32* @O_RDONLY
%37 = tail call i32 @open(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = load i32, i32* @stderr
%41 = tail call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %62
42: ; preds = %34
%43 = call i32 @read(i32 %37, i8* %3, i32 4)
%44 = icmp eq i32 %16, 0
br i1 %44, label %54, label %48
45: ; preds = %48
%46 = add nsw i32 %49, -1
%47 = icmp sgt i32 %49, 1
br i1 %47, label %48, label %54
48: ; preds = %42, %45
%49 = phi i32 [ %46, %45 ], [ %16, %42 ]
%50 = call i32 @read(i32 %37, i8* %3, i32 4)
%51 = icmp eq i32 %50, 4
br i1 %51, label %45, label %52
52: ; preds = %48
%53 = tail call i32 @close(i32 %37)
br label %62
54: ; preds = %45, %42
%55 = tail call i32 @read(i32 %37, i8* %1, i32 4)
%56 = icmp eq i32 %55, 4
%57 = zext i1 %56 to i32
br label %58
58: ; preds = %32, %54
%59 = phi i32 [ %8, %32 ], [ %37, %54 ]
%60 = phi i32 [ 0, %32 ], [ %57, %54 ]
%61 = tail call i32 @close(i32 %59)
br label %62
62: ; preds = %58, %52, %39, %10
%63 = phi i32 [ 0, %10 ], [ 0, %39 ], [ 0, %52 ], [ %60, %58 ]
ret i32 %63
}