#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 bcm4751_serial_read(int, void *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ hex_dump(void *, int);
void *malloc(int);
int /*<<< orphan*/ printf(char *, int);
void bcm4751_read_dump(int fd) {
  void *data = NULL;
  int length = 0x100;
  int rc = -1;
  data = malloc(length);
  while (1) {
    rc = bcm4751_serial_read(fd, data, length, NULL);
    if (rc > 0) {
      printf("Read %d bytes:\n", rc);
      hex_dump(data, rc);
    } else {
      free(data);
      return;
    }
  }
}


x86 assembly (clang, -O0):
<bcm4751_read_dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x100,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <bcm4751_serial_read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11bd <bcm4751_read_dump+0x6d>
mov    -0x18(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <hex_dump@plt>
jmp    11cc <bcm4751_read_dump+0x7c>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret
jmp    117d <bcm4751_read_dump+0x2d>

x86 assembly (clang, -O2):
<bcm4751_read_dump>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    $0x100,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %rax,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1070 <bcm4751_serial_read@plt>
test   %eax,%eax
jle    11bc <bcm4751_read_dump+0x6c>
mov    %eax,%ebp
lea    0xe7a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <hex_dump@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1070 <bcm4751_serial_read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1190 <bcm4751_read_dump+0x40>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"Read %d bytes:\0A\00"
define void @bcm4751_read_dump(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
store i8* null, i8** %3
store i32 256, i32* %4
store i32 -1, i32* %5
%6 = load i32, i32* %4
%7 = call i8* @malloc(i32 %6)
store i8* %7, i8** %3
br label %8
8: ; preds = %1, %24
%9 = load i32, i32* %2
%10 = load i8*, i8** %3
%11 = load i32, i32* %4
%12 = call i32 @bcm4751_serial_read(i32 %9, i8* %10, i32 %11, i32* null)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %8
%16 = load i32, i32* %5
%17 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = call i32 @hex_dump(i8* %18, i32 %19)
br label %24
21: ; preds = %8
%22 = load i8*, i8** %3
%23 = call i32 @free(i8* %22)
ret void
24: ; preds = %15
br label %8
}

LLVM IR (-O2):
@.str = [16 x i8] c"Read %d bytes:\0A\00"
define void @bcm4751_read_dump(i32 %0) {
%2 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%3 = tail call i32 @bcm4751_serial_read(i32 %0, i8* %2, i32 256, i32* null)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %11
5: ; preds = %1, %5
%6 = phi i32 [ %9, %5 ], [ %3, %1 ]
%7 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @hex_dump(i8* %2, i32 %6)
%9 = tail call i32 @bcm4751_serial_read(i32 %0, i8* %2, i32 256, i32* null)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %5, label %11
11: ; preds = %5, %1
%12 = tail call i32 @free(i8* %2)
ret void
}