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

/* Variables and functions */
int B38400;
int CLOCAL;
int CREAD;
int CS8;
int /*<<< orphan*/ IGNPAR;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCIFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ bzero(struct termios *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int write(int, unsigned char *, int);
int main(int argc, char **argv) {
  int fd, c, res;
  struct termios oldtio, newtio;
  char buf[255];
  unsigned char UA[5];
  UA[0] = 0x7e;
  UA[1] = 0x03;
  UA[2] = 0x03;
  UA[3] = 0x03 ^ 0x03;
  UA[4] = 0x7e;
  if (argc < 2) {
    printf("Usage:\tnserial SerialPort\n\tex: nserial /dev/ttyS1\n");
    exit(1);
  }
  fd = open(argv[1], O_RDWR | O_NOCTTY);
  if (fd < 0) {
    perror(argv[1]);
    exit(-1);
  }
  tcgetattr(fd, &oldtio);
  bzero(&newtio, sizeof(newtio));
  newtio.c_cflag = B38400 | CS8 | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR;
  newtio.c_oflag = 0;
  newtio.c_lflag = 0;
  newtio.c_cc[VTIME] = 0;
  newtio.c_cc[VMIN] = 5;
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd, TCSANOW, &newtio);
  res = 0;
  while (res == 0) {
    res = read(fd, buf, 255);
  }
  printf("Message Read\n");
  res = write(fd, UA, res);
  printf("Message resent\n");
  tcsetattr(fd, TCSANOW, &oldtio);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x7e,-0x175(%rbp)
movb   $0x3,-0x174(%rbp)
movb   $0x3,-0x173(%rbp)
movb   $0x0,-0x172(%rbp)
movb   $0x7e,-0x171(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    120c <main+0x5c>
lea    0xe03(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124f <main+0x9f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1080 <tcgetattr@plt>
lea    -0x70(%rbp),%rdi
mov    $0x28,%esi
call   10c0 <bzero@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    0x2d57(%rip),%rcx
or     (%rcx),%eax
mov    0x2d0e(%rip),%rcx
or     (%rcx),%eax
mov    0x2d4d(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x70(%rbp)
mov    0x2d09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x68(%rbp),%rax
mov    0x2cf1(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x68(%rbp),%rax
mov    0x2cfc(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x14(%rbp),%edi
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    -0x14(%rbp),%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
lea    -0x70(%rbp),%rdx
call   1090 <tcsetattr@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1329 <main+0x179>
mov    -0x14(%rbp),%edi
lea    -0x170(%rbp),%rsi
mov    $0xff,%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
jmp    1303 <main+0x153>
lea    0xd03(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x175(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
lea    0xcf0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2c80(%rip),%rax
mov    (%rax),%esi
lea    -0x48(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x160,%rsp
movl   $0x3037e,0xb(%rsp)
movb   $0x7e,0xf(%rsp)
cmp    $0x1,%edi
jle    1318 <main+0x168>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e05(%rip),%rax
mov    0x2dc6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   10a0 <open@plt>
test   %eax,%eax
js     132e <main+0x17e>
mov    %eax,%r15d
lea    0x38(%rsp),%rsi
mov    %eax,%edi
call   1080 <tcgetattr@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x28,%esi
call   10c0 <bzero@plt>
mov    0x2d65(%rip),%rax
mov    0x2da6(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d5b(%rip),%rax
or     (%rax),%ecx
mov    0x2d9a(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
mov    0x18(%rsp),%rax
mov    0x2d43(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d52(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    0x2d31(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
call   1050 <tcflush@plt>
mov    0x2d48(%rip),%r12
mov    (%r12),%esi
mov    %r15d,%edi
mov    %r14,%rdx
call   1090 <tcsetattr@plt>
lea    0x60(%rsp),%rbx
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1070 <read@plt>
test   %eax,%eax
je     12b0 <main+0x100>
mov    %eax,%ebp
lea    0xd33(%rip),%rdi
call   1030 <puts@plt>
lea    0xb(%rsp),%rsi
mov    %r15d,%edi
mov    %ebp,%edx
call   1040 <write@plt>
lea    0xd25(%rip),%rdi
call   1030 <puts@plt>
mov    (%r12),%esi
lea    0x38(%rsp),%rdx
mov    %r15d,%edi
call   1090 <tcsetattr@plt>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcfd(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x8(%r14),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i64, i64, i32 }
@.str = [51 x i8] c"Usage:\09nserial SerialPort\0A\09ex: nserial /dev/ttyS1\0A\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B38400 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.1 = [14 x i8] c"Message Read\0A\00"
@.str.2 = [16 x i8] c"Message resent\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.termios
%10 = alloca %struct.termios
%11 = alloca [255 x i8]
%12 = alloca [5 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 0
store i8 126, i8* %13
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 1
store i8 3, i8* %14
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 2
store i8 3, i8* %15
%16 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 3
store i8 0, i8* %16
%17 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 4
store i8 126, i8* %17
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 2
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_RDWR
%28 = load i32, i32* @O_NOCTTY
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %26, i32 %29)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %23
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @perror(i8* %36)
%38 = call i32 @exit(i32 -1)
unreachable
39: ; preds = %23
%40 = load i32, i32* %6
%41 = call i32 @tcgetattr(i32 %40, %struct.termios* %9)
%42 = call i32 @bzero(%struct.termios* %10, i32 40)
%43 = load i32, i32* @B38400
%44 = load i32, i32* @CS8
%45 = or i32 %43, %44
%46 = load i32, i32* @CLOCAL
%47 = or i32 %45, %46
%48 = load i32, i32* @CREAD
%49 = or i32 %47, %48
%50 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 0
store i32 %49, i32* %50
%51 = load i32, i32* @IGNPAR
%52 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 4
store i32 %51, i32* %52
%53 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 3
store i64 0, i64* %53
%54 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 2
store i64 0, i64* %54
%55 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 1
%56 = load i32*, i32** %55
%57 = load i64, i64* @VTIME
%58 = getelementptr inbounds i32, i32* %56, i64 %57
store i32 0, i32* %58
%59 = getelementptr inbounds %struct.termios, %struct.termios* %10, i32 0, i32 1
%60 = load i32*, i32** %59
%61 = load i64, i64* @VMIN
%62 = getelementptr inbounds i32, i32* %60, i64 %61
store i32 5, i32* %62
%63 = load i32, i32* %6
%64 = load i32, i32* @TCIFLUSH
%65 = call i32 @tcflush(i32 %63, i32 %64)
%66 = load i32, i32* %6
%67 = load i32, i32* @TCSANOW
%68 = call i32 @tcsetattr(i32 %66, i32 %67, %struct.termios* %10)
store i32 0, i32* %8
br label %69
69: ; preds = %72, %39
%70 = load i32, i32* %8
%71 = icmp eq i32 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %69
%73 = load i32, i32* %6
%74 = getelementptr inbounds [255 x i8], [255 x i8]* %11, i64 0, i64 0
%75 = call i32 @read(i32 %73, i8* %74, i32 255)
store i32 %75, i32* %8
br label %69
76: ; preds = %69
%77 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%78 = load i32, i32* %6
%79 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 0
%80 = load i32, i32* %8
%81 = call i32 @write(i32 %78, i8* %79, i32 %80)
store i32 %81, i32* %8
%82 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%83 = load i32, i32* %6
%84 = load i32, i32* @TCSANOW
%85 = call i32 @tcsetattr(i32 %83, i32 %84, %struct.termios* %9)
%86 = load i32, i32* %6
%87 = call i32 @close(i32 %86)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i64, i64, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B38400 = global i32 0
@CS8 = global i32 0
@CLOCAL = global i32 0
@CREAD = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCIFLUSH = global i32 0
@TCSANOW = global i32 0
@str = [13 x i8] c"Message Read\00"
@str.3 = [15 x i8] c"Message resent\00"
@str.4 = [50 x i8] c"Usage:\09nserial SerialPort\0A\09ex: nserial /dev/ttyS1\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca [255 x i8]
%6 = alloca [5 x i8]
%7 = bitcast %struct.termios* %3 to i8*
%8 = bitcast %struct.termios* %4 to i8*
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 0
store i8 126, i8* %10
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 1
store i8 3, i8* %11
%12 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 2
store i8 3, i8* %12
%13 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 3
store i8 0, i8* %13
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %6, i64 0, i64 4
store i8 126, i8* %14
%15 = icmp slt i32 %0, 2
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.4, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDWR
%23 = load i32, i32* @O_NOCTTY
%24 = or i32 %23, %22
%25 = tail call i32 @open(i8* %21, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = load i8*, i8** %20
%29 = tail call i32 @perror(i8* %28)
%30 = tail call i32 @exit(i32 -1)
unreachable
31: ; preds = %19
%32 = call i32 @tcgetattr(i32 %25, %struct.termios* %3)
%33 = call i32 @bzero(%struct.termios* %4, i32 40)
%34 = load i32, i32* @B38400
%35 = load i32, i32* @CS8
%36 = or i32 %35, %34
%37 = load i32, i32* @CLOCAL
%38 = or i32 %36, %37
%39 = load i32, i32* @CREAD
%40 = or i32 %38, %39
%41 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* @IGNPAR
%43 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 4
store i32 %42, i32* %43
%44 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 2
%45 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 1
%46 = bitcast i64* %44 to i8*
%47 = load i32*, i32** %45
%48 = load i64, i64* @VTIME
%49 = getelementptr inbounds i32, i32* %47, i64 %48
store i32 0, i32* %49
%50 = load i64, i64* @VMIN
%51 = getelementptr inbounds i32, i32* %47, i64 %50
store i32 5, i32* %51
%52 = load i32, i32* @TCIFLUSH
%53 = call i32 @tcflush(i32 %25, i32 %52)
%54 = load i32, i32* @TCSANOW
%55 = call i32 @tcsetattr(i32 %25, i32 %54, %struct.termios* %4)
br label %56
56: ; preds = %31, %56
%57 = call i32 @read(i32 %25, i8* %9, i32 255)
%58 = icmp eq i32 %57, 0
br i1 %58, label %56, label %59
59: ; preds = %56
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%61 = call i32 @write(i32 %25, i8* %10, i32 %57)
%62 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0))
%63 = load i32, i32* @TCSANOW
%64 = call i32 @tcsetattr(i32 %25, i32 %63, %struct.termios* %3)
%65 = call i32 @close(i32 %25)
ret i32 0
}