#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 termios {
  int c_lflag;
};
struct sockaddr_can {
  int /*<<< orphan*/ can_ifindex;
  int /*<<< orphan*/ can_family;
};
struct sockaddr {
  int dummy;
};
struct ifreq {
  int /*<<< orphan*/ ifr_ifindex;
  int /*<<< orphan*/ ifr_name;
};
struct can_filter {
  scalar_t__ can_mask;
  scalar_t__ can_id;
};
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_CAN;
int /*<<< orphan*/ CAN_RAW;
int /*<<< orphan*/ CAN_RAW_FILTER;
int ECHO;
int ECHOE;
int ECHOK;
int ECHOKE;
int ECHONL;
int ECHOPRT;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int ICANON;
int ICRNL;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ PF_CAN;
int /*<<< orphan*/ SIOCGIFINDEX;
int /*<<< orphan*/ SOCK_RAW;
int /*<<< orphan*/ SOL_CAN_RAW;
int /*<<< orphan*/ TCSANOW;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ can2pty(int, int, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ pty2can(int, int, struct can_filter *, int *, int *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int /*<<< orphan*/ *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
scalar_t__ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, char **argv) {
  fd_set rdfs;
  int p;
  int s;
  struct sockaddr_can addr;
  struct termios topts;
  struct ifreq ifr;
  int running = 1;
  int tstamp = 0;
  int is_open = 0;
  struct can_filter fi;
  if (argc != 3) {
    fprintf(stderr, "\n");
    fprintf(stderr,
            "%s creates a pty for applications using"
            " the slcan ASCII protocol and\n",
            argv[0]);
    fprintf(stderr, "converts the ASCII data to a CAN network"
                    " interface (and vice versa)\n\n");
    fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
    fprintf(stderr,
            "e.g. '%s /dev/ptyc0 can0' creates"
            " /dev/ttyc0 for the slcan application\n",
            argv[0]);
    fprintf(stderr, "\n");
    return 1;
  }
  p = open(argv[1], O_RDWR);
  if (p < 0) {
    perror("open pty");
    return 1;
  }
  if (tcgetattr(p, &topts)) {
    perror("tcgetattr");
    return 1;
  }
  topts.c_lflag &=
      ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
  tcsetattr(p, TCSANOW, &topts);
  s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  if (s < 0) {
    perror("socket");
    return 1;
  }
  addr.can_family = AF_CAN;
  strcpy(ifr.ifr_name, argv[2]);
  if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
    perror("SIOCGIFINDEX");
    return 1;
  }
  addr.can_ifindex = ifr.ifr_ifindex;
  setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
  if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("bind");
    return 1;
  }
  fi.can_id = 0;
  fi.can_mask = 0;
  while (running) {
    FD_ZERO(&rdfs);
    FD_SET(0, &rdfs);
    FD_SET(p, &rdfs);
    FD_SET(s, &rdfs);
    if (select(s + 1, &rdfs, NULL, NULL, NULL) < 0) {
      perror("select");
      return 1;
    }
    if (FD_ISSET(0, &rdfs)) {
      running = 0;
      continue;
    }
    if (FD_ISSET(p, &rdfs))
      if (pty2can(p, s, &fi, &is_open, &tstamp)) {
        running = 0;
        continue;
      }
    if (FD_ISSET(s, &rdfs))
      if (can2pty(p, s, &tstamp)) {
        running = 0;
        continue;
      }
  }
  close(p);
  close(s);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     12f0 <main+0xe0>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde7(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f8 <main+0x3e8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
call   10f0 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    132b <main+0x11b>
lea    0xddb(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f8 <main+0x3e8>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10d0 <tcgetattr@plt>
cmp    $0x0,%rax
je     1359 <main+0x149>
lea    0xdb6(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f8 <main+0x3e8>
mov    0x2c00(%rip),%rax
mov    (%rax),%eax
mov    0x2c4f(%rip),%rcx
or     (%rcx),%eax
mov    0x2c3e(%rip),%rcx
or     (%rcx),%eax
mov    0x2c0d(%rip),%rcx
or     (%rcx),%eax
mov    0x2bfc(%rip),%rcx
or     (%rcx),%eax
mov    0x2beb(%rip),%rcx
or     (%rcx),%eax
mov    0x2c0a(%rip),%rcx
or     (%rcx),%eax
mov    0x2c39(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2c0c(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   10e0 <tcsetattr@plt>
mov    0x2baa(%rip),%rax
mov    (%rax),%edi
mov    0x2c01(%rip),%rax
mov    (%rax),%esi
mov    0x2b90(%rip),%rax
mov    (%rax),%edx
call   1130 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1404 <main+0x1f4>
lea    0xd15(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f8 <main+0x3e8>
mov    0x2be5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x1c(%rbp),%edi
mov    0x2b26(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    1457 <main+0x247>
lea    0xcc9(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f8 <main+0x3e8>
mov    -0x38(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2b61(%rip),%rax
mov    (%rax),%esi
mov    0x2b20(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1040 <setsockopt@plt>
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    14b1 <main+0x2a1>
lea    0xc7c(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f8 <main+0x3e8>
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     15e1 <main+0x3d1>
lea    -0x14(%rbp),%rdi
call   1090 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1120 <FD_SET@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   10a0 <select@plt>
cmp    $0x0,%rax
jge    1533 <main+0x323>
lea    0xbff(%rip),%rdi
call   1100 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15f8 <main+0x3e8>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1554 <main+0x344>
movl   $0x0,-0x3c(%rbp)
jmp    14c1 <main+0x2b1>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     159c <main+0x38c>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
lea    -0x58(%rbp),%rdx
lea    -0x44(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1110 <pty2can@plt>
cmp    $0x0,%rax
je     1597 <main+0x387>
movl   $0x0,-0x3c(%rbp)
jmp    14c1 <main+0x2b1>
jmp    159c <main+0x38c>
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     15dc <main+0x3cc>
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%esi
lea    -0x40(%rbp),%rdx
call   10b0 <can2pty@plt>
cmp    $0x0,%rax
je     15d7 <main+0x3c7>
movl   $0x0,-0x3c(%rbp)
jmp    14c1 <main+0x2b1>
jmp    15dc <main+0x3cc>
jmp    14c1 <main+0x2b1>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r14
movl   $0x0,0x14(%rsp)
movl   $0x0,0x10(%rsp)
cmp    $0x3,%edi
jne    144a <main+0x23a>
mov    0x8(%r14),%rdi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   10f0 <open@plt>
test   %eax,%eax
js     14c1 <main+0x2b1>
mov    %eax,%r15d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10d0 <tcgetattr@plt>
test   %rax,%rax
jne    14ca <main+0x2ba>
mov    0x2ced(%rip),%rax
mov    0x2d3e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d2b(%rip),%rax
or     (%rax),%ecx
mov    0x2cfa(%rip),%rax
or     (%rax),%ecx
mov    0x2ce9(%rip),%rax
or     (%rax),%ecx
mov    0x2cd8(%rip),%rax
or     (%rax),%ecx
mov    0x2cf7(%rip),%rax
or     (%rax),%ecx
mov    0x2d26(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    0x2cff(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   10e0 <tcsetattr@plt>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
mov    0x2c7f(%rip),%rax
mov    (%rax),%edx
call   1130 <socket@plt>
test   %eax,%eax
js     14d3 <main+0x2c3>
mov    %eax,%ebp
mov    0x2cef(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x10(%r14),%rsi
call   1030 <strcpy@plt>
mov    0x2c39(%rip),%rax
mov    (%rax),%esi
lea    0x20(%rsp),%rdx
mov    %ebp,%edi
call   1060 <ioctl@plt>
test   %rax,%rax
js     14dc <main+0x2cc>
mov    0x20(%rsp),%eax
mov    %eax,0x18(%rsp)
mov    0x2c8b(%rip),%rax
mov    (%rax),%esi
mov    0x2c4a(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1040 <setsockopt@plt>
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
mov    $0x8,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     14e5 <main+0x2d5>
mov    %ebp,%r14d
add    $0x1,%r14d
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
lea    0x2c(%rsp),%rbx
lea    0x14(%rsp),%r12
lea    0x10(%rsp),%r13
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1120 <FD_SET@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <select@plt>
test   %rax,%rax
js     14ee <main+0x2de>
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
jne    1434 <main+0x224>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     140b <main+0x1fb>
mov    %r15d,%edi
mov    %ebp,%esi
lea    0x30(%rsp),%rdx
mov    %r13,%rcx
mov    %r12,%r8
call   1110 <pty2can@plt>
test   %rax,%rax
jne    1434 <main+0x224>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1390 <main+0x180>
mov    %r15d,%edi
mov    %ebp,%esi
mov    %r12,%rdx
call   10b0 <can2pty@plt>
test   %rax,%rax
je     1390 <main+0x180>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    14ff <main+0x2ef>
mov    0x2b47(%rip),%rbp
mov    0x0(%rbp),%edi
lea    0xc2f(%rip),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x0(%rbp),%edi
mov    (%r14),%rdx
lea    0xb8e(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x0(%rbp),%edi
lea    0xbc3(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x0(%rbp),%edi
mov    (%r14),%rdx
lea    0xbf5(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x0(%rbp),%edi
mov    (%r14),%rdx
lea    0xc02(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    14fa <main+0x2ea>
lea    0xc2d(%rip),%rdi
jmp    14f5 <main+0x2e5>
lea    0xc2d(%rip),%rdi
jmp    14f5 <main+0x2e5>
lea    0xc2e(%rip),%rdi
jmp    14f5 <main+0x2e5>
lea    0xc2c(%rip),%rdi
jmp    14f5 <main+0x2e5>
lea    0xc30(%rip),%rdi
jmp    14f5 <main+0x2e5>
lea    0xc2c(%rip),%rdi
call   1100 <perror@plt>
mov    $0x1,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_can = type { i32, i32 }
%struct.termios = type { i32 }
%struct.ifreq = type { i32, i32 }
%struct.can_filter = type { i64, i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [2 x i8] c"\0A\00"
@.str.1 = [70 x i8] c"%s creates a pty for applications using the slcan ASCII protocol and\0A\00"
@.str.2 = [70 x i8] c"converts the ASCII data to a CAN network interface (and vice versa)\0A\0A\00"
@.str.3 = [33 x i8] c"Usage: %s <pty> <can interface>\0A\00"
@.str.4 = [72 x i8] c"e.g. '%s /dev/ptyc0 can0' creates /dev/ttyc0 for the slcan application\0A\00"
@O_RDWR = global i32 0
@.str.5 = [9 x i8] c"open pty\00"
@.str.6 = [10 x i8] c"tcgetattr\00"
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@ECHOPRT = global i32 0
@ECHOKE = global i32 0
@ICRNL = global i32 0
@TCSANOW = global i32 0
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str.7 = [7 x i8] c"socket\00"
@AF_CAN = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.8 = [13 x i8] c"SIOCGIFINDEX\00"
@SOL_CAN_RAW = global i32 0
@CAN_RAW_FILTER = global i32 0
@.str.9 = [5 x i8] c"bind\00"
@.str.10 = [7 x i8] c"select\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_can
%10 = alloca %struct.termios
%11 = alloca %struct.ifreq
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.can_filter
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 3
br i1 %17, label %18, label %40
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 0
%24 = load i8*, i8** %23
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @stderr
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 0
%31 = load i8*, i8** %30
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = load i32, i32* @stderr
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.4, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %167
40: ; preds = %2
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = load i32, i32* @O_RDWR
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %167
50: ; preds = %40
%51 = load i32, i32* %7
%52 = call i64 @tcgetattr(i32 %51, %struct.termios* %10)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %167
56: ; preds = %50
%57 = load i32, i32* @ICANON
%58 = load i32, i32* @ECHO
%59 = or i32 %57, %58
%60 = load i32, i32* @ECHOE
%61 = or i32 %59, %60
%62 = load i32, i32* @ECHOK
%63 = or i32 %61, %62
%64 = load i32, i32* @ECHONL
%65 = or i32 %63, %64
%66 = load i32, i32* @ECHOPRT
%67 = or i32 %65, %66
%68 = load i32, i32* @ECHOKE
%69 = or i32 %67, %68
%70 = load i32, i32* @ICRNL
%71 = or i32 %69, %70
%72 = xor i32 %71, -1
%73 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 0
%74 = load i32, i32* %73
%75 = and i32 %74, %72
store i32 %75, i32* %73
%76 = load i32, i32* %7
%77 = load i32, i32* @TCSANOW
%78 = call i32 @tcsetattr(i32 %76, i32 %77, %struct.termios* %10)
%79 = load i32, i32* @PF_CAN
%80 = load i32, i32* @SOCK_RAW
%81 = load i32, i32* @CAN_RAW
%82 = call i32 @socket(i32 %79, i32 %80, i32 %81)
store i32 %82, i32* %8
%83 = load i32, i32* %8
%84 = icmp slt i32 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %56
%86 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %167
87: ; preds = %56
%88 = load i32, i32* @AF_CAN
%89 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %9, i32 0, i32 1
store i32 %88, i32* %89
%90 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 1
%91 = load i32, i32* %90
%92 = load i8**, i8*** %5
%93 = getelementptr inbounds i8*, i8** %92, i64 2
%94 = load i8*, i8** %93
%95 = call i32 @strcpy(i32 %91, i8* %94)
%96 = load i32, i32* %8
%97 = load i32, i32* @SIOCGIFINDEX
%98 = call i64 @ioctl(i32 %96, i32 %97, %struct.ifreq* %11)
%99 = icmp slt i64 %98, 0
br i1 %99, label %100, label %102
100: ; preds = %87
%101 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %167
102: ; preds = %87
%103 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %11, i32 0, i32 0
%104 = load i32, i32* %103
%105 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %9, i32 0, i32 0
store i32 %104, i32* %105
%106 = load i32, i32* %8
%107 = load i32, i32* @SOL_CAN_RAW
%108 = load i32, i32* @CAN_RAW_FILTER
%109 = call i32 @setsockopt(i32 %106, i32 %107, i32 %108, i32* null, i32 0)
%110 = load i32, i32* %8
%111 = bitcast %struct.sockaddr_can* %9 to %struct.sockaddr*
%112 = call i64 @bind(i32 %110, %struct.sockaddr* %111, i32 8)
%113 = icmp slt i64 %112, 0
br i1 %113, label %114, label %116
114: ; preds = %102
%115 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %167
116: ; preds = %102
%117 = getelementptr inbounds %struct.can_filter, %struct.can_filter* %15, i32 0, i32 1
store i64 0, i64* %117
%118 = getelementptr inbounds %struct.can_filter, %struct.can_filter* %15, i32 0, i32 0
store i64 0, i64* %118
br label %119
119: ; preds = %161, %159, %148, %138, %116
%120 = load i32, i32* %12
%121 = icmp ne i32 %120, 0
br i1 %121, label %122, label %162
122: ; preds = %119
%123 = call i32 @FD_ZERO(i32* %6)
%124 = call i32 @FD_SET(i32 0, i32* %6)
%125 = load i32, i32* %7
%126 = call i32 @FD_SET(i32 %125, i32* %6)
%127 = load i32, i32* %8
%128 = call i32 @FD_SET(i32 %127, i32* %6)
%129 = load i32, i32* %8
%130 = add nsw i32 %129, 1
%131 = call i64 @select(i32 %130, i32* %6, i32* null, i32* null, i32* null)
%132 = icmp slt i64 %131, 0
br i1 %132, label %133, label %135
133: ; preds = %122
%134 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %3
br label %167
135: ; preds = %122
%136 = call i64 @FD_ISSET(i32 0, i32* %6)
%137 = icmp ne i64 %136, 0
br i1 %137, label %138, label %139
138: ; preds = %135
store i32 0, i32* %12
br label %119
139: ; preds = %135
%140 = load i32, i32* %7
%141 = call i64 @FD_ISSET(i32 %140, i32* %6)
%142 = icmp ne i64 %141, 0
br i1 %142, label %143, label %150
143: ; preds = %139
%144 = load i32, i32* %7
%145 = load i32, i32* %8
%146 = call i64 @pty2can(i32 %144, i32 %145, %struct.can_filter* %15, i32* %14, i32* %13)
%147 = icmp ne i64 %146, 0
br i1 %147, label %148, label %149
148: ; preds = %143
store i32 0, i32* %12
br label %119
149: ; preds = %143
br label %150
150: ; preds = %149, %139
%151 = load i32, i32* %8
%152 = call i64 @FD_ISSET(i32 %151, i32* %6)
%153 = icmp ne i64 %152, 0
br i1 %153, label %154, label %161
154: ; preds = %150
%155 = load i32, i32* %7
%156 = load i32, i32* %8
%157 = call i64 @can2pty(i32 %155, i32 %156, i32* %13)
%158 = icmp ne i64 %157, 0
br i1 %158, label %159, label %160
159: ; preds = %154
store i32 0, i32* %12
br label %119
160: ; preds = %154
br label %161
161: ; preds = %160, %150
br label %119
162: ; preds = %119
%163 = load i32, i32* %7
%164 = call i32 @close(i32 %163)
%165 = load i32, i32* %8
%166 = call i32 @close(i32 %165)
store i32 0, i32* %3
br label %167
167: ; preds = %162, %133, %114, %100, %85, %54, %48, %18
%168 = load i32, i32* %3
ret i32 %168
}

LLVM IR (-O2):
%struct.sockaddr_can = type { i32, i32 }
%struct.termios = type { i32 }
%struct.ifreq = type { i32, i32 }
%struct.can_filter = type { i64, i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [2 x i8] c"\0A\00"
@.str.1 = [70 x i8] c"%s creates a pty for applications using the slcan ASCII protocol and\0A\00"
@.str.2 = [70 x i8] c"converts the ASCII data to a CAN network interface (and vice versa)\0A\0A\00"
@.str.3 = [33 x i8] c"Usage: %s <pty> <can interface>\0A\00"
@.str.4 = [72 x i8] c"e.g. '%s /dev/ptyc0 can0' creates /dev/ttyc0 for the slcan application\0A\00"
@O_RDWR = global i32 0
@.str.5 = [9 x i8] c"open pty\00"
@.str.6 = [10 x i8] c"tcgetattr\00"
@ICANON = global i32 0
@ECHO = global i32 0
@ECHOE = global i32 0
@ECHOK = global i32 0
@ECHONL = global i32 0
@ECHOPRT = global i32 0
@ECHOKE = global i32 0
@ICRNL = global i32 0
@TCSANOW = global i32 0
@PF_CAN = global i32 0
@SOCK_RAW = global i32 0
@CAN_RAW = global i32 0
@.str.7 = [7 x i8] c"socket\00"
@AF_CAN = global i32 0
@SIOCGIFINDEX = global i32 0
@.str.8 = [13 x i8] c"SIOCGIFINDEX\00"
@SOL_CAN_RAW = global i32 0
@CAN_RAW_FILTER = global i32 0
@.str.9 = [5 x i8] c"bind\00"
@.str.10 = [7 x i8] c"select\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_can
%5 = alloca %struct.termios
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.can_filter
%10 = bitcast i32* %3 to i8*
%11 = bitcast %struct.sockaddr_can* %4 to i8*
%12 = bitcast %struct.termios* %5 to i8*
%13 = bitcast %struct.ifreq* %6 to i8*
%14 = bitcast i32* %7 to i8*
store i32 0, i32* %7
%15 = bitcast i32* %8 to i8*
store i32 0, i32* %8
%16 = bitcast %struct.can_filter* %9 to i8*
%17 = icmp eq i32 %0, 3
br i1 %17, label %34, label %18
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %1
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %1
%28 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %1
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %130
34: ; preds = %2
%35 = getelementptr inbounds i8*, i8** %1, i64 1
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_RDWR
%38 = tail call i32 @open(i8* %36, i32 %37)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %130
42: ; preds = %34
%43 = call i64 @tcgetattr(i32 %38, %struct.termios* %5)
%44 = icmp eq i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %130
47: ; preds = %42
%48 = load i32, i32* @ICANON
%49 = load i32, i32* @ECHO
%50 = or i32 %49, %48
%51 = load i32, i32* @ECHOE
%52 = or i32 %50, %51
%53 = load i32, i32* @ECHOK
%54 = or i32 %52, %53
%55 = load i32, i32* @ECHONL
%56 = or i32 %54, %55
%57 = load i32, i32* @ECHOPRT
%58 = or i32 %56, %57
%59 = load i32, i32* @ECHOKE
%60 = or i32 %58, %59
%61 = load i32, i32* @ICRNL
%62 = or i32 %60, %61
%63 = xor i32 %62, -1
%64 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%65 = load i32, i32* %64
%66 = and i32 %65, %63
store i32 %66, i32* %64
%67 = load i32, i32* @TCSANOW
%68 = call i32 @tcsetattr(i32 %38, i32 %67, %struct.termios* %5)
%69 = load i32, i32* @PF_CAN
%70 = load i32, i32* @SOCK_RAW
%71 = load i32, i32* @CAN_RAW
%72 = call i32 @socket(i32 %69, i32 %70, i32 %71)
%73 = icmp slt i32 %72, 0
br i1 %73, label %74, label %76
74: ; preds = %47
%75 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %130
76: ; preds = %47
%77 = load i32, i32* @AF_CAN
%78 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i64 0, i32 1
store i32 %77, i32* %78
%79 = getelementptr inbounds i8*, i8** %1, i64 2
%80 = load i8*, i8** %79
%81 = call i32 @strcpy(i32 undef, i8* %80)
%82 = load i32, i32* @SIOCGIFINDEX
%83 = call i64 @ioctl(i32 %72, i32 %82, %struct.ifreq* %6)
%84 = icmp slt i64 %83, 0
br i1 %84, label %85, label %87
85: ; preds = %76
%86 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
br label %130
87: ; preds = %76
%88 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i64 0, i32 0
%89 = load i32, i32* %88
%90 = getelementptr inbounds %struct.sockaddr_can, %struct.sockaddr_can* %4, i64 0, i32 0
store i32 %89, i32* %90
%91 = load i32, i32* @SOL_CAN_RAW
%92 = load i32, i32* @CAN_RAW_FILTER
%93 = call i32 @setsockopt(i32 %72, i32 %91, i32 %92, i32* null, i32 0)
%94 = bitcast %struct.sockaddr_can* %4 to %struct.sockaddr*
%95 = call i64 @bind(i32 %72, %struct.sockaddr* %94, i32 8)
%96 = icmp slt i64 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %87
%98 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.9, i64 0, i64 0))
br label %130
99: ; preds = %87
%100 = add nuw nsw i32 %72, 1
%101 = bitcast %struct.can_filter* %9 to i8*
br label %102
102: ; preds = %123, %99
%103 = call i32 @FD_ZERO(i32* %3)
%104 = call i32 @FD_SET(i32 0, i32* %3)
%105 = call i32 @FD_SET(i32 %38, i32* %3)
%106 = call i32 @FD_SET(i32 %72, i32* %3)
%107 = call i64 @select(i32 %100, i32* %3, i32* null, i32* null, i32* null)
%108 = icmp slt i64 %107, 0
br i1 %108, label %109, label %111
109: ; preds = %102
%110 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.10, i64 0, i64 0))
br label %130
111: ; preds = %102
%112 = call i64 @FD_ISSET(i32 0, i32* %3)
%113 = icmp eq i64 %112, 0
br i1 %113, label %114, label %127
114: ; preds = %111
%115 = call i64 @FD_ISSET(i32 %38, i32* %3)
%116 = icmp eq i64 %115, 0
br i1 %116, label %120, label %117
117: ; preds = %114
%118 = call i64 @pty2can(i32 %38, i32 %72, %struct.can_filter* %9, i32* %8, i32* %7)
%119 = icmp eq i64 %118, 0
br i1 %119, label %120, label %127
120: ; preds = %117, %114
%121 = call i64 @FD_ISSET(i32 %72, i32* %3)
%122 = icmp eq i64 %121, 0
br i1 %122, label %123, label %124
123: ; preds = %120, %124
br label %102
124: ; preds = %120
%125 = call i64 @can2pty(i32 %38, i32 %72, i32* %7)
%126 = icmp eq i64 %125, 0
br i1 %126, label %123, label %127
127: ; preds = %111, %117, %124
%128 = call i32 @close(i32 %38)
%129 = call i32 @close(i32 %72)
br label %130
130: ; preds = %127, %109, %97, %85, %74, %45, %40, %18
%131 = phi i32 [ 1, %18 ], [ 1, %40 ], [ 1, %45 ], [ 1, %74 ], [ 1, %85 ], [ 1, %97 ], [ 1, %109 ], [ 0, %127 ]
ret i32 %131
}