#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 */
typedef int uintmax_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCMGET;
unsigned int TIOCM_CD;
unsigned int TIOCM_DSR;
int UINTMAX_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ doit(int, int);
int ioctl(int, int /*<<< orphan*/, unsigned int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ tty_init(int);
int main(void) {
  int fd;
  uintmax_t i;
  unsigned control = 0;
  fd = open("/dev/ttyACM0", O_RDWR);
  if (fd < 0) {
    perror("ACM0");
    return -1;
  }
  while (1) {
    int ret;
    ret = ioctl(fd, TIOCMGET, &control);
    if (ret < 0) {
      perror("ioctl");
      close(fd);
      goto out;
    }
    if ((control & TIOCM_CD) && (control & TIOCM_DSR))
      break;
    printf("waiting DCD | DSR\n");
  }
  if (tty_init(fd))
    goto out;
  for (i = 1; i < UINTMAX_MAX; i++)
    doit(fd, i);
out:
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c0 <main+0x50>
lea    0xe59(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <main+0x12d>
jmp    11c5 <main+0x55>
mov    -0x8(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1200 <main+0x90>
lea    0xe1f(%rip),%rdi
call   1090 <perror@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
jmp    128e <main+0x11e>
mov    -0x10(%rbp),%eax
mov    0x2db6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     122f <main+0xbf>
mov    -0x10(%rbp),%eax
mov    0x2db9(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     122f <main+0xbf>
jmp    1240 <main+0xd0>
lea    0xddd(%rip),%rdi
call   1030 <printf@plt>
jmp    11c5 <main+0x55>
mov    -0x8(%rbp),%edi
call   1050 <tty_init@plt>
cmp    $0x0,%rax
je     1257 <main+0xe7>
jmp    128e <main+0x11e>
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    0x2d88(%rip),%rcx
cmp    (%rcx),%eax
jge    1289 <main+0x119>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1070 <doit@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    125e <main+0xee>
jmp    128e <main+0x11e>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     126e <main+0xfe>
mov    %eax,%ebx
mov    0x2e27(%rip),%r12
mov    (%r12),%esi
lea    0x4(%rsp),%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
test   %eax,%eax
js     1243 <main+0xd3>
mov    0x2df8(%rip),%rbp
lea    0xe44(%rip),%r14
lea    0x4(%rsp),%r15
mov    0x2dfd(%rip),%r13
jmp    11fa <main+0x8a>
nopl   (%rax)
mov    %r14,%rdi
call   1030 <puts@plt>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1040 <ioctl@plt>
test   %eax,%eax
js     1243 <main+0xd3>
mov    0x4(%rsp),%eax
test   %eax,0x0(%rbp)
je     11e0 <main+0x70>
test   %eax,0x0(%r13)
je     11e0 <main+0x70>
mov    %ebx,%edi
call   1050 <tty_init@plt>
test   %rax,%rax
jne    1256 <main+0xe6>
mov    0x2dd4(%rip),%r14
cmpl   $0x2,(%r14)
jl     1256 <main+0xe6>
mov    $0x1,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %ebp,%esi
call   1070 <doit@plt>
add    $0x1,%ebp
cmp    (%r14),%ebp
jl     1230 <main+0xc0>
jmp    1256 <main+0xe6>
lea    0xdc3(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd93(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%eax
jmp    125f <main+0xef>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"ACM0\00"
@TIOCMGET = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@TIOCM_CD = global i32 0
@TIOCM_DSR = global i32 0
@.str.3 = [19 x i8] c"waiting DCD | DSR\0A\00"
@UINTMAX_MAX = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %4
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %57
12: ; preds = %0
br label %13
13: ; preds = %12, %34
%14 = load i32, i32* %2
%15 = load i32, i32* @TIOCMGET
%16 = call i32 @ioctl(i32 %14, i32 %15, i32* %4)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
br label %54
23: ; preds = %13
%24 = load i32, i32* %4
%25 = load i32, i32* @TIOCM_CD
%26 = and i32 %24, %25
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %23
%29 = load i32, i32* %4
%30 = load i32, i32* @TIOCM_DSR
%31 = and i32 %29, %30
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %28
br label %36
34: ; preds = %28, %23
%35 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %13
36: ; preds = %33
%37 = load i32, i32* %2
%38 = call i64 @tty_init(i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
br label %54
41: ; preds = %36
store i32 1, i32* %3
br label %42
42: ; preds = %50, %41
%43 = load i32, i32* %3
%44 = load i32, i32* @UINTMAX_MAX
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %53
46: ; preds = %42
%47 = load i32, i32* %2
%48 = load i32, i32* %3
%49 = call i32 @doit(i32 %47, i32 %48)
br label %50
50: ; preds = %46
%51 = load i32, i32* %3
%52 = add nsw i32 %51, 1
store i32 %52, i32* %3
br label %42
53: ; preds = %42
br label %54
54: ; preds = %53, %40, %19
%55 = load i32, i32* %2
%56 = call i32 @close(i32 %55)
store i32 0, i32* %1
br label %57
57: ; preds = %54, %10
%58 = load i32, i32* %1
ret i32 %58
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttyACM0\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"ACM0\00"
@TIOCMGET = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@TIOCM_CD = global i32 0
@TIOCM_DSR = global i32 0
@UINTMAX_MAX = global i32 0
@str = [18 x i8] c"waiting DCD | DSR\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %10, label %6
6: ; preds = %0
%7 = load i32, i32* @TIOCMGET
%8 = call i32 @ioctl(i32 %4, i32 %7, i32* %1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %12, label %15
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %43
12: ; preds = %24, %6
%13 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%14 = call i32 @close(i32 %4)
br label %41
15: ; preds = %6, %24
%16 = load i32, i32* %1
%17 = load i32, i32* @TIOCM_CD
%18 = and i32 %17, %16
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32, i32* @TIOCM_DSR
%22 = and i32 %21, %16
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %15, %20
%25 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%26 = load i32, i32* @TIOCMGET
%27 = call i32 @ioctl(i32 %4, i32 %26, i32* %1)
%28 = icmp slt i32 %27, 0
br i1 %28, label %12, label %15
29: ; preds = %20
%30 = call i64 @tty_init(i32 %4)
%31 = icmp eq i64 %30, 0
%32 = load i32, i32* @UINTMAX_MAX
%33 = icmp sgt i32 %32, 1
%34 = select i1 %31, i1 %33, i1 false
br i1 %34, label %35, label %41
35: ; preds = %29, %35
%36 = phi i32 [ %38, %35 ], [ 1, %29 ]
%37 = call i32 @doit(i32 %4, i32 %36)
%38 = add nuw nsw i32 %36, 1
%39 = load i32, i32* @UINTMAX_MAX
%40 = icmp slt i32 %38, %39
br i1 %40, label %35, label %41
41: ; preds = %35, %12, %29
%42 = call i32 @close(i32 %4)
br label %43
43: ; preds = %41, %10
%44 = phi i32 [ -1, %10 ], [ 0, %41 ]
ret i32 %44
}