#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 /*<<< orphan*/ c_cflag;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCAL;
int /*<<< orphan*/ HUPCL;
int O_NOCTTY;
int O_RDONLY;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCMGET;
int /*<<< orphan*/ TIOCMSET;
int TIOCM_DTR;
int TIOCM_RTS;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
unsigned int strtoul(char *, char **, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  int fd, flags;
  int delay = 10000;
  struct termios termios;
  unsigned int mode = 0;
  if ((argc != 2) && (argc != 3)) {
    fprintf(stderr, "usage: resetserial <device> [mode]\n");
    fprintf(stderr, "       mode = 0: default\n");
    fprintf(stderr, "       mode = 1: reset RTS+DTR\n");
    fprintf(stderr, "       mode = 2: reset DTR, set RTS\n");
    fprintf(stderr, "       mode = 3: reset RTS, set DTR\n");
    fprintf(stderr, "       mode = 4: set RTS+DTR\n");
    exit(1);
  }
  if (argc == 3)
    mode = strtoul(argv[2], (char **)0, 0);
  if ((fd = open(argv[1], O_RDONLY | O_NOCTTY, 0)) < 0) {
    perror("open");
    exit(1);
  }
  if (tcgetattr(fd, &termios)) {
    perror("tcgetattr");
    exit(1);
  }
  cfmakeraw(&termios);
  termios.c_cflag |= CLOCAL;
  termios.c_cflag &= ~HUPCL;
  if (tcsetattr(fd, TCSANOW, &termios)) {
    perror("tcsetattr");
    exit(1);
  }
  if (ioctl(fd, TIOCMGET, &flags)) {
    perror("ioctl 1\n");
    exit(1);
  }
  switch (mode) {
  case (0): {
    flags &= ~TIOCM_RTS;
    if (ioctl(fd, TIOCMSET, &flags)) {
      perror("ioctl 2\n");
      exit(1);
    }
    usleep(delay);
    if (mode)
      flags |= TIOCM_DTR;
    else
      flags &= ~TIOCM_DTR;
    if (ioctl(fd, TIOCMSET, &flags)) {
      perror("ioctl 3\n");
      exit(1);
    }
    usleep(delay);
    flags |= TIOCM_RTS;
    if (ioctl(fd, TIOCMSET, &flags)) {
      perror("ioctl 4\n");
      exit(1);
    }
    usleep(delay);
    flags |= TIOCM_DTR;
    if (ioctl(fd, TIOCMSET, &flags)) {
      perror("ioctl 5\n");
      exit(1);
    }
    break;
  }
  case (1): {
    flags &= ~TIOCM_RTS;
    flags &= ~TIOCM_DTR;
    if (ioctl(fd, TIOCMSET, &flags)) {
      perror("ioctl 12\n");
      exit(1);
    }
    break;
  }
  case (2): {
    flags |= TIOCM_RTS;
    flags &= ~TIOCM_DTR;
    if (ioctl(fd, TIOCMSET, &flags)) {
      perror("ioctl 22\n");
      exit(1);
    }
    break;
  }
  case (3): {
    flags &= ~TIOCM_RTS;
    flags |= TIOCM_DTR;
    if (ioctl(fd, TIOCMSET, &flags)) {
      perror("ioctl 32\n");
      exit(1);
    }
    break;
  }
  case (4): {
    flags |= TIOCM_RTS;
    flags |= TIOCM_DTR;
    if (ioctl(fd, TIOCMSET, &flags)) {
      perror("ioctl 42\n");
      exit(1);
    }
    break;
  }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2710,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1260 <main+0xc0>
cmpl   $0x3,-0x8(%rbp)
je     1260 <main+0xc0>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x3,-0x8(%rbp)
jne    1280 <main+0xe0>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    0x2d18(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12c3 <main+0x123>
lea    0xe26(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
je     12ef <main+0x14f>
lea    0xdff(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x20(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    0x2c91(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2c92(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cb5(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     134e <main+0x1ae>
lea    0xdaa(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2c68(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1383 <main+0x1e3>
lea    0xd7f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x24(%rbp),%eax
mov    %rax,-0x30(%rbp)
sub    $0x4,%rax
ja     1665 <main+0x4c5>
mov    -0x30(%rbp),%rax
lea    0xc61(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2c21(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2c1c(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     13ef <main+0x24f>
lea    0xd1c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <usleep@plt>
cmpl   $0x0,-0x24(%rbp)
je     1415 <main+0x275>
mov    0x2bc0(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    1427 <main+0x287>
mov    0x2bac(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2baf(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     145c <main+0x2bc>
lea    0xcb8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <usleep@plt>
mov    0x2b65(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2b63(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     14a8 <main+0x308>
lea    0xc75(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <usleep@plt>
mov    0x2b11(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2b17(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     14f4 <main+0x354>
lea    0xc32(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1665 <main+0x4c5>
mov    0x2ad0(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2ab6(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2ab9(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1552 <main+0x3b2>
lea    0xbdd(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1665 <main+0x4c5>
mov    0x2a72(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2a5b(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2a5e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     15ad <main+0x40d>
lea    0xb8c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1665 <main+0x4c5>
mov    0x2a17(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x29fd(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2a03(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1608 <main+0x468>
lea    0xb3b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1665 <main+0x4c5>
mov    0x29bc(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x29a5(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x29ab(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     1660 <main+0x4c0>
lea    0xaed(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1665 <main+0x4c5>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x2,%eax
jne    11fa <main+0x5a>
mov    %rsi,%rbx
xor    %ebp,%ebp
cmp    $0x3,%edi
jne    11cd <main+0x2d>
mov    0x10(%rbx),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <strtoul@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
mov    0x2e10(%rip),%rax
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
test   %eax,%eax
jns    125f <main+0xbf>
lea    0xee5(%rip),%rdi
jmp    13cb <main+0x22b>
mov    0x2da7(%rip),%rbx
mov    (%rbx),%edi
lea    0xe0a(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe20(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe2c(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe3e(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe55(%rip),%rsi
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xe6c(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   1060 <tcgetattr@plt>
test   %rax,%rax
jne    13b2 <main+0x212>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2d06(%rip),%rax
mov    0x10(%rsp),%ecx
or     (%rax),%ecx
mov    0x2d09(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    %eax,0x10(%rsp)
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
test   %rax,%rax
jne    13bb <main+0x21b>
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    13c4 <main+0x224>
cmp    $0x4,%ebp
ja     14d3 <main+0x333>
mov    %ebp,%eax
lea    0xd13(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2cd3(%rip),%r14
mov    (%r14),%eax
not    %eax
and    %eax,0xc(%rsp)
mov    0x2cd3(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    14da <main+0x33a>
mov    $0x2710,%edi
call   10c0 <usleep@plt>
mov    0x2c92(%rip),%r15
mov    (%r15),%eax
not    %eax
and    %eax,0xc(%rsp)
mov    0x0(%rbp),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    14f2 <main+0x352>
mov    $0x2710,%edi
call   10c0 <usleep@plt>
mov    (%r14),%eax
or     %eax,0xc(%rsp)
mov    0x0(%rbp),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    14fe <main+0x35e>
mov    $0x2710,%edi
call   10c0 <usleep@plt>
mov    (%r15),%eax
or     %eax,0xc(%rsp)
mov    0x0(%rbp),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     14d3 <main+0x333>
lea    0xd67(%rip),%rdi
jmp    13cb <main+0x22b>
lea    0xd26(%rip),%rdi
jmp    13cb <main+0x22b>
lea    0xd27(%rip),%rdi
jmp    13cb <main+0x22b>
lea    0xd28(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2bef(%rip),%rax
mov    0xc(%rsp),%ecx
or     (%rax),%ecx
mov    0x2bda(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %ecx,%eax
mov    %eax,0xc(%rsp)
mov    0x2be1(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     14d3 <main+0x333>
lea    0xd0d(%rip),%rdi
jmp    13cb <main+0x22b>
mov    0x2baa(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0xc(%rsp),%eax
mov    0x2b93(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0xc(%rsp)
mov    0x2b9e(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     14d3 <main+0x333>
lea    0xcd8(%rip),%rdi
jmp    13cb <main+0x22b>
mov    0x2b68(%rip),%rax
mov    0xc(%rsp),%ecx
or     (%rax),%ecx
mov    0x2b53(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0xc(%rsp)
mov    0x2b5e(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
je     14d3 <main+0x333>
lea    0xca2(%rip),%rdi
jmp    13cb <main+0x22b>
mov    0x2b28(%rip),%rax
mov    (%rax),%eax
mov    0x2b17(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %eax,0xc(%rsp)
mov    0x2b20(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    14e6 <main+0x346>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0xc1b(%rip),%rdi
jmp    13cb <main+0x22b>
lea    0xc33(%rip),%rdi
jmp    13cb <main+0x22b>
lea    0xc0c(%rip),%rdi
jmp    13cb <main+0x22b>
lea    0xc09(%rip),%rdi
jmp    13cb <main+0x22b>

LLVM IR (-O0):
%struct.termios = type { i32 }
@stderr = global i32 0
@.str = [36 x i8] c"usage: resetserial <device> [mode]\0A\00"
@.str.1 = [26 x i8] c" mode = 0: default\0A\00"
@.str.2 = [32 x i8] c" mode = 1: reset RTS+DTR\0A\00"
@.str.3 = [37 x i8] c" mode = 2: reset DTR, set RTS\0A\00"
@.str.4 = [37 x i8] c" mode = 3: reset RTS, set DTR\0A\00"
@.str.5 = [30 x i8] c" mode = 4: set RTS+DTR\0A\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.6 = [5 x i8] c"open\00"
@.str.7 = [10 x i8] c"tcgetattr\00"
@CLOCAL = global i32 0
@HUPCL = global i32 0
@TCSANOW = global i32 0
@.str.8 = [10 x i8] c"tcsetattr\00"
@TIOCMGET = global i32 0
@.str.9 = [9 x i8] c"ioctl 1\0A\00"
@TIOCM_RTS = global i32 0
@TIOCMSET = global i32 0
@.str.10 = [9 x i8] c"ioctl 2\0A\00"
@TIOCM_DTR = global i32 0
@.str.11 = [9 x i8] c"ioctl 3\0A\00"
@.str.12 = [9 x i8] c"ioctl 4\0A\00"
@.str.13 = [9 x i8] c"ioctl 5\0A\00"
@.str.14 = [10 x i8] c"ioctl 12\0A\00"
@.str.15 = [10 x i8] c"ioctl 22\0A\00"
@.str.16 = [10 x i8] c"ioctl 32\0A\00"
@.str.17 = [10 x i8] c"ioctl 42\0A\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.termios
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 10000, i32* %8
store i32 0, i32* %10
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %30
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %13, %2
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 3
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @strtoul(i8* %36, i8** null, i32 0)
store i32 %37, i32* %10
br label %38
38: ; preds = %33, %30
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = load i32, i32* @O_RDONLY
%43 = load i32, i32* @O_NOCTTY
%44 = or i32 %42, %43
%45 = call i32 @open(i8* %41, i32 %44, i32 0)
store i32 %45, i32* %6
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %38
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %38
%51 = load i32, i32* %6
%52 = call i64 @tcgetattr(i32 %51, %struct.termios* %9)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %50
%58 = call i32 @cfmakeraw(%struct.termios* %9)
%59 = load i32, i32* @CLOCAL
%60 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%61 = load i32, i32* %60
%62 = or i32 %61, %59
store i32 %62, i32* %60
%63 = load i32, i32* @HUPCL
%64 = xor i32 %63, -1
%65 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%66 = load i32, i32* %65
%67 = and i32 %66, %64
store i32 %67, i32* %65
%68 = load i32, i32* %6
%69 = load i32, i32* @TCSANOW
%70 = call i64 @tcsetattr(i32 %68, i32 %69, %struct.termios* %9)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %57
%73 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %57
%76 = load i32, i32* %6
%77 = load i32, i32* @TIOCMGET
%78 = call i64 @ioctl(i32 %76, i32 %77, i32* %7)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %75
%84 = load i32, i32* %10
switch i32 %84, label %210 [
i32 0, label %85
i32 1, label %146
i32 2, label %163
i32 3, label %179
i32 4, label %195
]
85: ; preds = %83
%86 = load i32, i32* @TIOCM_RTS
%87 = xor i32 %86, -1
%88 = load i32, i32* %7
%89 = and i32 %88, %87
store i32 %89, i32* %7
%90 = load i32, i32* %6
%91 = load i32, i32* @TIOCMSET
%92 = call i64 @ioctl(i32 %90, i32 %91, i32* %7)
%93 = icmp ne i64 %92, 0
br i1 %93, label %94, label %97
94: ; preds = %85
%95 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0))
%96 = call i32 @exit(i32 1)
unreachable
97: ; preds = %85
%98 = load i32, i32* %8
%99 = call i32 @usleep(i32 %98)
%100 = load i32, i32* %10
%101 = icmp ne i32 %100, 0
br i1 %101, label %102, label %106
102: ; preds = %97
%103 = load i32, i32* @TIOCM_DTR
%104 = load i32, i32* %7
%105 = or i32 %104, %103
store i32 %105, i32* %7
br label %111
106: ; preds = %97
%107 = load i32, i32* @TIOCM_DTR
%108 = xor i32 %107, -1
%109 = load i32, i32* %7
%110 = and i32 %109, %108
store i32 %110, i32* %7
br label %111
111: ; preds = %106, %102
%112 = load i32, i32* %6
%113 = load i32, i32* @TIOCMSET
%114 = call i64 @ioctl(i32 %112, i32 %113, i32* %7)
%115 = icmp ne i64 %114, 0
br i1 %115, label %116, label %119
116: ; preds = %111
%117 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
%118 = call i32 @exit(i32 1)
unreachable
119: ; preds = %111
%120 = load i32, i32* %8
%121 = call i32 @usleep(i32 %120)
%122 = load i32, i32* @TIOCM_RTS
%123 = load i32, i32* %7
%124 = or i32 %123, %122
store i32 %124, i32* %7
%125 = load i32, i32* %6
%126 = load i32, i32* @TIOCMSET
%127 = call i64 @ioctl(i32 %125, i32 %126, i32* %7)
%128 = icmp ne i64 %127, 0
br i1 %128, label %129, label %132
129: ; preds = %119
%130 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0))
%131 = call i32 @exit(i32 1)
unreachable
132: ; preds = %119
%133 = load i32, i32* %8
%134 = call i32 @usleep(i32 %133)
%135 = load i32, i32* @TIOCM_DTR
%136 = load i32, i32* %7
%137 = or i32 %136, %135
store i32 %137, i32* %7
%138 = load i32, i32* %6
%139 = load i32, i32* @TIOCMSET
%140 = call i64 @ioctl(i32 %138, i32 %139, i32* %7)
%141 = icmp ne i64 %140, 0
br i1 %141, label %142, label %145
142: ; preds = %132
%143 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0))
%144 = call i32 @exit(i32 1)
unreachable
145: ; preds = %132
br label %210
146: ; preds = %83
%147 = load i32, i32* @TIOCM_RTS
%148 = xor i32 %147, -1
%149 = load i32, i32* %7
%150 = and i32 %149, %148
store i32 %150, i32* %7
%151 = load i32, i32* @TIOCM_DTR
%152 = xor i32 %151, -1
%153 = load i32, i32* %7
%154 = and i32 %153, %152
store i32 %154, i32* %7
%155 = load i32, i32* %6
%156 = load i32, i32* @TIOCMSET
%157 = call i64 @ioctl(i32 %155, i32 %156, i32* %7)
%158 = icmp ne i64 %157, 0
br i1 %158, label %159, label %162
159: ; preds = %146
%160 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.14, i64 0, i64 0))
%161 = call i32 @exit(i32 1)
unreachable
162: ; preds = %146
br label %210
163: ; preds = %83
%164 = load i32, i32* @TIOCM_RTS
%165 = load i32, i32* %7
%166 = or i32 %165, %164
store i32 %166, i32* %7
%167 = load i32, i32* @TIOCM_DTR
%168 = xor i32 %167, -1
%169 = load i32, i32* %7
%170 = and i32 %169, %168
store i32 %170, i32* %7
%171 = load i32, i32* %6
%172 = load i32, i32* @TIOCMSET
%173 = call i64 @ioctl(i32 %171, i32 %172, i32* %7)
%174 = icmp ne i64 %173, 0
br i1 %174, label %175, label %178
175: ; preds = %163
%176 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.15, i64 0, i64 0))
%177 = call i32 @exit(i32 1)
unreachable
178: ; preds = %163
br label %210
179: ; preds = %83
%180 = load i32, i32* @TIOCM_RTS
%181 = xor i32 %180, -1
%182 = load i32, i32* %7
%183 = and i32 %182, %181
store i32 %183, i32* %7
%184 = load i32, i32* @TIOCM_DTR
%185 = load i32, i32* %7
%186 = or i32 %185, %184
store i32 %186, i32* %7
%187 = load i32, i32* %6
%188 = load i32, i32* @TIOCMSET
%189 = call i64 @ioctl(i32 %187, i32 %188, i32* %7)
%190 = icmp ne i64 %189, 0
br i1 %190, label %191, label %194
191: ; preds = %179
%192 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.16, i64 0, i64 0))
%193 = call i32 @exit(i32 1)
unreachable
194: ; preds = %179
br label %210
195: ; preds = %83
%196 = load i32, i32* @TIOCM_RTS
%197 = load i32, i32* %7
%198 = or i32 %197, %196
store i32 %198, i32* %7
%199 = load i32, i32* @TIOCM_DTR
%200 = load i32, i32* %7
%201 = or i32 %200, %199
store i32 %201, i32* %7
%202 = load i32, i32* %6
%203 = load i32, i32* @TIOCMSET
%204 = call i64 @ioctl(i32 %202, i32 %203, i32* %7)
%205 = icmp ne i64 %204, 0
br i1 %205, label %206, label %209
206: ; preds = %195
%207 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.17, i64 0, i64 0))
%208 = call i32 @exit(i32 1)
unreachable
209: ; preds = %195
br label %210
210: ; preds = %83, %209, %194, %178, %162, %145
%211 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stderr = global i32 0
@.str = [36 x i8] c"usage: resetserial <device> [mode]\0A\00"
@.str.1 = [26 x i8] c" mode = 0: default\0A\00"
@.str.2 = [32 x i8] c" mode = 1: reset RTS+DTR\0A\00"
@.str.3 = [37 x i8] c" mode = 2: reset DTR, set RTS\0A\00"
@.str.4 = [37 x i8] c" mode = 3: reset RTS, set DTR\0A\00"
@.str.5 = [30 x i8] c" mode = 4: set RTS+DTR\0A\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.6 = [5 x i8] c"open\00"
@.str.7 = [10 x i8] c"tcgetattr\00"
@CLOCAL = global i32 0
@HUPCL = global i32 0
@TCSANOW = global i32 0
@.str.8 = [10 x i8] c"tcsetattr\00"
@TIOCMGET = global i32 0
@.str.9 = [9 x i8] c"ioctl 1\0A\00"
@TIOCM_RTS = global i32 0
@TIOCMSET = global i32 0
@.str.10 = [9 x i8] c"ioctl 2\0A\00"
@TIOCM_DTR = global i32 0
@.str.11 = [9 x i8] c"ioctl 3\0A\00"
@.str.12 = [9 x i8] c"ioctl 4\0A\00"
@.str.13 = [9 x i8] c"ioctl 5\0A\00"
@.str.14 = [10 x i8] c"ioctl 12\0A\00"
@.str.15 = [10 x i8] c"ioctl 22\0A\00"
@.str.16 = [10 x i8] c"ioctl 32\0A\00"
@.str.17 = [10 x i8] c"ioctl 42\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.termios
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.termios* %4 to i8*
%7 = and i32 %0, -2
%8 = icmp eq i32 %7, 2
br i1 %8, label %23, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%18 = load i32, i32* @stderr
%19 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = icmp eq i32 %0, 3
br i1 %24, label %25, label %29
25: ; preds = %23
%26 = getelementptr inbounds i8*, i8** %1, i64 2
%27 = load i8*, i8** %26
%28 = tail call i32 @strtoul(i8* nocapture %27, i8** null, i32 0)
br label %29
29: ; preds = %25, %23
%30 = phi i32 [ %28, %25 ], [ 0, %23 ]
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_RDONLY
%34 = load i32, i32* @O_NOCTTY
%35 = or i32 %34, %33
%36 = tail call i32 @open(i8* %32, i32 %35, i32 0)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %29
%39 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %29
%42 = call i64 @tcgetattr(i32 %36, %struct.termios* %4)
%43 = icmp eq i64 %42, 0
br i1 %43, label %47, label %44
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %41
%48 = call i32 @cfmakeraw(%struct.termios* %4)
%49 = load i32, i32* @CLOCAL
%50 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%51 = load i32, i32* %50
%52 = or i32 %51, %49
%53 = load i32, i32* @HUPCL
%54 = xor i32 %53, -1
%55 = and i32 %52, %54
store i32 %55, i32* %50
%56 = load i32, i32* @TCSANOW
%57 = call i64 @tcsetattr(i32 %36, i32 %56, %struct.termios* %4)
%58 = icmp eq i64 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %47
%60 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.8, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %47
%63 = load i32, i32* @TIOCMGET
%64 = call i64 @ioctl(i32 %36, i32 %63, i32* %3)
%65 = icmp eq i64 %64, 0
br i1 %65, label %69, label %66
66: ; preds = %62
%67 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %62
switch i32 %30, label %166 [
i32 0, label %70
i32 1, label %115
i32 2, label %128
i32 3, label %141
i32 4, label %154
]
70: ; preds = %69
%71 = load i32, i32* @TIOCM_RTS
%72 = xor i32 %71, -1
%73 = load i32, i32* %3
%74 = and i32 %73, %72
store i32 %74, i32* %3
%75 = load i32, i32* @TIOCMSET
%76 = call i64 @ioctl(i32 %36, i32 %75, i32* %3)
%77 = icmp eq i64 %76, 0
br i1 %77, label %81, label %78
78: ; preds = %70
%79 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.10, i64 0, i64 0))
%80 = call i32 @exit(i32 1)
unreachable
81: ; preds = %70
%82 = call i32 @usleep(i32 10000)
%83 = load i32, i32* @TIOCM_DTR
%84 = xor i32 %83, -1
%85 = load i32, i32* %3
%86 = and i32 %85, %84
store i32 %86, i32* %3
%87 = load i32, i32* @TIOCMSET
%88 = call i64 @ioctl(i32 %36, i32 %87, i32* %3)
%89 = icmp eq i64 %88, 0
br i1 %89, label %93, label %90
90: ; preds = %81
%91 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.11, i64 0, i64 0))
%92 = call i32 @exit(i32 1)
unreachable
93: ; preds = %81
%94 = call i32 @usleep(i32 10000)
%95 = load i32, i32* @TIOCM_RTS
%96 = load i32, i32* %3
%97 = or i32 %96, %95
store i32 %97, i32* %3
%98 = load i32, i32* @TIOCMSET
%99 = call i64 @ioctl(i32 %36, i32 %98, i32* %3)
%100 = icmp eq i64 %99, 0
br i1 %100, label %104, label %101
101: ; preds = %93
%102 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.12, i64 0, i64 0))
%103 = call i32 @exit(i32 1)
unreachable
104: ; preds = %93
%105 = call i32 @usleep(i32 10000)
%106 = load i32, i32* @TIOCM_DTR
%107 = load i32, i32* %3
%108 = or i32 %107, %106
store i32 %108, i32* %3
%109 = load i32, i32* @TIOCMSET
%110 = call i64 @ioctl(i32 %36, i32 %109, i32* %3)
%111 = icmp eq i64 %110, 0
br i1 %111, label %166, label %112
112: ; preds = %104
%113 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.13, i64 0, i64 0))
%114 = call i32 @exit(i32 1)
unreachable
115: ; preds = %69
%116 = load i32, i32* @TIOCM_RTS
%117 = load i32, i32* %3
%118 = load i32, i32* @TIOCM_DTR
%119 = or i32 %116, %118
%120 = xor i32 %119, -1
%121 = and i32 %117, %120
store i32 %121, i32* %3
%122 = load i32, i32* @TIOCMSET
%123 = call i64 @ioctl(i32 %36, i32 %122, i32* %3)
%124 = icmp eq i64 %123, 0
br i1 %124, label %166, label %125
125: ; preds = %115
%126 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.14, i64 0, i64 0))
%127 = call i32 @exit(i32 1)
unreachable
128: ; preds = %69
%129 = load i32, i32* @TIOCM_RTS
%130 = load i32, i32* %3
%131 = or i32 %130, %129
%132 = load i32, i32* @TIOCM_DTR
%133 = xor i32 %132, -1
%134 = and i32 %131, %133
store i32 %134, i32* %3
%135 = load i32, i32* @TIOCMSET
%136 = call i64 @ioctl(i32 %36, i32 %135, i32* %3)
%137 = icmp eq i64 %136, 0
br i1 %137, label %166, label %138
138: ; preds = %128
%139 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.15, i64 0, i64 0))
%140 = call i32 @exit(i32 1)
unreachable
141: ; preds = %69
%142 = load i32, i32* @TIOCM_RTS
%143 = xor i32 %142, -1
%144 = load i32, i32* %3
%145 = and i32 %144, %143
%146 = load i32, i32* @TIOCM_DTR
%147 = or i32 %146, %145
store i32 %147, i32* %3
%148 = load i32, i32* @TIOCMSET
%149 = call i64 @ioctl(i32 %36, i32 %148, i32* %3)
%150 = icmp eq i64 %149, 0
br i1 %150, label %166, label %151
151: ; preds = %141
%152 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.16, i64 0, i64 0))
%153 = call i32 @exit(i32 1)
unreachable
154: ; preds = %69
%155 = load i32, i32* @TIOCM_RTS
%156 = load i32, i32* %3
%157 = or i32 %156, %155
%158 = load i32, i32* @TIOCM_DTR
%159 = or i32 %158, %157
store i32 %159, i32* %3
%160 = load i32, i32* @TIOCMSET
%161 = call i64 @ioctl(i32 %36, i32 %160, i32* %3)
%162 = icmp eq i64 %161, 0
br i1 %162, label %166, label %163
163: ; preds = %154
%164 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.17, i64 0, i64 0))
%165 = call i32 @exit(i32 1)
unreachable
166: ; preds = %154, %141, %128, %115, %104, %69
%167 = call i32 @exit(i32 0)
unreachable
}