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

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
char *get_instance_file(struct buffer_instance *, char *);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ trace_seq_printf(struct trace_seq *, char *, int, char *);
int /*<<< orphan*/ tracecmd_put_tracing_file(char *);
void tracecmd_stat_cpu_instance(struct buffer_instance *instance,
                                struct trace_seq *s, int cpu) {
  char buf[BUFSIZ];
  char *path;
  char *file;
  int fd;
  int r;
  file = malloc(40);
  if (!file)
    return;
  snprintf(file, 40, "per_cpu/cpu%d/stats", cpu);
  path = get_instance_file(instance, file);
  free(file);
  fd = open(path, O_RDONLY);
  tracecmd_put_tracing_file(path);
  if (fd < 0)
    return;
  while ((r = read(fd, buf, BUFSIZ)) > 0)
    trace_seq_printf(s, "%.*s", r, buf);
  close(fd);
}


x86 assembly (clang, -O0):
<tracecmd_stat_cpu_instance>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    $0x28,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11f4 <tracecmd_stat_cpu_instance+0x64>
movl   $0x1,-0x44(%rbp)
jmp    12ab <tracecmd_stat_cpu_instance+0x11b>
mov    -0x38(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x28,%esi
lea    0xdf9(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <get_instance_file@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rdi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <tracecmd_put_tracing_file@plt>
cmpl   $0x0,-0x3c(%rbp)
jge    125a <tracecmd_stat_cpu_instance+0xca>
movl   $0x1,-0x44(%rbp)
jmp    12ab <tracecmd_stat_cpu_instance+0x11b>
jmp    125f <tracecmd_stat_cpu_instance+0xcf>
mov    -0x50(%rbp),%rsi
mov    -0x3c(%rbp),%edi
mov    0x2d6b(%rip),%rax
mov    (%rax),%edx
call   1090 <read@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
jle    129c <tracecmd_stat_cpu_instance+0x10c>
mov    -0x50(%rbp),%rcx
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%edx
lea    0xd82(%rip),%rsi
call   1060 <trace_seq_printf@plt>
jmp    125f <tracecmd_stat_cpu_instance+0xcf>
mov    -0x3c(%rbp),%edi
call   1080 <close@plt>
movl   $0x0,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tracecmd_stat_cpu_instance>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    $0x28,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     127b <tracecmd_stat_cpu_instance+0xeb>
mov    %rax,%r13
lea    0xe22(%rip),%rdx
mov    %rax,%rdi
mov    $0x28,%esi
mov    %r12d,%ecx
call   1070 <snprintf@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1050 <get_instance_file@plt>
mov    %rax,%r12
mov    %r13,%rdi
call   1040 <free@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10b0 <open@plt>
mov    %eax,%r15d
mov    %r12,%rdi
call   1030 <tracecmd_put_tracing_file@plt>
test   %r15d,%r15d
js     127b <tracecmd_stat_cpu_instance+0xeb>
mov    0x2dac(%rip),%r13
mov    0x0(%r13),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1090 <read@plt>
test   %eax,%eax
jle    1273 <tracecmd_stat_cpu_instance+0xe3>
lea    0xdce(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %eax,%edx
mov    %rbx,%rcx
call   1060 <trace_seq_printf@plt>
mov    0x0(%r13),%edx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1090 <read@plt>
test   %eax,%eax
jg     1250 <tracecmd_stat_cpu_instance+0xc0>
mov    %r15d,%edi
call   1080 <close@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.buffer_instance = type { i32 }
%struct.trace_seq = type { i32 }
@BUFSIZ = global i32 0
@.str = [20 x i8] c"per_cpu/cpu%d/stats\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"%.*s\00"
define void @tracecmd_stat_cpu_instance(%struct.buffer_instance* %0, %struct.trace_seq* %1, i32 %2) {
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call dereferenceable_or_null(40) i8* @malloc(i32 40)
%8 = icmp eq i8* %7, null
br i1 %8, label %29, label %9
9: ; preds = %3
%10 = tail call i32 @snprintf(i8* %7, i32 40, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %2)
%11 = tail call i8* @get_instance_file(%struct.buffer_instance* %0, i8* %7)
%12 = tail call i32 @free(i8* %7)
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %11, i32 %13)
%15 = tail call i32 @tracecmd_put_tracing_file(i8* %11)
%16 = icmp slt i32 %14, 0
br i1 %16, label %29, label %17
17: ; preds = %9
%18 = load i32, i32* @BUFSIZ
%19 = call i32 @read(i32 %14, i8* %6, 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* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %22, i8* %6)
%24 = load i32, i32* @BUFSIZ
%25 = call i32 @read(i32 %14, i8* %6, i32 %24)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %21, label %27
27: ; preds = %21, %17
%28 = call i32 @close(i32 %14)
br label %29
29: ; preds = %9, %3, %27
ret void
}