#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_cflag;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ B57600;
int CLOCAL;
int CRTSCTS;
scalar_t__ EWOULDBLOCK;
int O_NOCTTY;
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCEXCL;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int ioctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ make_blocking(int);
int /*<<< orphan*/ make_nonblocking(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int tcdrain(int);
scalar_t__ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_SYNC);
  if (fd == -1) {
    printf("open failed\n");
    goto finish;
  }
  printf("Setting TIOCEXCL\n");
  if (ioctl(fd, TIOCEXCL, NULL) == -1) {
    printf("ioctl failed\n");
    goto finish;
  }
  printf("Getting tty\n");
  struct termios tty;
  if (tcgetattr(fd, &tty) != 0) {
    printf("tcgetattr failed\n");
    goto finish;
  }
  printf("Setting tty options\n");
  cfmakeraw(&tty);
  tty.c_cflag |= (CLOCAL | CRTSCTS);
  cfsetospeed(&tty, B57600);
  cfsetispeed(&tty, B57600);
  printf("Flushing tty\n");
  if (tcflush(fd, TCIOFLUSH) != 0) {
    printf("tcflush failed\n");
    goto finish;
  }
  printf("Setting tty\n");
  if (tcsetattr(fd, TCSANOW, &tty) != 0) {
    printf("tcsetattr failed\n");
    goto finish;
  }
  printf("Writing ATZ\n");
  ssize_t res = write(fd, "ATZ\r", 4);
  if (res == -1) {
    printf("write failed\n");
    goto finish;
  }
  printf("Waiting for write\n");
  res = tcdrain(fd);
  if (res == -1) {
    printf("tcdrain failed\n");
    goto finish;
  }
  printf("Wrote: ATZ\n");
  printf("Reading response\n");
  make_blocking(fd);
  for (;;) {
    unsigned char buf[512];
    res = read(fd, buf, sizeof(buf));
    if (res == -1 && errno == EWOULDBLOCK) {
      break;
    } else if (res == -1) {
      printf("read failed\n");
      goto finish;
    }
    make_nonblocking(fd);
    buf[res] = '\0';
    printf("Read: %s\n", buf);
  }
finish:
  if (fd != -1)
    close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
mov    0x2d9f(%rip),%rax
or     (%rax),%esi
mov    0x2db6(%rip),%rax
or     (%rax),%esi
lea    0xdd5(%rip),%rdi
call   10f0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1250 <main+0x60>
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14c6 <main+0x2d6>
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1080 <ioctl@plt>
cmp    $0xffffffff,%eax
jne    128f <main+0x9f>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14c6 <main+0x2d6>
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10d0 <tcgetattr@plt>
cmp    $0x0,%rax
je     12c6 <main+0xd6>
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14c6 <main+0x2d6>
lea    0xd8c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x18(%rbp),%rdi
call   10c0 <cfmakeraw@plt>
mov    0x2ca4(%rip),%rax
mov    (%rax),%eax
mov    0x2ca3(%rip),%rcx
or     (%rcx),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2ccc(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1050 <cfsetospeed@plt>
mov    0x2cba(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1100 <cfsetispeed@plt>
lea    0xd4e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2c6f(%rip),%rax
mov    (%rax),%esi
call   1060 <tcflush@plt>
cmp    $0x0,%rax
je     1355 <main+0x165>
lea    0xd33(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14c6 <main+0x2d6>
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2c63(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   10e0 <tcsetattr@plt>
cmp    $0x0,%rax
je     1395 <main+0x1a5>
lea    0xd10(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14c6 <main+0x2d6>
lea    0xd0f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xd0b(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    13d7 <main+0x1e7>
lea    0xcf2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14c6 <main+0x2d6>
lea    0xced(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1030 <tcdrain@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    140d <main+0x21d>
lea    0xcdd(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14c6 <main+0x2d6>
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xcd8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   10b0 <make_blocking@plt>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   10a0 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1471 <main+0x281>
mov    0x2b57(%rip),%rax
mov    (%rax),%rax
mov    0x2b45(%rip),%rcx
cmp    (%rcx),%rax
jne    1471 <main+0x281>
jmp    14c1 <main+0x2d1>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    148e <main+0x29e>
lea    0xc8a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    14c6 <main+0x2d6>
jmp    1493 <main+0x2a3>
mov    -0x14(%rbp),%edi
call   1110 <make_nonblocking@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rsi
lea    0xc64(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1431 <main+0x241>
jmp    14c6 <main+0x2d6>
cmpl   $0xffffffff,-0x14(%rbp)
je     14d8 <main+0x2e8>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x218,%rsp
mov    0x2dd4(%rip),%rax
mov    0x2d9d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db2(%rip),%rax
or     (%rax),%esi
lea    0xdd1(%rip),%rdi
call   1100 <open@plt>
cmp    $0xffffffff,%eax
je     1287 <main+0x87>
mov    %eax,%ebx
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1090 <ioctl@plt>
cmp    $0xffffffff,%eax
je     1298 <main+0x98>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   10e0 <tcgetattr@plt>
test   %rax,%rax
je     12a4 <main+0xa4>
lea    0xe66(%rip),%rdi
jmp    1347 <main+0x147>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
jmp    1353 <main+0x153>
lea    0xe5a(%rip),%rdi
jmp    1347 <main+0x147>
lea    0xd8e(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   10d0 <cfmakeraw@plt>
mov    0x2cc4(%rip),%rax
mov    0x2cc5(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,0x8(%rsp)
mov    0x2cee(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1060 <cfsetospeed@plt>
mov    0x0(%rbp),%esi
mov    %r14,%rdi
call   1110 <cfsetispeed@plt>
lea    0xd56(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c9d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1070 <tcflush@plt>
test   %rax,%rax
je     131a <main+0x11a>
lea    0xdc1(%rip),%rdi
jmp    1347 <main+0x147>
lea    0xd39(%rip),%rdi
call   1030 <puts@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   10f0 <tcsetattr@plt>
test   %rax,%rax
je     1363 <main+0x163>
lea    0xd81(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfc(%rip),%rdi
call   1030 <puts@plt>
lea    0xc97(%rip),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
je     1442 <main+0x242>
lea    0xce0(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1040 <tcdrain@plt>
cmp    $0xffffffff,%eax
je     144e <main+0x24e>
lea    0xcd6(%rip),%rdi
call   1030 <puts@plt>
lea    0xcd5(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   10c0 <make_blocking@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   10b0 <read@plt>
cmp    $0xffffffff,%eax
je     141c <main+0x21c>
mov    %eax,%ebp
lea    0xc2d(%rip),%r14
lea    0x10(%rsp),%r15
mov    %ebx,%edi
call   1120 <make_nonblocking@plt>
movslq %ebp,%rax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x200,%edx
call   10b0 <read@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    13ea <main+0x1ea>
mov    0x2b8d(%rip),%rax
mov    (%rax),%rax
mov    0x2b7b(%rip),%rcx
cmp    (%rcx),%rax
je     134c <main+0x14c>
lea    0xc63(%rip),%rdi
jmp    1347 <main+0x147>
lea    0xc72(%rip),%rdi
jmp    1347 <main+0x147>
lea    0xc57(%rip),%rdi
jmp    1347 <main+0x147>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@.str.1 = [13 x i8] c"open failed\0A\00"
@.str.2 = [18 x i8] c"Setting TIOCEXCL\0A\00"
@TIOCEXCL = global i32 0
@.str.3 = [14 x i8] c"ioctl failed\0A\00"
@.str.4 = [13 x i8] c"Getting tty\0A\00"
@.str.5 = [18 x i8] c"tcgetattr failed\0A\00"
@.str.6 = [21 x i8] c"Setting tty options\0A\00"
@CLOCAL = global i32 0
@CRTSCTS = global i32 0
@B57600 = global i32 0
@.str.7 = [14 x i8] c"Flushing tty\0A\00"
@TCIOFLUSH = global i32 0
@.str.8 = [16 x i8] c"tcflush failed\0A\00"
@.str.9 = [13 x i8] c"Setting tty\0A\00"
@TCSANOW = global i32 0
@.str.10 = [18 x i8] c"tcsetattr failed\0A\00"
@.str.11 = [13 x i8] c"Writing ATZ\0A\00"
@.str.12 = [5 x i8] c"ATZ\0D\00"
@.str.13 = [14 x i8] c"write failed\0A\00"
@.str.14 = [19 x i8] c"Waiting for write\0A\00"
@.str.15 = [16 x i8] c"tcdrain failed\0A\00"
@.str.16 = [12 x i8] c"Wrote: ATZ\0A\00"
@.str.17 = [18 x i8] c"Reading response\0A\00"
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str.18 = [13 x i8] c"read failed\0A\00"
@.str.19 = [10 x i8] c"Read: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.termios
%8 = alloca i32
%9 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_NOCTTY
%12 = or i32 %10, %11
%13 = load i32, i32* @O_SYNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %110
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* %6
%23 = load i32, i32* @TIOCEXCL
%24 = call i32 @ioctl(i32 %22, i32 %23, i32* null)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %110
28: ; preds = %20
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = call i64 @tcgetattr(i32 %30, %struct.termios* %7)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %110
35: ; preds = %28
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
%37 = call i32 @cfmakeraw(%struct.termios* %7)
%38 = load i32, i32* @CLOCAL
%39 = load i32, i32* @CRTSCTS
%40 = or i32 %38, %39
%41 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%42 = load i32, i32* %41
%43 = or i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* @B57600
%45 = call i32 @cfsetospeed(%struct.termios* %7, i32 %44)
%46 = load i32, i32* @B57600
%47 = call i32 @cfsetispeed(%struct.termios* %7, i32 %46)
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = load i32, i32* @TCIOFLUSH
%51 = call i64 @tcflush(i32 %49, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %35
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0))
br label %110
55: ; preds = %35
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
%57 = load i32, i32* %6
%58 = load i32, i32* @TCSANOW
%59 = call i64 @tcsetattr(i32 %57, i32 %58, %struct.termios* %7)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %55
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.10, i64 0, i64 0))
br label %110
63: ; preds = %55
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.11, i64 0, i64 0))
%65 = load i32, i32* %6
%66 = call i32 @write(i32 %65, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i32 4)
store i32 %66, i32* %8
%67 = load i32, i32* %8
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %71
69: ; preds = %63
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.13, i64 0, i64 0))
br label %110
71: ; preds = %63
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.14, i64 0, i64 0))
%73 = load i32, i32* %6
%74 = call i32 @tcdrain(i32 %73)
store i32 %74, i32* %8
%75 = load i32, i32* %8
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %79
77: ; preds = %71
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.15, i64 0, i64 0))
br label %110
79: ; preds = %71
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.16, i64 0, i64 0))
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.17, i64 0, i64 0))
%82 = load i32, i32* %6
%83 = call i32 @make_blocking(i32 %82)
br label %84
84: ; preds = %101, %79
%85 = load i32, i32* %6
%86 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%87 = call i32 @read(i32 %85, i8* %86, i32 512)
store i32 %87, i32* %8
%88 = load i32, i32* %8
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %95
90: ; preds = %84
%91 = load i64, i64* @errno
%92 = load i64, i64* @EWOULDBLOCK
%93 = icmp eq i64 %91, %92
br i1 %93, label %94, label %95
94: ; preds = %90
br label %109
95: ; preds = %90, %84
%96 = load i32, i32* %8
%97 = icmp eq i32 %96, -1
br i1 %97, label %98, label %100
98: ; preds = %95
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.18, i64 0, i64 0))
br label %110
100: ; preds = %95
br label %101
101: ; preds = %100
%102 = load i32, i32* %6
%103 = call i32 @make_nonblocking(i32 %102)
%104 = load i32, i32* %8
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 %105
store i8 0, i8* %106
%107 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.19, i64 0, i64 0), i8* %107)
br label %84
109: ; preds = %94
br label %110
110: ; preds = %109, %98, %77, %69, %61, %53, %33, %26, %18
%111 = load i32, i32* %6
%112 = icmp ne i32 %111, -1
br i1 %112, label %113, label %116
113: ; preds = %110
%114 = load i32, i32* %6
%115 = call i32 @close(i32 %114)
br label %116
116: ; preds = %113, %110
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_SYNC = global i32 0
@TIOCEXCL = global i32 0
@CLOCAL = global i32 0
@CRTSCTS = global i32 0
@B57600 = global i32 0
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.12 = [5 x i8] c"ATZ\0D\00"
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@.str.19 = [10 x i8] c"Read: %s\0A\00"
@str = [17 x i8] c"Setting TIOCEXCL\00"
@str.20 = [12 x i8] c"Getting tty\00"
@str.21 = [20 x i8] c"Setting tty options\00"
@str.22 = [13 x i8] c"Flushing tty\00"
@str.23 = [12 x i8] c"Setting tty\00"
@str.24 = [12 x i8] c"Writing ATZ\00"
@str.25 = [18 x i8] c"Waiting for write\00"
@str.26 = [11 x i8] c"Wrote: ATZ\00"
@str.27 = [17 x i8] c"Reading response\00"
@str.28 = [12 x i8] c"read failed\00"
@str.29 = [15 x i8] c"tcdrain failed\00"
@str.30 = [13 x i8] c"write failed\00"
@str.31 = [17 x i8] c"tcsetattr failed\00"
@str.32 = [15 x i8] c"tcflush failed\00"
@str.33 = [17 x i8] c"tcgetattr failed\00"
@str.34 = [13 x i8] c"ioctl failed\00"
@str.35 = [12 x i8] c"open failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.termios
%4 = alloca [512 x i8]
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.35, i64 0, i64 0))
br label %89
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%16 = load i32, i32* @TIOCEXCL
%17 = tail call i32 @ioctl(i32 %10, i32 %16, i32* null)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.34, i64 0, i64 0))
br label %87
21: ; preds = %14
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.20, i64 0, i64 0))
%23 = call i64 @tcgetattr(i32 %10, %struct.termios* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.33, i64 0, i64 0))
br label %87
27: ; preds = %21
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.21, i64 0, i64 0))
%29 = call i32 @cfmakeraw(%struct.termios* %3)
%30 = load i32, i32* @CLOCAL
%31 = load i32, i32* @CRTSCTS
%32 = or i32 %31, %30
%33 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%34 = load i32, i32* %33
%35 = or i32 %32, %34
store i32 %35, i32* %33
%36 = load i32, i32* @B57600
%37 = call i32 @cfsetospeed(%struct.termios* %3, i32 %36)
%38 = load i32, i32* @B57600
%39 = call i32 @cfsetispeed(%struct.termios* %3, i32 %38)
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.22, i64 0, i64 0))
%41 = load i32, i32* @TCIOFLUSH
%42 = call i64 @tcflush(i32 %10, i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %46, label %44
44: ; preds = %27
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.32, i64 0, i64 0))
br label %87
46: ; preds = %27
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.23, i64 0, i64 0))
%48 = load i32, i32* @TCSANOW
%49 = call i64 @tcsetattr(i32 %10, i32 %48, %struct.termios* %3)
%50 = icmp eq i64 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %46
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.31, i64 0, i64 0))
br label %87
53: ; preds = %46
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.24, i64 0, i64 0))
%55 = call i32 @write(i32 %10, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.12, i64 0, i64 0), i32 4)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %59
57: ; preds = %53
%58 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.30, i64 0, i64 0))
br label %87
59: ; preds = %53
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.25, i64 0, i64 0))
%61 = call i32 @tcdrain(i32 %10)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.29, i64 0, i64 0))
br label %87
65: ; preds = %59
%66 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.26, i64 0, i64 0))
%67 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.27, i64 0, i64 0))
%68 = call i32 @make_blocking(i32 %10)
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%70 = call i32 @read(i32 %10, i8* %69, i32 512)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %78
72: ; preds = %78, %65
%73 = load i64, i64* @errno
%74 = load i64, i64* @EWOULDBLOCK
%75 = icmp eq i64 %73, %74
br i1 %75, label %86, label %76
76: ; preds = %72
%77 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.28, i64 0, i64 0))
br label %86
78: ; preds = %65, %78
%79 = phi i32 [ %84, %78 ], [ %70, %65 ]
%80 = call i32 @make_nonblocking(i32 %10)
%81 = sext i32 %79 to i64
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %81
store i8 0, i8* %82
%83 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.19, i64 0, i64 0), i8* %69)
%84 = call i32 @read(i32 %10, i8* %69, i32 512)
%85 = icmp eq i32 %84, -1
br i1 %85, label %72, label %78
86: ; preds = %76, %72
br label %87
87: ; preds = %86, %19, %25, %44, %51, %57, %63
%88 = call i32 @close(i32 %10)
br label %89
89: ; preds = %12, %87
ret i32 0
}