#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 EIO;
unsigned long 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*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned int);
int sscanf(char *, char *, char *, unsigned long *);
int strlen(char *);
unsigned long proc_get_task_hmpsched_offset(unsigned int pid,
                                            unsigned int offset) {
  FILE *fp = NULL;
  char filename[255];
  char line[255];
  char value_name[255];
  unsigned long value_data;
  unsigned int index = 0;
  int ret;
  snprintf(filename, 255,
           "/proc/%d"
           "/sched",
           pid);
  fp = fopen(filename, "r");
  if (!fp) {
    perror("open error");
    return -ENODEV;
  }
  for (index = 0; !feof(fp) && index <= offset; index++) {
    if (!fgets(line, 255, fp)) {
      ret = -EIO;
      goto error;
    }
    if (strlen(line) > (255 - 10)) {
      ret = -EIO;
      goto error;
    }
    if (index < 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);
  }
error:
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<proc_get_task_hmpsched_offset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x340,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x32c(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0xc(%rbp),%ecx
mov    $0xff,%esi
lea    0xe37(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x120(%rbp),%rdi
lea    0xe3c(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1211 <proc_get_task_hmpsched_offset+0x81>
lea    0xe18(%rip),%rdi
call   10b0 <perror@plt>
xor    %eax,%eax
mov    0x2dd3(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    1377 <proc_get_task_hmpsched_offset+0x1e7>
movl   $0x0,-0x32c(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x331(%rbp)
jne    1249 <proc_get_task_hmpsched_offset+0xb9>
mov    -0x32c(%rbp),%eax
cmp    -0x10(%rbp),%eax
setbe  %al
mov    %al,-0x331(%rbp)
mov    -0x331(%rbp),%al
test   $0x1,%al
jne    125c <proc_get_task_hmpsched_offset+0xcc>
jmp    135e <proc_get_task_hmpsched_offset+0x1ce>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xff,%esi
call   1070 <fgets@plt>
cmp    $0x0,%eax
jne    1290 <proc_get_task_hmpsched_offset+0x100>
xor    %eax,%eax
mov    0x2d5d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x330(%rbp)
jmp    1363 <proc_get_task_hmpsched_offset+0x1d3>
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0xf5,%eax
jle    12bd <proc_get_task_hmpsched_offset+0x12d>
xor    %eax,%eax
mov    0x2d30(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x330(%rbp)
jmp    1363 <proc_get_task_hmpsched_offset+0x1d3>
cmpl   $0x27,-0x32c(%rbp)
jae    12cf <proc_get_task_hmpsched_offset+0x13f>
jmp    134a <proc_get_task_hmpsched_offset+0x1ba>
lea    -0x220(%rbp),%rdi
lea    -0x320(%rbp),%rdx
lea    0xd36(%rip),%rsi
lea    -0x328(%rbp),%rcx
call   1090 <sscanf@plt>
mov    %eax,-0x330(%rbp)
cmpl   $0x2,-0x330(%rbp)
je     1319 <proc_get_task_hmpsched_offset+0x189>
xor    %eax,%eax
mov    0x2cd4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x330(%rbp)
jmp    1363 <proc_get_task_hmpsched_offset+0x1d3>
lea    -0x220(%rbp),%rsi
lea    0xcfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x320(%rbp),%rsi
mov    -0x328(%rbp),%rdx
lea    0xce3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x32c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x32c(%rbp)
jmp    121b <proc_get_task_hmpsched_offset+0x8b>
jmp    1363 <proc_get_task_hmpsched_offset+0x1d3>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movslq -0x330(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x340,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_get_task_hmpsched_offset>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %esi,%r14d
mov    %edi,%ecx
lea    0xe53(%rip),%rdx
lea    0x210(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1060 <snprintf@plt>
lea    0xe4f(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12c1 <proc_get_task_hmpsched_offset+0x131>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <feof@plt>
mov    $0x2,%r15d
test   %eax,%eax
jne    12a4 <proc_get_task_hmpsched_offset+0x114>
lea    0x110(%rsp),%rbp
lea    0x10(%rsp),%r12
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%rdi
mov    $0xff,%esi
mov    %rbx,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     1297 <proc_get_task_hmpsched_offset+0x107>
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    $0xf6,%eax
jge    1297 <proc_get_task_hmpsched_offset+0x107>
cmp    $0x27,%r13d
jb     127c <proc_get_task_hmpsched_offset+0xec>
mov    %rbp,%rdi
lea    0xdd7(%rip),%rsi
mov    %r12,%rdx
lea    0x8(%rsp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    1297 <proc_get_task_hmpsched_offset+0x107>
lea    0xdc7(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%rdx
lea    0xdb4(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    12a4 <proc_get_task_hmpsched_offset+0x114>
add    $0x1,%r13d
cmp    %r14d,%r13d
jbe    1210 <proc_get_task_hmpsched_offset+0x80>
jmp    12a4 <proc_get_task_hmpsched_offset+0x114>
mov    0x2d42(%rip),%rax
xor    %r15d,%r15d
sub    (%rax),%r15d
mov    %rbx,%rdi
call   1030 <fclose@plt>
movslq %r15d,%rax
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd47(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d04(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%rax
jmp    12af <proc_get_task_hmpsched_offset+0x11f>

LLVM IR (-O0):
@.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"
@ENODEV = global i64 0
@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"
define i64 @proc_get_task_hmpsched_offset(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%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 i32 %1, i32* %5
store i32* null, i32** %6
store i32 0, i32* %11
%13 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%14 = load i32, i32* %4
%15 = call i32 @snprintf(i8* %13, i32 255, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %6
%18 = load i32*, i32** %6
%19 = icmp ne i32* %18, null
br i1 %19, label %24, label %20
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%22 = load i64, i64* @ENODEV
%23 = sub i64 0, %22
store i64 %23, i64* %3
br label %78
24: ; preds = %2
store i32 0, i32* %11
br label %25
25: ; preds = %69, %24
%26 = load i32*, i32** %6
%27 = call i32 @feof(i32* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %25
%30 = load i32, i32* %11
%31 = load i32, i32* %5
%32 = icmp ule i32 %30, %31
br label %33
33: ; preds = %29, %25
%34 = phi i1 [ false, %25 ], [ %32, %29 ]
br i1 %34, label %35, label %72
35: ; preds = %33
%36 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%37 = load i32*, i32** %6
%38 = call i32 @fgets(i8* %36, i32 255, i32* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = load i32, i32* @EIO
%42 = sub nsw i32 0, %41
store i32 %42, i32* %12
br label %73
43: ; preds = %35
%44 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%45 = call i32 @strlen(i8* %44)
%46 = icmp sgt i32 %45, 245
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @EIO
%49 = sub nsw i32 0, %48
store i32 %49, i32* %12
br label %73
50: ; preds = %43
%51 = load i32, i32* %11
%52 = icmp ult i32 %51, 39
br i1 %52, label %53, label %54
53: ; preds = %50
br label %69
54: ; preds = %50
%55 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%56 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%57 = call i32 @sscanf(i8* %55, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %56, i64* %10)
store i32 %57, i32* %12
%58 = load i32, i32* %12
%59 = icmp ne i32 %58, 2
br i1 %59, label %60, label %63
60: ; preds = %54
%61 = load i32, i32* @EIO
%62 = sub nsw i32 0, %61
store i32 %62, i32* %12
br label %73
63: ; preds = %54
%64 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%65 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %64)
%66 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%67 = load i64, i64* %10
%68 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %66, i64 %67)
br label %69
69: ; preds = %63, %53
%70 = load i32, i32* %11
%71 = add i32 %70, 1
store i32 %71, i32* %11
br label %25
72: ; preds = %33
br label %73
73: ; preds = %72, %60, %47, %40
%74 = load i32*, i32** %6
%75 = call i32 @fclose(i32* %74)
%76 = load i32, i32* %12
%77 = sext i32 %76 to i64
store i64 %77, i64* %3
br label %78
78: ; preds = %73, %20
%79 = load i64, i64* %3
ret i64 %79
}

LLVM IR (-O2):
@.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"
@ENODEV = global i64 0
@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"
define i64 @proc_get_task_hmpsched_offset(i32 %0, i32 %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 %17, label %14
14: ; preds = %2
%15 = call i32 @feof(i32* %12)
%16 = icmp eq i32 %15, 0
br i1 %16, label %21, label %52
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%19 = load i64, i64* @ENODEV
%20 = sub i64 0, %19
br label %56
21: ; preds = %14, %46
%22 = phi i32 [ %47, %46 ], [ 0, %14 ]
%23 = call i32 @fgets(i8* %8, i32 255, i32* %12)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* @EIO
%27 = sub nsw i32 0, %26
br label %52
28: ; preds = %21
%29 = call i32 @strlen(i8* %8)
%30 = icmp sgt i32 %29, 245
br i1 %30, label %31, label %34
31: ; preds = %28
%32 = load i32, i32* @EIO
%33 = sub nsw i32 0, %32
br label %52
34: ; preds = %28
%35 = icmp ult i32 %22, 39
br i1 %35, label %46, label %36
36: ; preds = %34
%37 = call i32 @sscanf(i8* %8, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %9, i64* %6)
%38 = icmp eq i32 %37, 2
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = load i32, i32* @EIO
%41 = sub nsw i32 0, %40
br label %52
42: ; preds = %36
%43 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %8)
%44 = load i64, i64* %6
%45 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %9, i64 %44)
br label %46
46: ; preds = %34, %42
%47 = add i32 %22, 1
%48 = call i32 @feof(i32* %12)
%49 = icmp eq i32 %48, 0
%50 = icmp ule i32 %47, %1
%51 = select i1 %49, i1 %50, i1 false
br i1 %51, label %21, label %52
52: ; preds = %46, %14, %39, %31, %25
%53 = phi i32 [ %33, %31 ], [ %41, %39 ], [ %27, %25 ], [ 2, %14 ], [ 2, %46 ]
%54 = call i32 @fclose(i32* %12)
%55 = sext i32 %53 to i64
br label %56
56: ; preds = %52, %17
%57 = phi i64 [ %55, %52 ], [ %20, %17 ]
ret i64 %57
}