#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetline(char *, int, int /*<<< orphan*/ *);
int first_line_len(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
char *get_cpu(void) {
  FILE *fp;
  char *s;
  int len = 0;
  if ((fp = fopen("/proc/stat", "r")) == NULL) {
    perror("get_cpu: can't open /proc/stat");
    return NULL;
  }
  len = first_line_len(fp);
  s = malloc(sizeof(char) * len + 1);
  if (fgetline(s, len, fp) == -1)
    return NULL;
  fclose(fp);
  return s;
}


x86 assembly (clang, -O0):
<get_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x1c(%rbp)
lea    0xea0(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a9 <get_cpu+0x49>
lea    0xe6b(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1203 <get_cpu+0xa3>
mov    -0x10(%rbp),%rdi
call   1060 <first_line_len@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1030 <fgetline@plt>
cmp    $0xffffffff,%eax
jne    11f2 <get_cpu+0x92>
movq   $0x0,-0x8(%rbp)
jmp    1203 <get_cpu+0xa3>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_cpu>:
push   %r15
push   %r14
push   %rbx
lea    0xeaa(%rip),%rdi
lea    0xe8d(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b4 <get_cpu+0x54>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <first_line_len@plt>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
call   1030 <fgetline@plt>
cmp    $0xffffffff,%eax
je     11c0 <get_cpu+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
jmp    11c3 <get_cpu+0x63>
lea    0xe47(%rip),%rdi
call   1080 <perror@plt>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"get_cpu: can't open /proc/stat\00"
define i8* @get_cpu() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
store i32 0, i32* %4
%5 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %1
br label %28
9: ; preds = %0
%10 = load i32*, i32** %2
%11 = call i32 @first_line_len(i32* %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = sext i32 %12 to i64
%14 = mul i64 1, %13
%15 = add i64 %14, 1
%16 = trunc i64 %15 to i32
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = load i32, i32* %4
%20 = load i32*, i32** %2
%21 = call i32 @fgetline(i8* %18, i32 %19, i32* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %9
store i8* null, i8** %1
br label %28
24: ; preds = %9
%25 = load i32*, i32** %2
%26 = call i32 @fclose(i32* %25)
%27 = load i8*, i8** %3
store i8* %27, i8** %1
br label %28
28: ; preds = %24, %23, %7
%29 = load i8*, i8** %1
ret i8* %29
}

LLVM IR (-O2):
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"get_cpu: can't open /proc/stat\00"
define i8* @get_cpu() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %13
5: ; preds = %0
%6 = tail call i32 @first_line_len(i32* %1)
%7 = add i32 %6, 1
%8 = tail call i8* @malloc(i32 %7)
%9 = tail call i32 @fgetline(i8* %8, i32 %6, i32* %1)
%10 = icmp eq i32 %9, -1
br i1 %10, label %13, label %11
11: ; preds = %5
%12 = tail call i32 @fclose(i32* %1)
br label %13
13: ; preds = %5, %11, %3
%14 = phi i8* [ null, %3 ], [ %8, %11 ], [ null, %5 ]
ret i8* %14
}