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

/* Variables and functions */
int EIO;
int ENODEV;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memcpy(unsigned long *, long *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int sscanf(char *, char *, char *, long *);
int strlen(char *);
int proc_get_task_hmpskched_avg(unsigned int pid, struct hmpsched_avg *avg) {
  FILE *fp = NULL;
  char filename[255];
  char line[255];
  char value_name[255];
  long value_data;
  int ret = -ENODEV;
  int offset = 0;
  snprintf(filename, 255,
           "/proc/%d"
           "/sched",
           pid);
  fp = fopen(filename, "r");
  if (!fp) {
    perror("open error");
    return -ENODEV;
  }
  for (offset = 0; !feof(fp); offset++) {
    if (!fgets(line, 255, fp)) {
      ret = -EIO;
      goto error;
    }
    if (strlen(line) > (255 - 10)) {
      ret = -EIO;
      goto error;
    }
    if (offset < 39) {
      continue;
    }
    ret = sscanf(line, "%s : %ld", value_name, &value_data);
    if (ret != 2) {
      ret = -EIO;
      goto error;
    }
    printf("%s", line);
    printf("%-45s:%21ld\n", value_name, value_data);
    memcpy((unsigned long *)avg + offset - 39, &value_data,
           sizeof(unsigned long));
    printf("%ld == %ld\n", value_data, *((unsigned long *)avg + offset - 39));
  }
  ret = 0;
error:
  fclose(fp);
  return (ret);
}


x86 assembly (clang, -O0):
<proc_get_task_hmpskched_avg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x330,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
xor    %eax,%eax
mov    0x2e15(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x32c(%rbp)
movl   $0x0,-0x330(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0xff,%esi
lea    0xe15(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1231 <proc_get_task_hmpskched_avg+0x91>
lea    0xdf6(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
mov    0x2db1(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13c9 <proc_get_task_hmpskched_avg+0x229>
movl   $0x0,-0x330(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1259 <proc_get_task_hmpskched_avg+0xb9>
jmp    13ad <proc_get_task_hmpskched_avg+0x20d>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xff,%esi
call   1070 <fgets@plt>
cmp    $0x0,%eax
jne    128d <proc_get_task_hmpskched_avg+0xed>
xor    %eax,%eax
mov    0x2d60(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x32c(%rbp)
jmp    13b7 <proc_get_task_hmpskched_avg+0x217>
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jle    12ba <proc_get_task_hmpskched_avg+0x11a>
xor    %eax,%eax
mov    0x2d33(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x32c(%rbp)
jmp    13b7 <proc_get_task_hmpskched_avg+0x217>
cmpl   $0x27,-0x330(%rbp)
jge    12cc <proc_get_task_hmpskched_avg+0x12c>
jmp    1399 <proc_get_task_hmpskched_avg+0x1f9>
lea    -0x220(%rbp),%rdi
lea    -0x320(%rbp),%rdx
lea    0xd39(%rip),%rsi
lea    -0x328(%rbp),%rcx
call   10a0 <sscanf@plt>
mov    %eax,-0x32c(%rbp)
cmpl   $0x2,-0x32c(%rbp)
je     1316 <proc_get_task_hmpskched_avg+0x176>
xor    %eax,%eax
mov    0x2cd7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x32c(%rbp)
jmp    13b7 <proc_get_task_hmpskched_avg+0x217>
lea    -0x220(%rbp),%rsi
lea    0xcff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x320(%rbp),%rsi
mov    -0x328(%rbp),%rdx
lea    0xce6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
movslq -0x330(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
add    $0xfffffffffffffec8,%rdi
lea    -0x328(%rbp),%rsi
mov    $0x8,%edx
call   1090 <memcpy@plt>
mov    -0x328(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq -0x330(%rbp),%rcx
mov    -0x138(%rax,%rcx,8),%rdx
ff
lea    0xca1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x330(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x330(%rbp)
jmp    123b <proc_get_task_hmpskched_avg+0x9b>
movl   $0x0,-0x32c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x32c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x330,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_get_task_hmpskched_avg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %rsi,%rbx
mov    %edi,%ecx
lea    0xe43(%rip),%rdx
lea    0x210(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xff,%esi
call   1060 <snprintf@plt>
lea    0xe3f(%rip),%rsi
mov    %rbp,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12fc <proc_get_task_hmpskched_avg+0x15c>
mov    %rax,%r14
mov    %rax,%rdi
call   1080 <feof@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    12e0 <proc_get_task_hmpskched_avg+0x140>
add    $0xfffffffffffffec8,%rbx
lea    0x110(%rsp),%r15
lea    0x10(%rsp),%r13
lea    0x8(%rsp),%rbp
xor    %r12d,%r12d
jmp    1238 <proc_get_task_hmpskched_avg+0x98>
nopl   (%rax)
add    $0x1,%r12
mov    %r14,%rdi
call   1080 <feof@plt>
add    $0x8,%rbx
test   %eax,%eax
jne    12de <proc_get_task_hmpskched_avg+0x13e>
mov    %r15,%rdi
mov    $0xff,%esi
mov    %r14,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     12d1 <proc_get_task_hmpskched_avg+0x131>
mov    %r15,%rdi
call   1040 <strlen@plt>
cmp    $0xf6,%eax
jge    12d1 <proc_get_task_hmpskched_avg+0x131>
cmp    $0x27,%r12
jb     1220 <proc_get_task_hmpskched_avg+0x80>
mov    %r15,%rdi
lea    0xdab(%rip),%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   10a0 <sscanf@plt>
cmp    $0x2,%eax
jne    12d1 <proc_get_task_hmpskched_avg+0x131>
lea    0xd9d(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rdx
lea    0xd8a(%rip),%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x8,%edx
call   1090 <memcpy@plt>
mov    0x8(%rsp),%rsi
mov    (%rbx),%rdx
lea    0xd6e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <proc_get_task_hmpskched_avg+0x80>
mov    0x2d08(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12e0 <proc_get_task_hmpskched_avg+0x140>
xor    %ebp,%ebp
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd0c(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cc9(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    12e8 <proc_get_task_hmpskched_avg+0x148>

LLVM IR (-O0):
%struct.hmpsched_avg = type { i32 }
@ENODEV = global i32 0
@.str = [15 x i8] c"/proc/%d/sched\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"open error\00"
@EIO = global i32 0
@.str.3 = [9 x i8] c"%s : %ld\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [13 x i8] c"%-45s:%21ld\0A\00"
@.str.6 = [12 x i8] c"%ld == %ld\0A\00"
define i32 @proc_get_task_hmpskched_avg(i32 %0, %struct.hmpsched_avg* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.hmpsched_avg*
%6 = alloca i32*
%7 = alloca [255 x i8]
%8 = alloca [255 x i8]
%9 = alloca [255 x i8]
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store %struct.hmpsched_avg* %1, %struct.hmpsched_avg** %5
store i32* null, i32** %6
%13 = load i32, i32* @ENODEV
%14 = sub nsw i32 0, %13
store i32 %14, i32* %11
store i32 0, i32* %12
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%16 = load i32, i32* %4
%17 = call i32 @snprintf(i8* %15, i32 255, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %26, label %22
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @ENODEV
%25 = sub nsw i32 0, %24
store i32 %25, i32* %3
br label %90
26: ; preds = %2
store i32 0, i32* %12
br label %27
27: ; preds = %82, %26
%28 = load i32*, i32** %6
%29 = call i32 @feof(i32* %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br i1 %31, label %32, label %85
32: ; preds = %27
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%34 = load i32*, i32** %6
%35 = call i32 @fgets(i8* %33, i32 255, i32* %34)
%36 = icmp ne i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %32
%38 = load i32, i32* @EIO
%39 = sub nsw i32 0, %38
store i32 %39, i32* %11
br label %86
40: ; preds = %32
%41 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%42 = call i32 @strlen(i8* %41)
%43 = icmp sgt i32 %42, 245
br i1 %43, label %44, label %47
44: ; preds = %40
%45 = load i32, i32* @EIO
%46 = sub nsw i32 0, %45
store i32 %46, i32* %11
br label %86
47: ; preds = %40
%48 = load i32, i32* %12
%49 = icmp slt i32 %48, 39
br i1 %49, label %50, label %51
50: ; preds = %47
br label %82
51: ; preds = %47
%52 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%53 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%54 = call i32 @sscanf(i8* %52, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %53, i64* %10)
store i32 %54, i32* %11
%55 = load i32, i32* %11
%56 = icmp ne i32 %55, 2
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = load i32, i32* @EIO
%59 = sub nsw i32 0, %58
store i32 %59, i32* %11
br label %86
60: ; preds = %51
%61 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %61)
%63 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%64 = load i64, i64* %10
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %63, i64 %64)
%66 = load %struct.hmpsched_avg*, %struct.hmpsched_avg** %5
%67 = bitcast %struct.hmpsched_avg* %66 to i64*
%68 = load i32, i32* %12
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds i64, i64* %67, i64 %69
%71 = getelementptr inbounds i64, i64* %70, i64 -39
%72 = call i32 @memcpy(i64* %71, i64* %10, i32 8)
%73 = load i64, i64* %10
%74 = load %struct.hmpsched_avg*, %struct.hmpsched_avg** %5
%75 = bitcast %struct.hmpsched_avg* %74 to i64*
%76 = load i32, i32* %12
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i64, i64* %75, i64 %77
%79 = getelementptr inbounds i64, i64* %78, i64 -39
%80 = load i64, i64* %79
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i64 %73, i64 %80)
br label %82
82: ; preds = %60, %50
%83 = load i32, i32* %12
%84 = add nsw i32 %83, 1
store i32 %84, i32* %12
br label %27
85: ; preds = %27
store i32 0, i32* %11
br label %86
86: ; preds = %85, %57, %44, %37
%87 = load i32*, i32** %6
%88 = call i32 @fclose(i32* %87)
%89 = load i32, i32* %11
store i32 %89, i32* %3
br label %90
90: ; preds = %86, %22
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.hmpsched_avg = type { i32 }
@ENODEV = global i32 0
@.str = [15 x i8] c"/proc/%d/sched\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [11 x i8] c"open error\00"
@EIO = global i32 0
@.str.3 = [9 x i8] c"%s : %ld\00"
@.str.4 = [3 x i8] c"%s\00"
@.str.5 = [13 x i8] c"%-45s:%21ld\0A\00"
@.str.6 = [12 x i8] c"%ld == %ld\0A\00"
define i32 @proc_get_task_hmpskched_avg(i32 %0, %struct.hmpsched_avg* %1) {
%3 = alloca [255 x i8]
%4 = alloca [255 x i8]
%5 = alloca [255 x i8]
%6 = alloca i64
%7 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%10 = bitcast i64* %6 to i8*
%11 = call i32 @snprintf(i8* %7, i32 255, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = call i32 @feof(i32* %12)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %58
17: ; preds = %14
%18 = getelementptr inbounds %struct.hmpsched_avg, %struct.hmpsched_avg* %1, i64 -78
%19 = bitcast %struct.hmpsched_avg* %18 to i64*
br label %24
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @ENODEV
%23 = sub nsw i32 0, %22
br label %61
24: ; preds = %17, %54
%25 = phi i64 [ 0, %17 ], [ %55, %54 ]
%26 = call i32 @fgets(i8* %8, i32 255, i32* %12)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @EIO
%30 = sub nsw i32 0, %29
br label %58
31: ; preds = %24
%32 = call i32 @strlen(i8* %8)
%33 = icmp sgt i32 %32, 245
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @EIO
%36 = sub nsw i32 0, %35
br label %58
37: ; preds = %31
%38 = icmp ult i64 %25, 39
br i1 %38, label %54, label %39
39: ; preds = %37
%40 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %9, i64* %6)
%41 = icmp eq i32 %40, 2
br i1 %41, label %45, label %42
42: ; preds = %39
%43 = load i32, i32* @EIO
%44 = sub nsw i32 0, %43
br label %58
45: ; preds = %39
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%47 = load i64, i64* %6
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %9, i64 %47)
%49 = getelementptr inbounds i64, i64* %19, i64 %25
%50 = call i32 @memcpy(i64* %49, i64* %6, i32 8)
%51 = load i64, i64* %6
%52 = load i64, i64* %49
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i64 %51, i64 %52)
br label %54
54: ; preds = %37, %45
%55 = add nuw i64 %25, 1
%56 = call i32 @feof(i32* %12)
%57 = icmp eq i32 %56, 0
br i1 %57, label %24, label %58
58: ; preds = %54, %14, %42, %34, %28
%59 = phi i32 [ %36, %34 ], [ %44, %42 ], [ %30, %28 ], [ 0, %14 ], [ 0, %54 ]
%60 = call i32 @fclose(i32* %12)
br label %61
61: ; preds = %58, %20
%62 = phi i32 [ %59, %58 ], [ %23, %20 ]
ret i32 %62
}