#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 */

/* Variables and functions */
int O_NDELAY;
int O_NOCTTY;
int O_RDONLY;
int /*<<< orphan*/ TIOCMGET;
int /*<<< orphan*/ TIOCMSET;
int TIOCM_DTR;
int TIOCM_RTS;
char *getenv(char *);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int open(char const *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ report_state(int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ toggle_dtr(int, int);
int /*<<< orphan*/ toggle_rts(int, int);
int main(int argc, char *argv[]) {
  const char *d = "/dev/ttyUSB0";
  const char *p = getenv("ESP_PORT");
  if (p) {
    d = p;
  }
  int fd = open(d, O_RDONLY | O_NOCTTY | O_NDELAY);
  int pins = 0;
  ioctl(fd, TIOCMGET, &pins);
  pins = pins & (~(TIOCM_RTS | TIOCM_DTR));
  ioctl(fd, TIOCMSET, &pins);
  report_state(fd);
  sleep(2);
  toggle_dtr(fd, true);
  toggle_dtr(fd, false);
  toggle_rts(fd, true);
  toggle_rts(fd, false);
  report_state(fd);
  sleep(1);
  toggle_dtr(fd, true);
  sleep(1);
  toggle_rts(fd, true);
  report_state(fd);
  printf("Done.\n");
  return 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)
lea    0xe63(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe65(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11c4 <main+0x44>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2de0(%rip),%rax
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <ioctl@plt>
mov    -0x28(%rbp),%eax
mov    0x2dc7(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2db6(%rip),%rdx
or     (%rdx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <ioctl@plt>
mov    -0x24(%rbp),%edi
call   1050 <report_state@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1090 <toggle_dtr@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1090 <toggle_dtr@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1040 <toggle_rts@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1040 <toggle_rts@plt>
mov    -0x24(%rbp),%edi
call   1050 <report_state@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1090 <toggle_dtr@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1040 <toggle_rts@plt>
mov    -0x24(%rbp),%edi
call   1050 <report_state@plt>
lea    0xd58(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe82(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
lea    0xe66(%rip),%rdi
cmovne %rax,%rdi
mov    0x2e43(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df9(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebx
movl   $0x0,0x4(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    0x4(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
mov    0x2df1(%rip),%rax
mov    0x2de2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x4(%rsp)
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <ioctl@plt>
mov    %ebx,%edi
call   1060 <report_state@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1090 <toggle_dtr@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1090 <toggle_dtr@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <toggle_rts@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <toggle_rts@plt>
mov    %ebx,%edi
call   1060 <report_state@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1090 <toggle_dtr@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <toggle_rts@plt>
mov    %ebx,%edi
call   1060 <report_state@plt>
lea    0xd8f(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@.str.1 = [9 x i8] c"ESP_PORT\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@TIOCMGET = global i32 0
@TIOCM_RTS = global i32 0
@TIOCM_DTR = global i32 0
@TIOCMSET = global i32 0
@.str.2 = [7 x i8] c"Done.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %6
%10 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i8*, i8** %7
store i8* %14, i8** %6
br label %15
15: ; preds = %13, %2
%16 = load i8*, i8** %6
%17 = load i32, i32* @O_RDONLY
%18 = load i32, i32* @O_NOCTTY
%19 = or i32 %17, %18
%20 = load i32, i32* @O_NDELAY
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %16, i32 %21)
store i32 %22, i32* %8
store i32 0, i32* %9
%23 = load i32, i32* %8
%24 = load i32, i32* @TIOCMGET
%25 = call i32 @ioctl(i32 %23, i32 %24, i32* %9)
%26 = load i32, i32* %9
%27 = load i32, i32* @TIOCM_RTS
%28 = load i32, i32* @TIOCM_DTR
%29 = or i32 %27, %28
%30 = xor i32 %29, -1
%31 = and i32 %26, %30
store i32 %31, i32* %9
%32 = load i32, i32* %8
%33 = load i32, i32* @TIOCMSET
%34 = call i32 @ioctl(i32 %32, i32 %33, i32* %9)
%35 = load i32, i32* %8
%36 = call i32 @report_state(i32 %35)
%37 = call i32 @sleep(i32 2)
%38 = load i32, i32* %8
%39 = call i32 @toggle_dtr(i32 %38, i32 1)
%40 = load i32, i32* %8
%41 = call i32 @toggle_dtr(i32 %40, i32 0)
%42 = load i32, i32* %8
%43 = call i32 @toggle_rts(i32 %42, i32 1)
%44 = load i32, i32* %8
%45 = call i32 @toggle_rts(i32 %44, i32 0)
%46 = load i32, i32* %8
%47 = call i32 @report_state(i32 %46)
%48 = call i32 @sleep(i32 1)
%49 = load i32, i32* %8
%50 = call i32 @toggle_dtr(i32 %49, i32 1)
%51 = call i32 @sleep(i32 1)
%52 = load i32, i32* %8
%53 = call i32 @toggle_rts(i32 %52, i32 1)
%54 = load i32, i32* %8
%55 = call i32 @report_state(i32 %54)
%56 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyUSB0\00"
@.str.1 = [9 x i8] c"ESP_PORT\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@TIOCMGET = global i32 0
@TIOCM_RTS = global i32 0
@TIOCM_DTR = global i32 0
@TIOCMSET = global i32 0
@str = [6 x i8] c"Done.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i8* %4, null
%6 = select i1 %5, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %4
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %8, %7
%10 = load i32, i32* @O_NDELAY
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %6, i32 %11)
%13 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%14 = load i32, i32* @TIOCMGET
%15 = call i32 @ioctl(i32 %12, i32 %14, i32* %3)
%16 = load i32, i32* %3
%17 = load i32, i32* @TIOCM_RTS
%18 = load i32, i32* @TIOCM_DTR
%19 = or i32 %18, %17
%20 = xor i32 %19, -1
%21 = and i32 %16, %20
store i32 %21, i32* %3
%22 = load i32, i32* @TIOCMSET
%23 = call i32 @ioctl(i32 %12, i32 %22, i32* %3)
%24 = call i32 @report_state(i32 %12)
%25 = call i32 @sleep(i32 2)
%26 = call i32 @toggle_dtr(i32 %12, i32 1)
%27 = call i32 @toggle_dtr(i32 %12, i32 0)
%28 = call i32 @toggle_rts(i32 %12, i32 1)
%29 = call i32 @toggle_rts(i32 %12, i32 0)
%30 = call i32 @report_state(i32 %12)
%31 = call i32 @sleep(i32 1)
%32 = call i32 @toggle_dtr(i32 %12, i32 1)
%33 = call i32 @sleep(i32 1)
%34 = call i32 @toggle_rts(i32 %12, i32 1)
%35 = call i32 @report_state(i32 %12)
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
ret i32 0
}