#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;
  int /*<<< orphan*/ c_lflag;
  int /*<<< orphan*/ c_oflag;
  int /*<<< orphan*/ c_iflag;
};

/* Variables and functions */
int B9600;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  unsigned char frame[1];
  int fd;
  struct termios tio = {.c_cflag = B9600 | CS8 | CLOCAL | CREAD,
                        .c_iflag = IGNPAR,
                        .c_oflag = 0,
                        .c_lflag = 0};
  if (argc != 2) {
    printf("name serial port\n");
    exit(1);
  }
  fd = open(argv[1], O_RDWR | O_NOCTTY);
  if (fd == -1) {
    printf("error open serial port\n");
    exit(1);
  }
  tcgetattr(fd, &tio);
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &tio);
  write(fd, "\x01\x03\x01\x68\x00\x02\x44\x2B", 8);
  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)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    0x2e32(%rip),%rcx
or     (%rcx),%eax
mov    0x2df9(%rip),%rcx
or     (%rcx),%eax
mov    0x2e28(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11e7 <main+0x77>
lea    0xe28(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1229 <main+0xb9>
lea    0xdf8(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x18(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x18(%rbp),%edi
mov    0x2d90(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x18(%rbp),%edi
lea    0xdc5(%rip),%rsi
mov    $0x8,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e5a(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e08(%rip),%rax
or     (%rax),%ecx
mov    0x2e37(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
movq   $0x0,0xc(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
cmp    $0x2,%edi
jne    122b <main+0xbb>
mov    0x8(%rsi),%rdi
mov    0x2e23(%rip),%rax
mov    0x2de4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1234 <main+0xc4>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <tcflush@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
lea    0xdeb(%rip),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1040 <write@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdee(%rip),%rdi
jmp    123b <main+0xcb>
lea    0xdce(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32, i32, i32 }
@B9600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@.str = [18 x i8] c"name serial port\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [24 x i8] c"error open serial port\0A\00"
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [9 x i8] c"\01\03\01h\00\02D+\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1 x i8]
%7 = alloca i32
%8 = alloca %struct.termios
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%10 = load i32, i32* @B9600
%11 = load i32, i32* @CS8
%12 = or i32 %10, %11
%13 = load i32, i32* @CLOCAL
%14 = or i32 %12, %13
%15 = load i32, i32* @CREAD
%16 = or i32 %14, %15
store i32 %16, i32* %9
%17 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 1
store i32 0, i32* %17
%18 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 2
store i32 0, i32* %18
%19 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 3
%20 = load i32, i32* @IGNPAR
store i32 %20, i32* %19
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 2
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_RDWR
%31 = load i32, i32* @O_NOCTTY
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %29, i32 %32)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %26
%40 = load i32, i32* %7
%41 = call i32 @tcgetattr(i32 %40, %struct.termios* %8)
%42 = load i32, i32* %7
%43 = load i32, i32* @TCIFLUSH
%44 = call i32 @tcflush(i32 %42, i32 %43)
%45 = load i32, i32* %7
%46 = load i32, i32* @TCSANOW
%47 = call i32 @tcsetattr(i32 %45, i32 %46, %struct.termios* %8)
%48 = load i32, i32* %7
%49 = call i32 @write(i32 %48, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 8)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32, i32, i32, i32 }
@B9600 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.2 = [9 x i8] c"\01\03\01h\00\02D+\00"
@str = [23 x i8] c"error open serial port\00"
@str.3 = [17 x i8] c"name serial port\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%6 = load i32, i32* @B9600
%7 = load i32, i32* @CS8
%8 = or i32 %7, %6
%9 = load i32, i32* @CLOCAL
%10 = or i32 %8, %9
%11 = load i32, i32* @CREAD
%12 = or i32 %10, %11
store i32 %12, i32* %5
%13 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%16 = load i32, i32* @IGNPAR
store i32 %16, i32* %15
%17 = icmp eq i32 %0, 2
br i1 %17, label %21, label %18
18: ; preds = %2
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.3, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NOCTTY
%26 = or i32 %25, %24
%27 = tail call i32 @open(i8* %23, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %21
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = call i32 @tcgetattr(i32 %27, %struct.termios* %3)
%34 = load i32, i32* @TCIFLUSH
%35 = call i32 @tcflush(i32 %27, i32 %34)
%36 = load i32, i32* @TCSANOW
%37 = call i32 @tcsetattr(i32 %27, i32 %36, %struct.termios* %3)
%38 = call i32 @write(i32 %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 8)
ret i32 0
}