#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__ off_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
char *realloc(char *, int);
int read_cpuinfo(char **buf) {
  int res, fd, hasRdLen;
  int bufLen;
  hasRdLen = 0;
  bufLen = 1024;
  *buf = malloc(bufLen);
  fd = open("/proc/cpuinfo", O_RDONLY);
  if (fd == -1) {
    perror("Error opening cpuinfo");
    res = -1;
    goto out;
  }
  do {
    res = read(fd, *buf, bufLen);
    if (res == -1) {
      if (errno == EINTR)
        continue;
      perror("Error reading cpuinfo");
      goto out;
    }
    hasRdLen += res;
  } while (res > 0);
  bufLen = hasRdLen;
  hasRdLen = 0;
  *buf = realloc(*buf, bufLen);
  memset(*buf, 0, bufLen);
  if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
    perror("Error rewinding cpuinfo");
    res = -1;
    goto out;
  }
  do {
    res = read(fd, *buf + hasRdLen, bufLen - hasRdLen);
    if (res == -1) {
      if (errno == EINTR)
        continue;
      perror("Error reading cpuinfo");
      goto out;
    }
    hasRdLen += res;
  } while (res > 0);
  (*buf)[bufLen - 1] = 0;
out:
  if (res) {
    free(*buf);
    *buf = NULL;
  }
  if (fd)
    close(fd);
  return res;
}


x86 assembly (clang, -O0):
<read_cpuinfo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x400,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe34(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11f6 <read_cpuinfo+0x66>
lea    0xe29(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    134e <read_cpuinfo+0x1be>
jmp    11fb <read_cpuinfo+0x6b>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    124a <read_cpuinfo+0xba>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%rax
jne    1239 <read_cpuinfo+0xa9>
jmp    1253 <read_cpuinfo+0xc3>
lea    0xde4(%rip),%rdi
call   10b0 <perror@plt>
jmp    134e <read_cpuinfo+0x1be>
mov    -0xc(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     11fb <read_cpuinfo+0x6b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%esi
call   1090 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x10(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
jne    12c9 <read_cpuinfo+0x139>
lea    0xd82(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0xc(%rbp)
jmp    134e <read_cpuinfo+0x1be>
jmp    12ce <read_cpuinfo+0x13e>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x18(%rbp),%edx
sub    -0x14(%rbp),%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1327 <read_cpuinfo+0x197>
mov    0x2cda(%rip),%rax
mov    (%rax),%rax
mov    0x2cc0(%rip),%rcx
cmp    (%rcx),%rax
jne    1316 <read_cpuinfo+0x186>
jmp    1330 <read_cpuinfo+0x1a0>
lea    0xd07(%rip),%rdi
call   10b0 <perror@plt>
jmp    134e <read_cpuinfo+0x1be>
mov    -0xc(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     12ce <read_cpuinfo+0x13e>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0xc(%rbp)
je     136f <read_cpuinfo+0x1df>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rax
movq   $0x0,(%rax)
cmpl   $0x0,-0x10(%rbp)
je     1381 <read_cpuinfo+0x1f1>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_cpuinfo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    $0x400,%edi
call   1080 <malloc@plt>
mov    %rax,(%r15)
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
lea    0xe48(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
je     12d6 <read_cpuinfo+0x146>
xor    %r12d,%r12d
nopl   0x0(%rax)
mov    (%r15),%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     11ee <read_cpuinfo+0x5e>
add    %eax,%r12d
test   %eax,%eax
jg     11d0 <read_cpuinfo+0x40>
jmp    1208 <read_cpuinfo+0x78>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%rax
jne    12f2 <read_cpuinfo+0x162>
mov    (%r15),%rdi
mov    %r12d,%esi
call   1090 <realloc@plt>
mov    %rax,(%r15)
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1050 <memset@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     12e4 <read_cpuinfo+0x154>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebp,%rax
mov    (%r15),%rsi
add    %rax,%rsi
mov    %r12d,%edx
sub    %eax,%edx
mov    %r14d,%edi
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1289 <read_cpuinfo+0xf9>
mov    %eax,%ebx
add    %eax,%ebp
test   %eax,%eax
jg     1250 <read_cpuinfo+0xc0>
mov    (%r15),%rax
movslq %r12d,%rcx
movb   $0x0,-0x1(%rcx,%rax,1)
jne    12af <read_cpuinfo+0x11f>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    12c3 <read_cpuinfo+0x133>
jmp    12cb <read_cpuinfo+0x13b>
mov    0x2d48(%rip),%rax
mov    (%rax),%rax
mov    0x2d2e(%rip),%rcx
cmp    (%rcx),%rax
jne    12f2 <read_cpuinfo+0x162>
mov    (%r15),%rax
movslq %r12d,%rcx
movb   $0x0,-0x1(%rcx,%rax,1)
mov    $0xffffffff,%ebx
mov    (%r15),%rdi
call   1030 <free@plt>
movq   $0x0,(%r15)
test   %r14d,%r14d
je     12cb <read_cpuinfo+0x13b>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd31(%rip),%rdi
call   10b0 <perror@plt>
jmp    12aa <read_cpuinfo+0x11a>
lea    0xd4f(%rip),%rdi
call   10b0 <perror@plt>
jmp    12aa <read_cpuinfo+0x11a>
lea    0xd2b(%rip),%rdi
call   10b0 <perror@plt>
jmp    12aa <read_cpuinfo+0x11a>

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

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/cpuinfo\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"Error opening cpuinfo\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.2 = [22 x i8] c"Error reading cpuinfo\00"
@SEEK_SET = global i32 0
@.str.3 = [24 x i8] c"Error rewinding cpuinfo\00"
define i32 @read_cpuinfo(i8** nocapture %0) {
%2 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
store i8* %2, i8** %0
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %60
8: ; preds = %1, %19
%9 = phi i32 [ %20, %19 ], [ 0, %1 ]
%10 = load i8*, i8** %0
%11 = tail call i32 @read(i32 %4, i8* %10, i32 1024)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %8
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %60
19: ; preds = %8
%20 = add nsw i32 %11, %9
%21 = icmp sgt i32 %11, 0
br i1 %21, label %8, label %22
22: ; preds = %19, %13
%23 = phi i32 [ %9, %13 ], [ %20, %19 ]
%24 = load i8*, i8** %0
%25 = tail call i8* @realloc(i8* %24, i32 %23)
store i8* %25, i8** %0
%26 = tail call i32 @memset(i8* %25, i32 0, i32 %23)
%27 = load i32, i32* @SEEK_SET
%28 = tail call i64 @lseek(i32 %4, i32 0, i32 %27)
%29 = icmp eq i64 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %60
32: ; preds = %22, %51
%33 = phi i32 [ %52, %51 ], [ 0, %22 ]
%34 = load i8*, i8** %0
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
%37 = sub nsw i32 %23, %33
%38 = tail call i32 @read(i32 %4, i8* %36, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %51
40: ; preds = %32
%41 = load i64, i64* @errno
%42 = load i64, i64* @EINTR
%43 = icmp eq i64 %41, %42
br i1 %43, label %44, label %49
44: ; preds = %40
%45 = load i8*, i8** %0
%46 = add nsw i32 %23, -1
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
br label %60
49: ; preds = %40
%50 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %60
51: ; preds = %32
%52 = add nsw i32 %38, %33
%53 = icmp sgt i32 %38, 0
br i1 %53, label %32, label %54
54: ; preds = %51
%55 = load i8*, i8** %0
%56 = add nsw i32 %23, -1
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
store i8 0, i8* %58
%59 = icmp eq i32 %38, 0
br i1 %59, label %64, label %60
60: ; preds = %17, %49, %30, %6, %44, %54
%61 = phi i32 [ %38, %54 ], [ -1, %44 ], [ -1, %6 ], [ -1, %30 ], [ -1, %49 ], [ -1, %17 ]
%62 = load i8*, i8** %0
%63 = tail call i32 @free(i8* %62)
store i8* null, i8** %0
br label %64
64: ; preds = %60, %54
%65 = phi i32 [ %61, %60 ], [ 0, %54 ]
%66 = icmp eq i32 %4, 0
br i1 %66, label %69, label %67
67: ; preds = %64
%68 = tail call i32 @close(i32 %4)
br label %69
69: ; preds = %67, %64
ret i32 %65
}