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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
scalar_t__ read_int(char *, int *);
scalar_t__ write_int(char *, int);
int idlestat_init_trace(unsigned int duration) {
  int bufsize;
  bufsize = 2 * 196 * 10000;
  bufsize += 196 * 100;
  bufsize = (bufsize * duration / (1 << 10)) + 1;
  if (write_int("/sys/kernel/debug/tracing"
                "/buffer_size_kb",
                bufsize))
    return -1;
  if (read_int("/sys/kernel/debug/tracing"
               "/buffer_total_size_kb",
               &bufsize))
    return -1;
  printf("Total trace buffer: %d kB\n", bufsize);
  if (write_int("/sys/kernel/debug/tracing"
                "/events/enable",
                0))
    return -1;
  if (write_int("/sys/kernel/debug/tracing"
                "/events/power/cpu_idle/enable",
                1))
    return -1;
  if (write_int("/sys/kernel/debug/tracing"
                "/events/power/cpu_frequency/enable",
                1))
    return -1;
  if (write_int("/sys/kernel/debug/tracing"
                "/events/irq/irq_handler_entry/enable",
                1))
    return -1;
  write_int("/sys/kernel/debug/tracing"
            "/events/ipi/ipi_entry/enable",
            1);
  return 0;
}


x86 assembly (clang, -O0):
<idlestat_init_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x3bd080,-0xc(%rbp)
mov    -0xc(%rbp),%eax
add    $0x4c90,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
imul   -0x8(%rbp),%eax
shr    $0xa,%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe99(%rip),%rdi
call   1040 <write_int@plt>
cmp    $0x0,%rax
je     1182 <idlestat_init_trace+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <idlestat_init_trace+0x138>
lea    0xea0(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1050 <read_int@plt>
cmp    $0x0,%rax
je     11a8 <idlestat_init_trace+0x78>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <idlestat_init_trace+0x138>
mov    -0xc(%rbp),%esi
lea    0xea6(%rip),%rdi
call   1030 <printf@plt>
lea    0xeb5(%rip),%rdi
xor    %esi,%esi
call   1040 <write_int@plt>
cmp    $0x0,%rax
je     11db <idlestat_init_trace+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <idlestat_init_trace+0x138>
lea    0xeb9(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_int@plt>
cmp    $0x0,%rax
je     1202 <idlestat_init_trace+0xd2>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <idlestat_init_trace+0x138>
lea    0xec9(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_int@plt>
cmp    $0x0,%rax
je     1229 <idlestat_init_trace+0xf9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <idlestat_init_trace+0x138>
lea    0xede(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_int@plt>
cmp    $0x0,%rax
je     1250 <idlestat_init_trace+0x120>
movl   $0xffffffff,-0x4(%rbp)
jmp    1268 <idlestat_init_trace+0x138>
lea    0xef5(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_int@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<idlestat_init_trace>:
push   %rbx
sub    $0x10,%rsp
imul   $0x3c1d10,%edi,%esi
shr    $0xa,%esi
add    $0x1,%esi
mov    %esi,0xc(%rsp)
lea    0xeb4(%rip),%rdi
call   1040 <write_int@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    11ed <idlestat_init_trace+0xbd>
lea    0xec3(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1050 <read_int@plt>
test   %rax,%rax
jne    11ed <idlestat_init_trace+0xbd>
mov    0xc(%rsp),%esi
lea    0xed8(%rip),%rdi
call   1030 <printf@plt>
lea    0xee7(%rip),%rdi
xor    %esi,%esi
call   1040 <write_int@plt>
test   %rax,%rax
jne    11ed <idlestat_init_trace+0xbd>
lea    0xefc(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_int@plt>
test   %rax,%rax
jne    11ed <idlestat_init_trace+0xbd>
lea    0xf1d(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_int@plt>
test   %rax,%rax
jne    11ed <idlestat_init_trace+0xbd>
lea    0xf43(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_int@plt>
test   %rax,%rax
jne    11ed <idlestat_init_trace+0xbd>
lea    0xf6b(%rip),%rdi
mov    $0x1,%esi
call   1040 <write_int@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [41 x i8] c"/sys/kernel/debug/tracing/buffer_size_kb\00"
@.str.1 = [47 x i8] c"/sys/kernel/debug/tracing/buffer_total_size_kb\00"
@.str.2 = [27 x i8] c"Total trace buffer: %d kB\0A\00"
@.str.3 = [40 x i8] c"/sys/kernel/debug/tracing/events/enable\00"
@.str.4 = [55 x i8] c"/sys/kernel/debug/tracing/events/power/cpu_idle/enable\00"
@.str.5 = [60 x i8] c"/sys/kernel/debug/tracing/events/power/cpu_frequency/enable\00"
@.str.6 = [62 x i8] c"/sys/kernel/debug/tracing/events/irq/irq_handler_entry/enable\00"
@.str.7 = [54 x i8] c"/sys/kernel/debug/tracing/events/ipi/ipi_entry/enable\00"
define i32 @idlestat_init_trace(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
store i32 3920000, i32* %4
%5 = load i32, i32* %4
%6 = add nsw i32 %5, 19600
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = load i32, i32* %3
%9 = mul i32 %7, %8
%10 = udiv i32 %9, 1024
%11 = add i32 %10, 1
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = call i64 @write_int(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
store i32 -1, i32* %2
br label %40
16: ; preds = %1
%17 = call i64 @read_int(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %16
store i32 -1, i32* %2
br label %40
20: ; preds = %16
%21 = load i32, i32* %4
%22 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i64 @write_int(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 0)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 -1, i32* %2
br label %40
26: ; preds = %20
%27 = call i64 @write_int(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i32 1)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 -1, i32* %2
br label %40
30: ; preds = %26
%31 = call i64 @write_int(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0), i32 1)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 -1, i32* %2
br label %40
34: ; preds = %30
%35 = call i64 @write_int(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.6, i64 0, i64 0), i32 1)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %34
store i32 -1, i32* %2
br label %40
38: ; preds = %34
%39 = call i64 @write_int(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0), i32 1)
store i32 0, i32* %2
br label %40
40: ; preds = %38, %37, %33, %29, %25, %19, %15
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
@.str = [41 x i8] c"/sys/kernel/debug/tracing/buffer_size_kb\00"
@.str.1 = [47 x i8] c"/sys/kernel/debug/tracing/buffer_total_size_kb\00"
@.str.2 = [27 x i8] c"Total trace buffer: %d kB\0A\00"
@.str.3 = [40 x i8] c"/sys/kernel/debug/tracing/events/enable\00"
@.str.4 = [55 x i8] c"/sys/kernel/debug/tracing/events/power/cpu_idle/enable\00"
@.str.5 = [60 x i8] c"/sys/kernel/debug/tracing/events/power/cpu_frequency/enable\00"
@.str.6 = [62 x i8] c"/sys/kernel/debug/tracing/events/irq/irq_handler_entry/enable\00"
@.str.7 = [54 x i8] c"/sys/kernel/debug/tracing/events/ipi/ipi_entry/enable\00"
define i32 @idlestat_init_trace(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = mul i32 %0, 3939600
%5 = lshr i32 %4, 10
%6 = add nuw nsw i32 %5, 1
store i32 %6, i32* %2
%7 = tail call i64 @write_int(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %1
%10 = call i64 @read_int(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %9
%13 = load i32, i32* %2
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = call i64 @write_int(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = call i64 @write_int(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0), i32 1)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %28
20: ; preds = %17
%21 = call i64 @write_int(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0), i32 1)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = call i64 @write_int(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.6, i64 0, i64 0), i32 1)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i64 @write_int(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.7, i64 0, i64 0), i32 1)
br label %28
28: ; preds = %23, %20, %17, %12, %9, %1, %26
%29 = phi i32 [ 0, %26 ], [ -1, %1 ], [ -1, %9 ], [ -1, %12 ], [ -1, %17 ], [ -1, %20 ], [ -1, %23 ]
ret i32 %29
}