#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 scalar_t__ uint8_t;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct can_frame {
  scalar_t__ *data;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ init_set_cmd(struct can_frame *);
int /*<<< orphan*/ memcpy(struct can_frame *, struct can_frame *, int);
int /*<<< orphan*/ perror(char *);
int read(int, struct can_frame *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int write(int, struct can_frame *, int);
uint8_t get_status(int s, uint8_t module_id, struct can_frame *cf) {
  struct can_frame frame;
  fd_set rdfs;
  struct timeval tv;
  int ret;
  init_set_cmd(&frame);
  frame.data[2] = module_id;
  frame.data[3] = 0;
  frame.data[4] = 0;
  frame.data[5] = 0;
  frame.data[6] = 0;
  if (write(s, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) {
    perror("write");
    exit(1);
  }
  FD_ZERO(&rdfs);
  FD_SET(s, &rdfs);
  tv.tv_sec = 3;
  tv.tv_usec = 0;
  ret = select(s + 1, &rdfs, NULL, NULL, &tv);
  if (ret < 0) {
    perror("select");
    exit(1);
  }
  if (FD_ISSET(s, &rdfs)) {
    ret = read(s, &frame, sizeof(struct can_frame));
    if (ret < 0) {
      perror("read");
      exit(1);
    }
    if (cf)
      memcpy(cf, &frame, sizeof(struct can_frame));
    return frame.data[5];
  }
  fprintf(stderr, "timeout in get_status process!\n");
  exit(1);
}


x86 assembly (clang, -O0):
<get_status>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1030 <init_set_cmd@plt>
mov    -0x10(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x18(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x20(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x28(%rax)
mov    -0x20(%rbp),%rax
movq   $0x0,0x30(%rax)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cltq
cmp    $0x8,%rax
je     123b <get_status+0x8b>
lea    0xdd4(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x24(%rbp),%rdi
call   1090 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10d0 <FD_SET@plt>
movl   $0x3,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   10a0 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    129c <get_status+0xec>
lea    0xd79(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1311 <get_status+0x161>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12e6 <get_status+0x136>
lea    0xd36(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
je     1303 <get_status+0x153>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1080 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    0x28(%rax),%rax
add    $0x40,%rsp
pop    %rbp
ret
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
lea    0xcf1(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<get_status>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x8(%rsp),%r12
mov    %r12,%rdi
call   1030 <init_set_cmd@plt>
mov    0x8(%rsp),%rax
mov    %r15,0x10(%rax)
xorps  %xmm0,%xmm0
movups %xmm0,0x28(%rax)
movups %xmm0,0x18(%rax)
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x8,%edx
call   1040 <write@plt>
cmp    $0x8,%eax
jne    128e <get_status+0xde>
lea    0x14(%rsp),%r15
mov    %r15,%rdi
call   1090 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   10d0 <FD_SET@plt>
movl   $0x3,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x1(%rbx),%edi
lea    0x18(%rsp),%r8
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   10a0 <select@plt>
test   %eax,%eax
js     1297 <get_status+0xe7>
lea    0x14(%rsp),%rsi
mov    %ebx,%edi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     12a0 <get_status+0xf0>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <read@plt>
test   %eax,%eax
js     12bf <get_status+0x10f>
test   %r14,%r14
je     1279 <get_status+0xc9>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
mov    $0x8,%edx
call   1080 <memcpy@plt>
mov    0x8(%rsp),%rax
mov    0x28(%rax),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd6b(%rip),%rdi
jmp    12c6 <get_status+0x116>
lea    0xd68(%rip),%rdi
jmp    12c6 <get_status+0x116>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xd62(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd47(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.can_frame = type { i64* }
%struct.timeval = type { i32, i64 }
@.str = [6 x i8] c"write\00"
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"timeout in get_status process!\0A\00"
define i64 @get_status(i32 %0, i64 %1, %struct.can_frame* %2) {
%4 = alloca i32
%5 = alloca i64
%6 = alloca %struct.can_frame*
%7 = alloca %struct.can_frame
%8 = alloca i32
%9 = alloca %struct.timeval
%10 = alloca i32
store i32 %0, i32* %4
store i64 %1, i64* %5
store %struct.can_frame* %2, %struct.can_frame** %6
%11 = call i32 @init_set_cmd(%struct.can_frame* %7)
%12 = load i64, i64* %5
%13 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%14 = load i64*, i64** %13
%15 = getelementptr inbounds i64, i64* %14, i64 2
store i64 %12, i64* %15
%16 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%17 = load i64*, i64** %16
%18 = getelementptr inbounds i64, i64* %17, i64 3
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%20 = load i64*, i64** %19
%21 = getelementptr inbounds i64, i64* %20, i64 4
store i64 0, i64* %21
%22 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%23 = load i64*, i64** %22
%24 = getelementptr inbounds i64, i64* %23, i64 5
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%26 = load i64*, i64** %25
%27 = getelementptr inbounds i64, i64* %26, i64 6
store i64 0, i64* %27
%28 = load i32, i32* %4
%29 = call i32 @write(i32 %28, %struct.can_frame* %7, i32 8)
%30 = sext i32 %29 to i64
%31 = icmp ne i64 %30, 8
br i1 %31, label %32, label %35
32: ; preds = %3
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %3
%36 = call i32 @FD_ZERO(i32* %8)
%37 = load i32, i32* %4
%38 = call i32 @FD_SET(i32 %37, i32* %8)
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 3, i32* %39
%40 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %40
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
%43 = call i32 @select(i32 %42, i32* %8, i32* null, i32* null, %struct.timeval* %9)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %35
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %35
%50 = load i32, i32* %4
%51 = call i64 @FD_ISSET(i32 %50, i32* %8)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %72
53: ; preds = %49
%54 = load i32, i32* %4
%55 = call i32 @read(i32 %54, %struct.can_frame* %7, i32 8)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %53
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %53
%62 = load %struct.can_frame*, %struct.can_frame** %6
%63 = icmp ne %struct.can_frame* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load %struct.can_frame*, %struct.can_frame** %6
%66 = call i32 @memcpy(%struct.can_frame* %65, %struct.can_frame* %7, i32 8)
br label %67
67: ; preds = %64, %61
%68 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %7, i32 0, i32 0
%69 = load i64*, i64** %68
%70 = getelementptr inbounds i64, i64* %69, i64 5
%71 = load i64, i64* %70
ret i64 %71
72: ; preds = %49
%73 = load i32, i32* @stderr
%74 = call i32 @fprintf(i32 %73, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%75 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
%struct.can_frame = type { i64* }
%struct.timeval = type { i32, i64 }
@.str = [6 x i8] c"write\00"
@.str.1 = [7 x i8] c"select\00"
@.str.2 = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.3 = [32 x i8] c"timeout in get_status process!\0A\00"
define i64 @get_status(i32 %0, i64 %1, %struct.can_frame* %2) {
%4 = alloca %struct.can_frame
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = bitcast %struct.can_frame* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = bitcast %struct.timeval* %6 to i8*
%10 = call i32 @init_set_cmd(%struct.can_frame* %4)
%11 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %4, i64 0, i32 0
%12 = load i64*, i64** %11
%13 = getelementptr inbounds i64, i64* %12, i64 2
store i64 %1, i64* %13
%14 = getelementptr inbounds i64, i64* %12, i64 3
%15 = bitcast i64* %14 to i8*
%16 = call i32 @write(i32 %0, %struct.can_frame* %4, i32 8)
%17 = icmp eq i32 %16, 8
br i1 %17, label %21, label %18
18: ; preds = %3
%19 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %3
%22 = call i32 @FD_ZERO(i32* %5)
%23 = call i32 @FD_SET(i32 %0, i32* %5)
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 3, i32* %24
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %25
%26 = add nsw i32 %0, 1
%27 = call i32 @select(i32 %26, i32* %5, i32* null, i32* null, %struct.timeval* %6)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = call i64 @FD_ISSET(i32 %0, i32* %5)
%34 = icmp eq i64 %33, 0
br i1 %34, label %49, label %35
35: ; preds = %32
%36 = call i32 @read(i32 %0, %struct.can_frame* %4, i32 8)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %35
%42 = icmp eq %struct.can_frame* %2, null
br i1 %42, label %45, label %43
43: ; preds = %41
%44 = call i32 @memcpy(%struct.can_frame* %2, %struct.can_frame* %4, i32 8)
br label %45
45: ; preds = %43, %41
%46 = load i64*, i64** %11
%47 = getelementptr inbounds i64, i64* %46, i64 5
%48 = load i64, i64* %47
ret i64 %48
49: ; preds = %32
%50 = load i32, i32* @stderr
%51 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
}