#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 /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int serial_read(int fd, char readbuff[], int datalen) {
  int nread = 0;
  printf("Ready for receiving data...");
  nread = read(fd, readbuff, datalen);
  if (nread > 0) {
    printf("readlength=%d\n", nread);
  }
  return nread;
}


x86 assembly (clang, -O0):
<serial_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
lea    0xec0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1174 <serial_read+0x54>
mov    -0x18(%rbp),%esi
lea    0xeaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_read>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xecd(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    115d <serial_read+0x3d>
lea    0xec8(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Ready for receiving data...\00"
@.str.1 = [15 x i8] c"readlength=%d\0A\00"
define i32 @serial_read(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %4
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call i32 @read(i32 %9, i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* %7
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %15, %3
%19 = load i32, i32* %7
ret i32 %19
}

LLVM IR (-O2):
@.str = [28 x i8] c"Ready for receiving data...\00"
@.str.1 = [15 x i8] c"readlength=%d\0A\00"
define i32 @serial_read(i32 %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @read(i32 %0, i8* %1, i32 %2)
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %9
9: ; preds = %7, %3
ret i32 %5
}