#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 */
typedef int uint8_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int *malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
int read_from_serial_device(int device_file, uint8_t *data) {
  int bytesRead;
  uint8_t buffer[256];
  bytesRead = read(device_file, buffer, sizeof(buffer));
  data = malloc(sizeof(uint8_t) * bytesRead);
  if (bytesRead > 0) {
    int i = 0;
    printf("Received: ");
    for (; i < bytesRead; i++) {
      data[i] = buffer[i];
      printf("0x%x, ", data[i]);
    }
    printf("\n");
  }
  return bytesRead;
}


x86 assembly (clang, -O0):
<read_from_serial_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11f7 <read_from_serial_device+0xc7>
movl   $0x0,-0x424(%rbp)
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x424(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11e9 <read_from_serial_device+0xb9>
movslq -0x424(%rbp),%rax
mov    -0x420(%rbp,%rax,4),%edx
mov    -0x10(%rbp),%rax
movslq -0x424(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
movslq -0x424(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    118e <read_from_serial_device+0x5e>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_serial_device>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsp,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%r14d
lea    0x0(,%r14,4),%edi
call   1060 <malloc@plt>
test   %r14d,%r14d
jle    11b4 <read_from_serial_device+0x74>
mov    %rax,%r12
lea    0xe84(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%r13d
lea    0xe7e(%rip),%r15
xor    %ebx,%ebx
nop
mov    (%rsp,%rbx,4),%esi
mov    %esi,(%r12,%rbx,4)
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1190 <read_from_serial_device+0x50>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [11 x i8] c"Received: \00"
@.str.1 = [7 x i8] c"0x%x, \00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @read_from_serial_device(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [256 x i32]
%7 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%8 = load i32, i32* %3
%9 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 0
%10 = call i32 @read(i32 %8, i32* %9, i32 1024)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = sext i32 %11 to i64
%13 = mul i64 4, %12
%14 = trunc i64 %13 to i32
%15 = call i32* @malloc(i32 %14)
store i32* %15, i32** %4
%16 = load i32, i32* %5
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %44
18: ; preds = %2
store i32 0, i32* %7
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %39, %18
%21 = load i32, i32* %7
%22 = load i32, i32* %5
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %42
24: ; preds = %20
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [256 x i32], [256 x i32]* %6, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = load i32*, i32** %4
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %29, i64 %31
store i32 %28, i32* %32
%33 = load i32*, i32** %4
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %33, i64 %35
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %24
%40 = load i32, i32* %7
%41 = add nsw i32 %40, 1
store i32 %41, i32* %7
br label %20
42: ; preds = %20
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %2
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@.str = [11 x i8] c"Received: \00"
@.str.1 = [7 x i8] c"0x%x, \00"
define i32 @read_from_serial_device(i32 %0, i32* nocapture readnone %1) {
%3 = alloca [256 x i32]
%4 = bitcast [256 x i32]* %3 to i8*
%5 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 0
%6 = call i32 @read(i32 %0, i32* %5, i32 1024)
%7 = shl i32 %6, 2
%8 = tail call i32* @malloc(i32 %7)
%9 = icmp sgt i32 %6, 0
br i1 %9, label %10, label %23
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%12 = zext i32 %6 to i64
br label %13
13: ; preds = %10, %13
%14 = phi i64 [ 0, %10 ], [ %19, %13 ]
%15 = getelementptr inbounds [256 x i32], [256 x i32]* %3, i64 0, i64 %14
%16 = load i32, i32* %15
%17 = getelementptr inbounds i32, i32* %8, i64 %14
store i32 %16, i32* %17
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%19 = add nuw nsw i64 %14, 1
%20 = icmp eq i64 %19, %12
br i1 %20, label %21, label %13
21: ; preds = %13
%22 = tail call i32 @putchar(i32 10)
br label %23
23: ; preds = %21, %2
ret i32 %6
}