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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCM_DTR;
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setterminal(int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ tty_CTRL(int, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int res;
  printf("Prog\n");
  int fdn = open("/dev/ttyUSB0", O_RDWR);
  if (fdn < 0) {
    perror("");
    return 1;
  }
  setterminal(fdn);
  tty_CTRL(fdn, TIOCM_DTR, 0);
  FILE *fd = fdopen(fdn, "r+");
  if (fd == NULL) {
    perror("");
    return 1;
  }
  sleep(2);
  fprintf(fd, "ATtE:230,250,16\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d5 <main+0x55>
lea    0xe62(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1247 <main+0xc7>
mov    -0xc(%rbp),%edi
call   1070 <setterminal@plt>
mov    -0xc(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <tty_CTRL@plt>
mov    -0xc(%rbp),%edi
lea    0xe19(%rip),%rsi
call   1060 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1226 <main+0xa6>
lea    0xe11(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1247 <main+0xc7>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    -0x18(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe97(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1207 <main+0x87>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <setterminal@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %edx,%edx
call   1040 <tty_CTRL@plt>
lea    0xe41(%rip),%rsi
mov    %ebp,%edi
call   1050 <fdopen@plt>
test   %rax,%rax
je     1207 <main+0x87>
mov    %rax,%rbp
mov    $0x2,%edi
call   10a0 <sleep@plt>
lea    0xe24(%rip),%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1090 <fwrite@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe12(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%ebx
jmp    11fe <main+0x7e>

LLVM IR (-O0):
@.str = [6 x i8] c"Prog\0A\00"
@.str.1 = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@.str.2 = [1 x i8] zeroinitializer
@TIOCM_DTR = global i32 0
@.str.3 = [3 x i8] c"r+\00"
@.str.4 = [17 x i8] c"ATtE:230,250,16\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %28
12: ; preds = %0
%13 = load i32, i32* %3
%14 = call i32 @setterminal(i32 %13)
%15 = load i32, i32* %3
%16 = load i32, i32* @TIOCM_DTR
%17 = call i32 @tty_CTRL(i32 %15, i32 %16, i32 0)
%18 = load i32, i32* %3
%19 = call i32* @fdopen(i32 %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %19, i32** %4
%20 = load i32*, i32** %4
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %28
24: ; preds = %12
%25 = call i32 @sleep(i32 2)
%26 = load i32*, i32** %4
%27 = call i32 @fprintf(i32* %26, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %28
28: ; preds = %24, %22, %10
%29 = load i32, i32* %1
ret i32 %29
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"/dev/ttyUSB0\00"
@O_RDWR = global i32 0
@.str.2 = [1 x i8] zeroinitializer
@TIOCM_DTR = global i32 0
@.str.3 = [3 x i8] c"r+\00"
@.str.4 = [17 x i8] c"ATtE:230,250,16\0A\00"
@str = [5 x i8] c"Prog\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %18
7: ; preds = %0
%8 = tail call i32 @setterminal(i32 %3)
%9 = load i32, i32* @TIOCM_DTR
%10 = tail call i32 @tty_CTRL(i32 %3, i32 %9, i32 0)
%11 = tail call i32* @fdopen(i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @perror(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
br label %18
15: ; preds = %7
%16 = tail call i32 @sleep(i32 2)
%17 = tail call i64 @fwrite(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i64 16, i64 1, i32* %11)
br label %18
18: ; preds = %13, %15, %5
%19 = phi i32 [ 1, %5 ], [ 1, %13 ], [ 0, %15 ]
ret i32 %19
}