#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 timeval {
  int dummy;
};
struct sockaddr_can {
  int can_family;
  int /*<<< orphan*/ can_ifindex;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ socketPoll;
typedef int /*<<< orphan*/ readBuffer;
typedef int /*<<< orphan*/ address;

/* Variables and functions */
int /*<<< orphan*/ CAN_RAW;
int POLLIN;
int /*<<< orphan*/ PTHREAD_CANCEL_ENABLE;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ pthread_setcancelstate(int /*<<< orphan*/,
                                          int /*<<< orphan*/ *);
int /*<<< orphan*/ recv(int, char *, int, int /*<<< orphan*/);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
void *canRecv(void *data) {
  int sock;
  struct sockaddr_can address;
  struct ifreq ifr;
  char readBuffer[64];
  struct timeval systemTime;
  struct pollfd socketPoll;
  long unsigned int time;
  int pollReturn;
  if ((sock = socket(29, SOCK_RAW, CAN_RAW)) < 0) {
    printf("Couldn't open socket\n");
    return -1;
  }
  address.can_family = 29;
  strcpy(ifr.ifr_name, "can1");
  ioctl(sock, SIOCGIFINDEX, &ifr);
  address.can_ifindex = ifr.ifr_ifindex;
  bind(sock, (struct sockaddr *)&address, sizeof(address));
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  while (1) {
    memset((void *)&socketPoll, 0, sizeof(socketPoll));
    socketPoll.fd = sock;
    socketPoll.events = POLLIN;
    pollReturn = poll(&socketPoll, 1, 20000);
    if (pollReturn < 0) {
      printf("CANBus poll failed\n");
      return -1;
    } else if (pollReturn == 0) {
      printf("CANBus connection timed out\n");
    } else if (socketPoll.revents & POLLIN && pollReturn > 0) {
      recv(sock, readBuffer, sizeof(readBuffer), 0);
    }
  }
  return (1);
}


x86 assembly (clang, -O0):
<canRecv>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    $0x1d,%edi
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11e3 <canRecv+0x53>
lea    0xe32(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1309 <canRecv+0x179>
movl   $0x1d,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    0xe22(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x14(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1080 <ioctl@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <bind@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <pthread_setcancelstate@plt>
lea    -0x88(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x88(%rbp)
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x84(%rbp)
lea    -0x88(%rbp),%rdi
mov    $0x1,%esi
mov    $0x4e20,%edx
call   1090 <poll@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0x0,-0x94(%rbp)
jge    12a7 <canRecv+0x117>
lea    0xd89(%rip),%rdi
call   1060 <printf@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1309 <canRecv+0x179>
cmpl   $0x0,-0x94(%rbp)
jne    12c5 <canRecv+0x135>
lea    0xd74(%rip),%rdi
call   1060 <printf@plt>
jmp    12ff <canRecv+0x16f>
mov    -0x80(%rbp),%eax
mov    0x2d11(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12fa <canRecv+0x16a>
cmpl   $0x0,-0x94(%rbp)
jle    12fa <canRecv+0x16a>
mov    -0x14(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x40,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
jmp    12ff <canRecv+0x16f>
jmp    1304 <canRecv+0x174>
jmp    1237 <canRecv+0xa7>
mov    -0x8(%rbp),%rax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canRecv>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x60,%rsp
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    $0x1d,%edi
call   10b0 <socket@plt>
test   %eax,%eax
js     12c2 <canRecv+0x132>
mov    %eax,%ebp
movl   $0x1d,0x10(%rsp)
lea    0xe2f(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    0x18(%rsp),%rdx
mov    %ebp,%edi
call   1080 <ioctl@plt>
mov    0x18(%rsp),%eax
mov    %eax,0x14(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10a0 <bind@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <pthread_setcancelstate@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    %ebp,(%rsp)
mov    0x2db0(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x4e20,%edx
call   1090 <poll@plt>
test   %eax,%eax
js     12b9 <canRecv+0x129>
lea    0xdb0(%rip),%r14
mov    %rsp,%rbx
lea    0x20(%rsp),%r15
jmp    1298 <canRecv+0x108>
nop
mov    %r14,%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    %ebp,(%rsp)
mov    (%r12),%eax
mov    %eax,0x4(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x4e20,%edx
call   1090 <poll@plt>
test   %eax,%eax
js     12b9 <canRecv+0x129>
test   %eax,%eax
je     1260 <canRecv+0xd0>
mov    (%r12),%eax
test   %eax,0x8(%rsp)
je     1268 <canRecv+0xd8>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x40,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
jmp    1268 <canRecv+0xd8>
lea    0xd61(%rip),%rdi
jmp    12c9 <canRecv+0x139>
lea    0xd6b(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffffffffffff,%rax
add    $0x60,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_can = type { i32, i32 }
%struct.ifreq = type { i32, i32 }
%struct.timeval = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str = [22 x i8] c"Couldn't open socket\0A\00"
@.str.1 = [5 x i8] c"can1\00"
@SIOCGIFINDEX = global i32 0
@PTHREAD_CANCEL_ENABLE = global i32 0
@POLLIN = global i32 0
@.str.2 = [20 x i8] c"CANBus poll failed\0A\00"
@.str.3 = [29 x i8] c"CANBus connection timed out\0A\00"
define i8* @canRecv(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sockaddr_can
%6 = alloca %struct.ifreq
%7 = alloca [64 x i8]
%8 = alloca %struct.timeval
%9 = alloca %struct.pollfd
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %3
%12 = load i32, i32* @SOCK_RAW
%13 = load i32, i32* @CAN_RAW
%14 = call i32 @socket(i32 29, i32 %12, i32 %13)
store i32 %14, i32* %4
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %1
%17 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i8* inttoptr (i64 -1 to i8*), i8** %2
br label %67
18: ; preds = %1
%19 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %5, i32 0, i32 0
store i32 29, i32* %19
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 @strcpy(i32 %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %4
%24 = load i32, i32* @SIOCGIFINDEX
%25 = call i32 @ioctl(i32 %23, i32 %24, %struct.ifreq* %6)
%26 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = load i32, i32* %4
%30 = bitcast %struct.sockaddr_can* %5 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 8)
%32 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%33 = call i32 @pthread_setcancelstate(i32 %32, i32* null)
br label %34
34: ; preds = %18, %66
%35 = bitcast %struct.pollfd* %9 to i8*
%36 = call i32 @memset(i8* %35, i32 0, i32 12)
%37 = load i32, i32* %4
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* @POLLIN
%40 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 1
store i32 %39, i32* %40
%41 = call i32 @poll(%struct.pollfd* %9, i32 1, i32 20000)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %34
%45 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i8* inttoptr (i64 -1 to i8*), i8** %2
br label %67
46: ; preds = %34
%47 = load i32, i32* %11
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
br label %65
51: ; preds = %46
%52 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %9, i32 0, i32 2
%53 = load i32, i32* %52
%54 = load i32, i32* @POLLIN
%55 = and i32 %53, %54
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %51
%58 = load i32, i32* %11
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %57
%61 = load i32, i32* %4
%62 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%63 = call i32 @recv(i32 %61, i8* %62, i32 64, i32 0)
br label %64
64: ; preds = %60, %57, %51
br label %65
65: ; preds = %64, %49
br label %66
66: ; preds = %65
br label %34
67: ; preds = %44, %16
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
%struct.sockaddr_can = type { i32, i32 }
%struct.ifreq = type { i32, i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str.1 = [5 x i8] c"can1\00"
@SIOCGIFINDEX = global i32 0
@PTHREAD_CANCEL_ENABLE = global i32 0
@POLLIN = global i32 0
@str = [28 x i8] c"CANBus connection timed out\00"
@str.4 = [19 x i8] c"CANBus poll failed\00"
@str.5 = [21 x i8] c"Couldn't open socket\00"
define i8* @canRecv(i8* nocapture readnone %0) {
%2 = alloca %struct.sockaddr_can
%3 = alloca %struct.ifreq
%4 = alloca [64 x i8]
%5 = alloca %struct.pollfd
%6 = bitcast %struct.sockaddr_can* %2 to i8*
%7 = bitcast %struct.ifreq* %3 to i8*
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.pollfd* %5 to i8*
%10 = load i32, i32* @SOCK_RAW
%11 = load i32, i32* @CAN_RAW
%12 = tail call i32 @socket(i32 29, i32 %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, i64 0, i64 0))
br label %55
16: ; preds = %1
%17 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %2, i64 0, i32 0
store i32 29, i32* %17
%18 = tail call i32 @strcpy(i32 undef, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @SIOCGIFINDEX
%20 = call i32 @ioctl(i32 %12, i32 %19, %struct.ifreq* %3)
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %2, i64 0, i32 1
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_can* %2 to %struct.sockaddr*
%25 = call i32 @bind(i32 %12, %struct.sockaddr* %24, i32 8)
%26 = load i32, i32* @PTHREAD_CANCEL_ENABLE
%27 = call i32 @pthread_setcancelstate(i32 %26, i32* null)
%28 = call i32 @memset(i8* %9, i32 0, i32 12)
%29 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
store i32 %12, i32* %29
%30 = load i32, i32* @POLLIN
%31 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
store i32 %30, i32* %31
%32 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 20000)
%33 = icmp slt i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %16
%35 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
br label %38
36: ; preds = %50, %16
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
br label %55
38: ; preds = %34, %50
%39 = phi i32 [ %32, %34 ], [ %53, %50 ]
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %50
43: ; preds = %38
%44 = load i32, i32* %35
%45 = load i32, i32* @POLLIN
%46 = and i32 %45, %44
%47 = icmp eq i32 %46, 0
br i1 %47, label %50, label %48
48: ; preds = %43
%49 = call i32 @recv(i32 %12, i8* %8, i32 64, i32 0)
br label %50
50: ; preds = %41, %48, %43
%51 = call i32 @memset(i8* %9, i32 0, i32 12)
store i32 %12, i32* %29
%52 = load i32, i32* @POLLIN
store i32 %52, i32* %31
%53 = call i32 @poll(%struct.pollfd* %5, i32 1, i32 20000)
%54 = icmp slt i32 %53, 0
br i1 %54, label %36, label %38
55: ; preds = %36, %14
ret i8* inttoptr (i64 -1 to i8*)
}