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

/* Variables and functions */
char *alloc_tracebuffer(int);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free_tracebuffer(char *);
scalar_t__ print_packet(struct trace_packet *);
int read_tracebuffer(FILE *file, int bufsize) {
  char *buffer = NULL;
  int read_len = 0, offset = 0;
  buffer = alloc_tracebuffer(bufsize);
  if (buffer == NULL) {
    return -1;
  }
  read_len = fread(buffer, sizeof(char), bufsize, file);
  if (read_len < 0) {
    return -1;
  }
  while (offset < read_len) {
    offset += print_packet((struct trace_packet *)(buffer + offset));
  }
  free_tracebuffer(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<read_tracebuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <alloc_tracebuffer@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1188 <read_tracebuffer+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <read_tracebuffer+0xb9>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11b6 <read_tracebuffer+0x76>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f9 <read_tracebuffer+0xb9>
jmp    11bb <read_tracebuffer+0x7b>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11e9 <read_tracebuffer+0xa9>
mov    -0x20(%rbp),%rdi
movslq -0x28(%rbp),%rax
add    %rax,%rdi
call   1060 <print_packet@plt>
mov    %rax,%rcx
movslq -0x28(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x28(%rbp)
jmp    11bb <read_tracebuffer+0x7b>
mov    -0x20(%rbp),%rdi
call   1040 <free_tracebuffer@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_tracebuffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r15
mov    %esi,%edi
call   1050 <alloc_tracebuffer@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     119d <read_tracebuffer+0x5d>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
js     119d <read_tracebuffer+0x5d>
je     1192 <read_tracebuffer+0x52>
mov    %eax,%ebp
xor    %ebx,%ebx
xchg   %ax,%ax
movslq %ebx,%rbx
lea    (%r12,%rbx,1),%rdi
call   1060 <print_packet@plt>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1180 <read_tracebuffer+0x40>
mov    %r12,%rdi
call   1040 <free_tracebuffer@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.trace_packet = type { i32 }
define i32 @read_tracebuffer(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* null, i8** %6
store i32 0, i32* %7
store i32 0, i32* %8
%9 = load i32, i32* %5
%10 = call i8* @alloc_tracebuffer(i32 %9)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %41
14: ; preds = %2
%15 = load i8*, i8** %6
%16 = load i32, i32* %5
%17 = load i32*, i32** %4
%18 = call i32 @fread(i8* %15, i32 1, i32 %16, i32* %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %14
store i32 -1, i32* %3
br label %41
22: ; preds = %14
br label %23
23: ; preds = %27, %22
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %38
27: ; preds = %23
%28 = load i8*, i8** %6
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = bitcast i8* %31 to %struct.trace_packet*
%33 = call i64 @print_packet(%struct.trace_packet* %32)
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = add nsw i64 %35, %33
%37 = trunc i64 %36 to i32
store i32 %37, i32* %8
br label %23
38: ; preds = %23
%39 = load i8*, i8** %6
%40 = call i32 @free_tracebuffer(i8* %39)
store i32 0, i32* %3
br label %41
41: ; preds = %38, %21, %13
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.trace_packet = type { i32 }
define i32 @read_tracebuffer(i32* nocapture %0, i32 %1) {
%3 = tail call i8* @alloc_tracebuffer(i32 %1)
%4 = icmp eq i8* %3, null
br i1 %4, label %21, label %5
5: ; preds = %2
%6 = tail call i32 @fread(i8* %3, i32 1, i32 %1, i32* %0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %5
%9 = icmp eq i32 %6, 0
br i1 %9, label %19, label %10
10: ; preds = %8, %10
%11 = phi i32 [ %17, %10 ], [ 0, %8 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %3, i64 %12
%14 = bitcast i8* %13 to %struct.trace_packet*
%15 = tail call i64 @print_packet(%struct.trace_packet* %14)
%16 = trunc i64 %15 to i32
%17 = add i32 %11, %16
%18 = icmp slt i32 %17, %6
br i1 %18, label %10, label %19
19: ; preds = %10, %8
%20 = tail call i32 @free_tracebuffer(i8* %3)
br label %21
21: ; preds = %5, %2, %19
%22 = phi i32 [ 0, %19 ], [ -1, %2 ], [ -1, %5 ]
ret i32 %22
}