#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 */
struct can_frame {
  int dummy;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int read(int, struct can_frame *, int);
int /*<<< orphan*/ stderr;
int can_receive_frame(int fd, struct can_frame *msg) {
  ssize_t len;
  len = read(fd, msg, sizeof(struct can_frame));
  if (len < 0) {
    perror("Reading from can socket failed");
    return -1;
  }
  if (len != sizeof(struct can_frame)) {
    do {
      fprintf(stderr, "Received incomplete can frame"
                      "\n");
    } while (0);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<can_receive_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1175 <can_receive_frame+0x45>
lea    0xe9c(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <can_receive_frame+0x80>
movslq -0x14(%rbp),%rax
cmp    $0x4,%rax
je     11a9 <can_receive_frame+0x79>
jmp    1188 <can_receive_frame+0x58>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b0 <can_receive_frame+0x80>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_receive_frame>:
push   %rax
mov    $0x4,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1164 <can_receive_frame+0x34>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x4,%ecx
je     1162 <can_receive_frame+0x32>
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
lea    0xec7(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rcx
ret
lea    0xe95(%rip),%rdi
call   1050 <perror@plt>
jmp    115d <can_receive_frame+0x2d>

LLVM IR (-O0):
%struct.can_frame = type { i32 }
@.str = [31 x i8] c"Reading from can socket failed\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Received incomplete can frame\0A\00"
define i32 @can_receive_frame(i32 %0, %struct.can_frame* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.can_frame*
%6 = alloca i32
store i32 %0, i32* %4
store %struct.can_frame* %1, %struct.can_frame** %5
%7 = load i32, i32* %4
%8 = load %struct.can_frame*, %struct.can_frame** %5
%9 = call i32 @read(i32 %7, %struct.can_frame* %8, i32 4)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %24
14: ; preds = %2
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = icmp ne i64 %16, 4
br i1 %17, label %18, label %23
18: ; preds = %14
br label %19
19: ; preds = %18
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %19
store i32 -1, i32* %3
br label %24
23: ; preds = %14
store i32 0, i32* %3
br label %24
24: ; preds = %23, %22, %12
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.can_frame = type { i32 }
@.str = [31 x i8] c"Reading from can socket failed\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"Received incomplete can frame\0A\00"
define i32 @can_receive_frame(i32 %0, %struct.can_frame* nocapture %1) {
%3 = tail call i32 @read(i32 %0, %struct.can_frame* %1, i32 4)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %12
7: ; preds = %2
%8 = icmp eq i32 %3, 4
br i1 %8, label %12, label %9
9: ; preds = %7
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %7, %9, %5
%13 = phi i32 [ -1, %5 ], [ -1, %9 ], [ 0, %7 ]
ret i32 %13
}