#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 /*<<< orphan*/ uint32_t;
struct pollfd {
  int fd;
  int /*<<< orphan*/ revents;
  int /*<<< orphan*/ events;
};

/* Variables and functions */
int /*<<< orphan*/ POLLIN;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int read(int, int /*<<< orphan*/ *, int);
uint32_t serial_recv(int fd) {
  struct pollfd pfd = {.fd = fd, .events = POLLIN, .revents = 0};
  poll(&pfd, 1, 10000);
  uint32_t ret = 0;
  if (read(fd, &ret, 4) != 4) {
    printf("short read\n");
    exit(2);
  }
  return ret;
}


x86 assembly (clang, -O0):
<serial_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1050 <poll@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
je     11ae <serial_recv+0x6e>
lea    0xe61(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_recv>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
mov    %edi,0x10(%rsp)
movl   $0x0,0x14(%rsp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x2710,%edx
call   1050 <poll@plt>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jne    119c <serial_recv+0x5c>
mov    0xc(%rsp),%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@.str = [12 x i8] c"short read\0A\00"
define i32 @serial_recv(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.pollfd
%4 = alloca i32
store i32 %0, i32* %2
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 0
%6 = load i32, i32* %2
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 1
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i32 0, i32 2
%9 = load i32, i32* @POLLIN
store i32 %9, i32* %8
%10 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 10000)
store i32 0, i32* %4
%11 = load i32, i32* %2
%12 = call i32 @read(i32 %11, i32* %4, i32 4)
%13 = icmp ne i32 %12, 4
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %1
%18 = load i32, i32* %4
ret i32 %18
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i32, i32 }
@POLLIN = global i32 0
@str = [11 x i8] c"short read\00"
define i32 @serial_recv(i32 %0) {
%2 = alloca %struct.pollfd
%3 = alloca i32
%4 = bitcast %struct.pollfd* %2 to i8*
%5 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %0, i32* %5
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 2
%8 = load i32, i32* @POLLIN
store i32 %8, i32* %7
%9 = call i32 @poll(%struct.pollfd* %2, i32 1, i32 10000)
%10 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%11 = call i32 @read(i32 %0, i32* %3, i32 4)
%12 = icmp eq i32 %11, 4
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%15 = call i32 @exit(i32 2)
unreachable
16: ; preds = %1
%17 = load i32, i32* %3
ret i32 %17
}