#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 trace_seq {
  int dummy;
};

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ ENODEV;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc_or_die(scalar_t__);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ trace_seq_printf(struct trace_seq *, char *, int, char *);
char *tracecmd_find_tracing_dir();
void tracecmd_stat_cpu(struct trace_seq *s, int cpu) {
  char buf[BUFSIZ];
  char *tracing;
  char *path;
  int fd;
  int r;
  tracing = tracecmd_find_tracing_dir();
  if (!tracing) {
    errno = ENODEV;
    goto out_free;
  }
  path = malloc_or_die(strlen(tracing) + 40);
  if (!path)
    goto out_free;
  sprintf(path, "%s/per_cpu/cpu%d/stats", tracing, cpu);
  fd = open(path, O_RDONLY);
  if (fd < 0)
    goto out_free;
  while ((r = read(fd, buf, BUFSIZ)) > 0)
    trace_seq_printf(s, "%.*s", r, buf);
  close(fd);
out_free:
  free(path);
  free(tracing);
}


x86 assembly (clang, -O0):
<tracecmd_stat_cpu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10a0 <tracecmd_find_tracing_dir@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f8 <tracecmd_stat_cpu+0x68>
mov    0x2de0(%rip),%rax
mov    (%rax),%ecx
mov    0x2ddf(%rip),%rax
mov    %ecx,(%rax)
jmp    12a6 <tracecmd_stat_cpu+0x116>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x28,%rdi
call   1080 <malloc_or_die@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1221 <tracecmd_stat_cpu+0x91>
jmp    12a6 <tracecmd_stat_cpu+0x116>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xdcd(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    125c <tracecmd_stat_cpu+0xcc>
jmp    12a6 <tracecmd_stat_cpu+0x116>
jmp    1261 <tracecmd_stat_cpu+0xd1>
mov    -0x40(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jle    129e <tracecmd_stat_cpu+0x10e>
mov    -0x40(%rbp),%rcx
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    0xd83(%rip),%rsi
call   1040 <trace_seq_printf@plt>
jmp    1261 <tracecmd_stat_cpu+0xd1>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tracecmd_stat_cpu>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r13d
mov    %rdi,%r15
mov    0x2e2a(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %eax,%eax
call   10a0 <tracecmd_find_tracing_dir@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1050 <strlen@plt>
lea    0x28(%rax),%rdi
call   1080 <malloc_or_die@plt>
mov    %rax,%rdi
test   %rax,%rax
je     1273 <tracecmd_stat_cpu+0xe3>
lea    0xe10(%rip),%rsi
mov    %rdi,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    %r12,%rdx
mov    %r13d,%ecx
call   10b0 <sprintf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rdi
call   1090 <open@plt>
mov    -0x30(%rbp),%rdi
test   %eax,%eax
js     1273 <tracecmd_stat_cpu+0xe3>
mov    %eax,%r13d
mov    %r12,-0x38(%rbp)
mov    (%r14),%edx
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <read@plt>
test   %eax,%eax
jle    1263 <tracecmd_stat_cpu+0xd3>
mov    %r14,%r12
lea    0xdd8(%rip),%r14
nop
mov    %r15,%rdi
mov    %r14,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1040 <trace_seq_printf@plt>
mov    (%r12),%edx
mov    %r13d,%edi
mov    %rbx,%rsi
call   1070 <read@plt>
test   %eax,%eax
jg     1240 <tracecmd_stat_cpu+0xb0>
mov    %r13d,%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%r12
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.trace_seq = type { i32 }
@BUFSIZ = global i32 0
@ENODEV = global i32 0
@errno = global i32 0
@.str = [23 x i8] c"%s/per_cpu/cpu%d/stats\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"%.*s\00"
define void @tracecmd_stat_cpu(%struct.trace_seq* %0, i32 %1) {
%3 = alloca %struct.trace_seq*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store %struct.trace_seq* %0, %struct.trace_seq** %3
store i32 %1, i32* %4
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = call i8* (...) @tracecmd_find_tracing_dir()
store i8* %15, i8** %7
%16 = load i8*, i8** %7
%17 = icmp ne i8* %16, null
br i1 %17, label %20, label %18
18: ; preds = %2
%19 = load i32, i32* @ENODEV
store i32 %19, i32* @errno
br label %52
20: ; preds = %2
%21 = load i8*, i8** %7
%22 = call i64 @strlen(i8* %21)
%23 = add nsw i64 %22, 40
%24 = call i8* @malloc_or_die(i64 %23)
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp ne i8* %25, null
br i1 %26, label %28, label %27
27: ; preds = %20
br label %52
28: ; preds = %20
%29 = load i8*, i8** %8
%30 = load i8*, i8** %7
%31 = load i32, i32* %4
%32 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %30, i32 %31)
%33 = load i8*, i8** %8
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %28
br label %52
39: ; preds = %28
br label %40
40: ; preds = %45, %39
%41 = load i32, i32* %9
%42 = load i32, i32* @BUFSIZ
%43 = call i32 @read(i32 %41, i8* %14, i32 %42)
store i32 %43, i32* %10
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %40
%46 = load %struct.trace_seq*, %struct.trace_seq** %3
%47 = load i32, i32* %10
%48 = call i32 @trace_seq_printf(%struct.trace_seq* %46, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %47, i8* %14)
br label %40
49: ; preds = %40
%50 = load i32, i32* %9
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %38, %27, %18
%53 = load i8*, i8** %8
%54 = call i32 @free(i8* %53)
%55 = load i8*, i8** %7
%56 = call i32 @free(i8* %55)
%57 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
%struct.trace_seq = type { i32 }
@BUFSIZ = global i32 0
@ENODEV = global i32 0
@errno = global i32 0
@.str = [23 x i8] c"%s/per_cpu/cpu%d/stats\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"%.*s\00"
define void @tracecmd_stat_cpu(%struct.trace_seq* %0, i32 %1) {
%3 = load i32, i32* @BUFSIZ
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = tail call i8* (...) @tracecmd_find_tracing_dir()
%7 = icmp ne i8* %6, null
tail call void @llvm.assume(i1 %7)
%8 = tail call i64 @strlen(i8* %6)
%9 = add nsw i64 %8, 40
%10 = tail call i8* @malloc_or_die(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %29, label %12
12: ; preds = %2
%13 = tail call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %6, i32 %1)
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %10, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %29, label %17
17: ; preds = %12
%18 = load i32, i32* @BUFSIZ
%19 = call i32 @read(i32 %15, i8* %5, i32 %18)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %17, %21
%22 = phi i32 [ %25, %21 ], [ %19, %17 ]
%23 = call i32 @trace_seq_printf(%struct.trace_seq* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %22, i8* %5)
%24 = load i32, i32* @BUFSIZ
%25 = call i32 @read(i32 %15, i8* %5, i32 %24)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %21, label %27
27: ; preds = %21, %17
%28 = call i32 @close(i32 %15)
br label %29
29: ; preds = %12, %2, %27
%30 = call i32 @free(i8* %10)
%31 = call i32 @free(i8* %6)
ret void
}