#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 */
struct cpuusage_jiffies_stat {
  int dummy;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
struct cpuusage_jiffies_stat *cpuusage_set_jiffies_stat(char *, long, long,
                                                        long, long, long, long,
                                                        long, long, long, long,
                                                        int /*<<< orphan*/ *,
                                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sscanf(char *, char *, char *, long *, long *, long *,
                          long *, long *, long *, long *, long *, long *,
                          long *);
int strlen(char *);
struct cpuusage_jiffies_stat *proc_get_jiffies_stat(unsigned int cpuid) {
  FILE *fp = NULL;
  char buf[128];
  char cpu[6];
  long int user, nice, system, idle, iowait, irq, softirq, steal, guest,
      guest_nice;
  struct cpuusage_jiffies_stat *jiffies_stat = NULL;
  fp = fopen("/proc/stat", "r");
  if (fp == NULL) {
    perror("fopen:");
    exit(0);
  }
  if (!fgets(buf, sizeof(buf), fp)) {
    goto error;
  }
  if (strlen(buf) > (255 - 10)) {
    goto error;
  }
  for (unsigned int id = 0; id <= cpuid && !feof(fp); id++) {
    if (!fgets(buf, sizeof(buf), fp)) {
      goto error;
    }
    if (strlen(buf) > (255 - 10)) {
      goto error;
    }
    if (id != cpuid) {
      continue;
    }
    sscanf(buf, "%s%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld", cpu, &user, &nice, &system,
           &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice);
    fclose(fp);
  }
  if ((jiffies_stat = cpuusage_set_jiffies_stat(
           cpu, user, nice, system, idle, iowait, irq, softirq, steal, guest,
           guest_nice, NULL, NULL)) == NULL) {
    perror("error ");
    exit(-1);
  }
  return jiffies_stat;
error:
  fclose(fp);
  return NULL;
}


x86 assembly (clang, -O0):
<proc_get_jiffies_stat>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x140,%rsp
mov    %edi,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x120(%rbp)
lea    0xe41(%rip),%rdi
lea    0xe45(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ed <proc_get_jiffies_stat+0x5d>
lea    0xe2c(%rip),%rdi
call   10a0 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    -0xc0(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    1210 <proc_get_jiffies_stat+0x80>
jmp    141f <proc_get_jiffies_stat+0x28f>
lea    -0xc0(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jle    122c <proc_get_jiffies_stat+0x9c>
jmp    141f <proc_get_jiffies_stat+0x28f>
movl   $0x0,-0x124(%rbp)
mov    -0x124(%rbp),%ecx
xor    %eax,%eax
cmp    -0x2c(%rbp),%ecx
mov    %al,-0x125(%rbp)
ja     1264 <proc_get_jiffies_stat+0xd4>
mov    -0x38(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x125(%rbp)
mov    -0x125(%rbp),%al
test   $0x1,%al
jne    1277 <proc_get_jiffies_stat+0xe7>
jmp    1369 <proc_get_jiffies_stat+0x1d9>
lea    -0xc0(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x80,%esi
call   1050 <fgets@plt>
cmp    $0x0,%eax
jne    129a <proc_get_jiffies_stat+0x10a>
jmp    141f <proc_get_jiffies_stat+0x28f>
lea    -0xc0(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jle    12b6 <proc_get_jiffies_stat+0x126>
jmp    141f <proc_get_jiffies_stat+0x28f>
mov    -0x124(%rbp),%eax
cmp    -0x2c(%rbp),%eax
je     12ca <proc_get_jiffies_stat+0x13a>
jmp    1355 <proc_get_jiffies_stat+0x1c5>
lea    -0xc0(%rbp),%rdi
lea    -0xc6(%rbp),%rdx
lea    0xd35(%rip),%rsi
lea    -0xd0(%rbp),%rcx
lea    -0xd8(%rbp),%r8
lea    -0xe0(%rbp),%r9
lea    -0xe8(%rbp),%r12
lea    -0xf0(%rbp),%r15
lea    -0xf8(%rbp),%r14
lea    -0x100(%rbp),%rbx
lea    -0x108(%rbp),%r11
lea    -0x110(%rbp),%r10
lea    -0x118(%rbp),%rax
mov    %r12,(%rsp)
mov    %r15,0x8(%rsp)
mov    %r14,0x10(%rsp)
mov    %rbx,0x18(%rsp)
mov    %r11,0x20(%rsp)
mov    %r10,0x28(%rsp)
mov    %rax,0x30(%rsp)
call   1070 <sscanf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    1236 <proc_get_jiffies_stat+0xa6>
lea    -0xc6(%rbp),%rdi
mov    -0xd0(%rbp),%rsi
mov    -0xd8(%rbp),%rdx
mov    -0xe0(%rbp),%rcx
mov    -0xe8(%rbp),%r8
mov    -0xf0(%rbp),%r9
mov    -0xf8(%rbp),%r14
mov    -0x100(%rbp),%rbx
mov    -0x108(%rbp),%r11
mov    -0x110(%rbp),%r10
mov    -0x118(%rbp),%rax
xor    %r15d,%r15d
mov    %r14,(%rsp)
mov    %rbx,0x8(%rsp)
mov    %r11,0x10(%rsp)
mov    %r10,0x18(%rsp)
mov    %rax,0x20(%rsp)
movq   $0x0,0x28(%rsp)
movq   $0x0,0x30(%rsp)
call   1080 <cpuusage_set_jiffies_stat@plt>
mov    %rax,-0x120(%rbp)
cmp    $0x0,%rax
jne    140f <proc_get_jiffies_stat+0x27f>
lea    0xc35(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x120(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1430 <proc_get_jiffies_stat+0x2a0>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x140,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_get_jiffies_stat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
mov    %edi,%ebp
lea    0xe56(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1346 <proc_get_jiffies_stat+0x1b6>
mov    %rax,%rbx
lea    0x60(%rsp),%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     11e9 <proc_get_jiffies_stat+0x59>
lea    0x60(%rsp),%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jle    1205 <proc_get_jiffies_stat+0x75>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r13d,%r13d
lea    0x60(%rsp),%r14
lea    0x50(%rsp),%r15
lea    0x48(%rsp),%r12
jmp    122d <proc_get_jiffies_stat+0x9d>
nopl   0x0(%rax)
add    $0x1,%r13d
cmp    %ebp,%r13d
ja     12c3 <proc_get_jiffies_stat+0x133>
mov    %rbx,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    12c3 <proc_get_jiffies_stat+0x133>
mov    %r14,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %eax,%eax
je     11e9 <proc_get_jiffies_stat+0x59>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jg     11e9 <proc_get_jiffies_stat+0x59>
cmp    %r13d,%ebp
jne    1220 <proc_get_jiffies_stat+0x90>
sub    $0x8,%rsp
mov    %r14,%rdi
lea    0xda1(%rip),%rsi
lea    0x12(%rsp),%rdx
lea    0x60(%rsp),%rcx
mov    %r15,%r8
mov    %r12,%r9
lea    0x18(%rsp),%rax
push   %rax
lea    0x28(%rsp),%rax
push   %rax
lea    0x38(%rsp),%rax
push   %rax
lea    0x48(%rsp),%rax
push   %rax
lea    0x58(%rsp),%rax
push   %rax
lea    0x68(%rsp),%rax
push   %rax
lea    0x78(%rsp),%rax
push   %rax
call   1070 <sscanf@plt>
add    $0x40,%rsp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1220 <proc_get_jiffies_stat+0x90>
mov    0x58(%rsp),%rsi
mov    0x50(%rsp),%rdx
mov    0x48(%rsp),%rcx
mov    0x40(%rsp),%r8
mov    0x38(%rsp),%r9
mov    0x30(%rsp),%r10
mov    0x28(%rsp),%rdi
mov    0x20(%rsp),%rbp
mov    0x18(%rsp),%rbx
mov    0x10(%rsp),%rax
sub    $0x40,%rsp
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rsp)
mov    %rax,0x20(%rsp)
mov    %rbx,0x18(%rsp)
mov    %rbp,0x10(%rsp)
mov    %rdi,0x8(%rsp)
mov    %r10,(%rsp)
lea    0x4a(%rsp),%rdi
call   1080 <cpuusage_set_jiffies_stat@plt>
add    $0x40,%rsp
test   %rax,%rax
jne    11f3 <proc_get_jiffies_stat+0x63>
lea    0xcfe(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xcc0(%rip),%rdi
call   10a0 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.cpuusage_jiffies_stat = type { i32 }
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"fopen:\00"
@.str.3 = [33 x i8] c"%s%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld\00"
@.str.4 = [7 x i8] c"error \00"
define %struct.cpuusage_jiffies_stat* @proc_get_jiffies_stat(i32 %0) {
%2 = alloca %struct.cpuusage_jiffies_stat*
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [128 x i8]
%6 = alloca [6 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca %struct.cpuusage_jiffies_stat*
%18 = alloca i32
store i32 %0, i32* %3
store i32* null, i32** %4
store %struct.cpuusage_jiffies_stat* null, %struct.cpuusage_jiffies_stat** %17
%19 = 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* %19, i32** %4
%20 = load i32*, i32** %4
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %25
22: ; preds = %1
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @exit(i32 0)
unreachable
25: ; preds = %1
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%27 = load i32*, i32** %4
%28 = call i32 @fgets(i8* %26, i32 128, i32* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %25
br label %92
31: ; preds = %25
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = icmp sgt i32 %33, 245
br i1 %34, label %35, label %36
35: ; preds = %31
br label %92
36: ; preds = %31
store i32 0, i32* %18
br label %37
37: ; preds = %70, %36
%38 = load i32, i32* %18
%39 = load i32, i32* %3
%40 = icmp ule i32 %38, %39
br i1 %40, label %41, label %46
41: ; preds = %37
%42 = load i32*, i32** %4
%43 = call i32 @feof(i32* %42)
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br label %46
46: ; preds = %41, %37
%47 = phi i1 [ false, %37 ], [ %45, %41 ]
br i1 %47, label %48, label %73
48: ; preds = %46
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%50 = load i32*, i32** %4
%51 = call i32 @fgets(i8* %49, i32 128, i32* %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %54, label %53
53: ; preds = %48
br label %92
54: ; preds = %48
%55 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%56 = call i32 @strlen(i8* %55)
%57 = icmp sgt i32 %56, 245
br i1 %57, label %58, label %59
58: ; preds = %54
br label %92
59: ; preds = %54
%60 = load i32, i32* %18
%61 = load i32, i32* %3
%62 = icmp ne i32 %60, %61
br i1 %62, label %63, label %64
63: ; preds = %59
br label %70
64: ; preds = %59
%65 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%66 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%67 = call i32 @sscanf(i8* %65, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %66, i64* %7, i64* %8, i64* %9, i64* %10, i64* %11, i64* %12, i64* %13, i64* %14, i64* %15, i64* %16)
%68 = load i32*, i32** %4
%69 = call i32 @fclose(i32* %68)
br label %70
70: ; preds = %64, %63
%71 = load i32, i32* %18
%72 = add i32 %71, 1
store i32 %72, i32* %18
br label %37
73: ; preds = %46
%74 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%75 = load i64, i64* %7
%76 = load i64, i64* %8
%77 = load i64, i64* %9
%78 = load i64, i64* %10
%79 = load i64, i64* %11
%80 = load i64, i64* %12
%81 = load i64, i64* %13
%82 = load i64, i64* %14
%83 = load i64, i64* %15
%84 = load i64, i64* %16
%85 = call %struct.cpuusage_jiffies_stat* @cpuusage_set_jiffies_stat(i8* %74, i64 %75, i64 %76, i64 %77, i64 %78, i64 %79, i64 %80, i64 %81, i64 %82, i64 %83, i64 %84, i32* null, i32* null)
store %struct.cpuusage_jiffies_stat* %85, %struct.cpuusage_jiffies_stat** %17
%86 = icmp eq %struct.cpuusage_jiffies_stat* %85, null
br i1 %86, label %87, label %90
87: ; preds = %73
%88 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%89 = call i32 @exit(i32 -1)
unreachable
90: ; preds = %73
%91 = load %struct.cpuusage_jiffies_stat*, %struct.cpuusage_jiffies_stat** %17
store %struct.cpuusage_jiffies_stat* %91, %struct.cpuusage_jiffies_stat** %2
br label %95
92: ; preds = %58, %53, %35, %30
%93 = load i32*, i32** %4
%94 = call i32 @fclose(i32* %93)
store %struct.cpuusage_jiffies_stat* null, %struct.cpuusage_jiffies_stat** %2
br label %95
95: ; preds = %92, %90
%96 = load %struct.cpuusage_jiffies_stat*, %struct.cpuusage_jiffies_stat** %2
ret %struct.cpuusage_jiffies_stat* %96
}

LLVM IR (-O2):
%struct.cpuusage_jiffies_stat = type { i32 }
@.str = [11 x i8] c"/proc/stat\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"fopen:\00"
@.str.3 = [33 x i8] c"%s%ld%ld%ld%ld%ld%ld%ld%ld%ld%ld\00"
@.str.4 = [7 x i8] c"error \00"
define %struct.cpuusage_jiffies_stat* @proc_get_jiffies_stat(i32 %0) {
%2 = alloca [128 x i8]
%3 = alloca [6 x i8]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i64
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%16 = bitcast i64* %4 to i8*
%17 = bitcast i64* %5 to i8*
%18 = bitcast i64* %6 to i8*
%19 = bitcast i64* %7 to i8*
%20 = bitcast i64* %8 to i8*
%21 = bitcast i64* %9 to i8*
%22 = bitcast i64* %10 to i8*
%23 = bitcast i64* %11 to i8*
%24 = bitcast i64* %12 to i8*
%25 = bitcast i64* %13 to i8*
%26 = 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))
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %1
%29 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%30 = tail call i32 @exit(i32 0)
unreachable
31: ; preds = %1
%32 = call i32 @fgets(i8* %14, i32 128, i32* %26)
%33 = icmp eq i32 %32, 0
br i1 %33, label %71, label %34
34: ; preds = %31
%35 = call i32 @strlen(i8* %14)
%36 = icmp sgt i32 %35, 245
br i1 %36, label %71, label %37
37: ; preds = %34, %52
%38 = phi i32 [ %53, %52 ], [ 0, %34 ]
%39 = call i32 @feof(i32* %26)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %55
41: ; preds = %37
%42 = call i32 @fgets(i8* %14, i32 128, i32* %26)
%43 = icmp eq i32 %42, 0
br i1 %43, label %71, label %44
44: ; preds = %41
%45 = call i32 @strlen(i8* %14)
%46 = icmp sgt i32 %45, 245
br i1 %46, label %71, label %47
47: ; preds = %44
%48 = icmp eq i32 %38, %0
br i1 %48, label %49, label %52
49: ; preds = %47
%50 = call i32 @sscanf(i8* %14, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %15, i64* %4, i64* %5, i64* %6, i64* %7, i64* %8, i64* %9, i64* %10, i64* %11, i64* %12, i64* %13)
%51 = call i32 @fclose(i32* %26)
br label %52
52: ; preds = %47, %49
%53 = add i32 %38, 1
%54 = icmp ugt i32 %53, %0
br i1 %54, label %55, label %37
55: ; preds = %52, %37
%56 = load i64, i64* %4
%57 = load i64, i64* %5
%58 = load i64, i64* %6
%59 = load i64, i64* %7
%60 = load i64, i64* %8
%61 = load i64, i64* %9
%62 = load i64, i64* %10
%63 = load i64, i64* %11
%64 = load i64, i64* %12
%65 = load i64, i64* %13
%66 = call %struct.cpuusage_jiffies_stat* @cpuusage_set_jiffies_stat(i8* %15, i64 %56, i64 %57, i64 %58, i64 %59, i64 %60, i64 %61, i64 %62, i64 %63, i64 %64, i64 %65, i32* null, i32* null)
%67 = icmp eq %struct.cpuusage_jiffies_stat* %66, null
br i1 %67, label %68, label %73
68: ; preds = %55
%69 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%70 = call i32 @exit(i32 -1)
unreachable
71: ; preds = %41, %44, %34, %31
%72 = call i32 @fclose(i32* %26)
br label %73
73: ; preds = %55, %71
%74 = phi %struct.cpuusage_jiffies_stat* [ null, %71 ], [ %66, %55 ]
ret %struct.cpuusage_jiffies_stat* %74
}