#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*/ uint8_t;
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);
int /*<<< orphan*/ write(int, int /*<<< orphan*/ *, int);
uint32_t serial_recv(int fd) {
  uint8_t send[5] = {0};
  write(fd, send, 5);
  struct pollfd pfd = {.fd = fd, .events = POLLIN, .revents = 0};
  poll(&pfd, 1, 1000000);
  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    $0x40,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x1,%esi
mov    $0xf4240,%edx
call   1070 <poll@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cmp    $0x4,%eax
je     11ef <serial_recv+0x8f>
lea    0xe20(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_recv>:
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movl   $0x0,0x30(%rsp)
lea    0x20(%rsp),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    %ebx,0x10(%rsp)
movl   $0x0,0x14(%rsp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0xf4240,%edx
call   1060 <poll@plt>
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
jne    11cb <serial_recv+0x7b>
mov    0xc(%rsp),%eax
add    $0x40,%rsp
pop    %rbx
ret
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1070 <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 [5 x i32]
%4 = alloca %struct.pollfd
%5 = alloca i32
store i32 %0, i32* %2
%6 = bitcast [5 x i32]* %3 to i8*
%7 = load i32, i32* %2
%8 = getelementptr inbounds [5 x i32], [5 x i32]* %3, i64 0, i64 0
%9 = call i32 @write(i32 %7, i32* %8, i32 5)
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
%11 = load i32, i32* %2
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 2
%14 = load i32, i32* @POLLIN
store i32 %14, i32* %13
%15 = call i32 @poll(%struct.pollfd* %4, i32 1, i32 1000000)
store i32 0, i32* %5
%16 = load i32, i32* %2
%17 = call i32 @read(i32 %16, i32* %5, i32 4)
%18 = icmp ne i32 %17, 4
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %1
%23 = load i32, i32* %5
ret i32 %23
}

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 [5 x i32]
%3 = alloca %struct.pollfd
%4 = alloca i32
%5 = bitcast [5 x i32]* %2 to i8*
%6 = getelementptr inbounds [5 x i32], [5 x i32]* %2, i64 0, i64 0
%7 = call i32 @write(i32 %0, i32* %6, i32 5)
%8 = bitcast %struct.pollfd* %3 to i8*
%9 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 0
store i32 %0, i32* %9
%10 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 1
store i32 0, i32* %10
%11 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %3, i64 0, i32 2
%12 = load i32, i32* @POLLIN
store i32 %12, i32* %11
%13 = call i32 @poll(%struct.pollfd* %3, i32 1, i32 1000000)
%14 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%15 = call i32 @read(i32 %0, i32* %4, i32 4)
%16 = icmp eq i32 %15, 4
br i1 %16, label %20, label %17
17: ; preds = %1
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%19 = call i32 @exit(i32 2)
unreachable
20: ; preds = %1
%21 = load i32, i32* %4
ret i32 %21
}