#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 scalar_t__ uint8_t;
typedef scalar_t__ 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, scalar_t__ *, int);
int /*<<< orphan*/ stderr;
int mem_drcindex_to_drcname(uint32_t drc_idx, char *drc_name, int buf_size) {
  int fd, offset = 0, found = 0;
  uint32_t index;
  uint8_t ch;
  if ((fd = open("/proc/device-tree/ibm,drc-indexes", O_RDONLY)) < 0) {
    fprintf(stderr, "error opening /proc/device-tree/"
                    "ibm,drc-indexes");
    return 0;
  }
  read(fd, &index, 4);
  while ((read(fd, &index, 4)) != 0) {
    if (index == drc_idx) {
      found = 1;
      break;
    }
    offset++;
  }
  close(fd);
  if (found) {
    if ((fd = open("/proc/device-tree/ibm,drc-names", O_RDONLY)) < 0) {
      fprintf(stderr, "error opening /proc/device-tree/"
                      "ibm,drc-names");
      return 0;
    }
    read(fd, &index, 4);
    while (offset > 0) {
      do {
        if ((read(fd, &ch, 1)) != 1) {
          close(fd);
          return 0;
        }
      } while (ch != 0);
      offset--;
    }
    while ((read(fd, &ch, 1)) == 1) {
      if (offset + 1 == buf_size) {
        drc_name[offset] = '\0';
        break;
      }
      drc_name[offset++] = ch;
      if (ch == 0)
        break;
    }
  }
  close(fd);
  return found;
}


x86 assembly (clang, -O0):
<mem_drcindex_to_drcname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11a3 <mem_drcindex_to_drcname+0x63>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1338 <mem_drcindex_to_drcname+0x1f8>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x0,%eax
je     11f6 <mem_drcindex_to_drcname+0xb6>
mov    -0x30(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11e8 <mem_drcindex_to_drcname+0xa8>
movl   $0x1,-0x28(%rbp)
jmp    11f6 <mem_drcindex_to_drcname+0xb6>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b4 <mem_drcindex_to_drcname+0x74>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x28(%rbp)
je     132a <mem_drcindex_to_drcname+0x1ea>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    0xe26(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    124a <mem_drcindex_to_drcname+0x10a>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1338 <mem_drcindex_to_drcname+0x1f8>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmpl   $0x0,-0x24(%rbp)
jle    12b6 <mem_drcindex_to_drcname+0x176>
jmp    126a <mem_drcindex_to_drcname+0x12a>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1298 <mem_drcindex_to_drcname+0x158>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1338 <mem_drcindex_to_drcname+0x1f8>
jmp    129d <mem_drcindex_to_drcname+0x15d>
cmpq   $0x0,-0x38(%rbp)
jne    126a <mem_drcindex_to_drcname+0x12a>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    125b <mem_drcindex_to_drcname+0x11b>
jmp    12bb <mem_drcindex_to_drcname+0x17b>
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    1325 <mem_drcindex_to_drcname+0x1e5>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jne    12f5 <mem_drcindex_to_drcname+0x1b5>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1325 <mem_drcindex_to_drcname+0x1e5>
mov    -0x38(%rbp),%rax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpq   $0x0,-0x38(%rbp)
jne    1320 <mem_drcindex_to_drcname+0x1e0>
jmp    1325 <mem_drcindex_to_drcname+0x1e5>
jmp    12bb <mem_drcindex_to_drcname+0x17b>
jmp    132a <mem_drcindex_to_drcname+0x1ea>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_drcindex_to_drcname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%rbp
mov    0x2e8b(%rip),%r15
mov    (%r15),%esi
lea    0xea7(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e2 <mem_drcindex_to_drcname+0xa2>
mov    %eax,%r13d
mov    %ebx,0x14(%rsp)
lea    0x8(%rsp),%r12
mov    %eax,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
test   %eax,%eax
je     11ce <mem_drcindex_to_drcname+0x8e>
xor    %ebx,%ebx
lea    0x8(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
cmp    %rbp,0x8(%rsp)
je     120a <mem_drcindex_to_drcname+0xca>
add    $0x1,%ebx
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1040 <read@plt>
test   %eax,%eax
jne    11b0 <mem_drcindex_to_drcname+0x70>
mov    %r13d,%edi
call   1030 <close@plt>
xor    %ebp,%ebp
mov    %r13d,%edi
call   1030 <close@plt>
jmp    11f9 <mem_drcindex_to_drcname+0xb9>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
call   1050 <fprintf@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13d,%edi
call   1030 <close@plt>
mov    (%r15),%esi
lea    0xe22(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     12de <mem_drcindex_to_drcname+0x19e>
mov    %eax,%r13d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
test   %ebx,%ebx
je     1276 <mem_drcindex_to_drcname+0x136>
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    12d1 <mem_drcindex_to_drcname+0x191>
cmpq   $0x0,(%rsp)
jne    1250 <mem_drcindex_to_drcname+0x110>
lea    -0x1(%rbx),%eax
cmp    $0x1,%ebx
mov    %eax,%ebx
jg     1250 <mem_drcindex_to_drcname+0x110>
mov    0x14(%rsp),%r12d
add    $0xffffffffffffffff,%r12
xor    %ebx,%ebx
xor    %r15d,%r15d
mov    $0x1,%ebp
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rsp,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
jne    11d8 <mem_drcindex_to_drcname+0x98>
cmp    %r15,%r12
je     12f3 <mem_drcindex_to_drcname+0x1b3>
mov    (%rsp),%rax
mov    %al,(%r14,%r15,1)
movabs $0x100000000,%rcx
add    %rcx,%rbx
add    $0x1,%r15
test   %rax,%rax
jne    1290 <mem_drcindex_to_drcname+0x150>
jmp    11d8 <mem_drcindex_to_drcname+0x98>
mov    %r13d,%edi
call   1030 <close@plt>
jmp    11f7 <mem_drcindex_to_drcname+0xb7>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
lea    0xd42(%rip),%rsi
jmp    11f2 <mem_drcindex_to_drcname+0xb2>
sar    $0x20,%rbx
movb   $0x0,(%r14,%rbx,1)
jmp    11d8 <mem_drcindex_to_drcname+0x98>

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

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