#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) {
    free_tracebuffer(buffer);
    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    1202 <read_tracebuffer+0xc2>
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    11bf <read_tracebuffer+0x7f>
mov    -0x20(%rbp),%rdi
call   1040 <free_tracebuffer@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1202 <read_tracebuffer+0xc2>
jmp    11c4 <read_tracebuffer+0x84>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    11f2 <read_tracebuffer+0xb2>
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    11c4 <read_tracebuffer+0x84>
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   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
call   1050 <alloc_tracebuffer@plt>
test   %rax,%rax
je     1194 <read_tracebuffer+0x54>
mov    %rax,%r15
mov    %rax,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
test   %eax,%eax
js     119c <read_tracebuffer+0x5c>
je     11a4 <read_tracebuffer+0x64>
mov    %eax,%ebp
xor    %r14d,%r14d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rbx
lea    (%r15,%rbx,1),%rdi
call   1060 <print_packet@plt>
add    %eax,%ebx
cmp    %ebp,%ebx
jl     1180 <read_tracebuffer+0x40>
jmp    11a7 <read_tracebuffer+0x67>
mov    $0xffffffff,%r14d
jmp    11af <read_tracebuffer+0x6f>
mov    $0xffffffff,%r14d
jmp    11a7 <read_tracebuffer+0x67>
xor    %r14d,%r14d
mov    %r15,%rdi
call   1040 <free_tracebuffer@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
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 %43
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 %24
21: ; preds = %14
%22 = load i8*, i8** %6
%23 = call i32 @free_tracebuffer(i8* %22)
store i32 -1, i32* %3
br label %43
24: ; preds = %14
br label %25
25: ; preds = %29, %24
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %40
29: ; preds = %25
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = bitcast i8* %33 to %struct.trace_packet*
%35 = call i64 @print_packet(%struct.trace_packet* %34)
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = add nsw i64 %37, %35
%39 = trunc i64 %38 to i32
store i32 %39, i32* %8
br label %25
40: ; preds = %25
%41 = load i8*, i8** %6
%42 = call i32 @free_tracebuffer(i8* %41)
store i32 0, i32* %3
br label %43
43: ; preds = %40, %21, %13
%44 = load i32, i32* %3
ret i32 %44
}

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 %22, 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 %19, 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, %5
%20 = phi i32 [ -1, %5 ], [ 0, %8 ], [ 0, %10 ]
%21 = tail call i32 @free_tracebuffer(i8* %3)
br label %22
22: ; preds = %19, %2
%23 = phi i32 [ -1, %2 ], [ %20, %19 ]
ret i32 %23
}