#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_iflag;
  int *c_cc;
  unsigned short c_cflag;
  scalar_t__ c_line;
  scalar_t__ c_lflag;
  scalar_t__ c_oflag;
};

/* Variables and functions */
unsigned short B1200;
unsigned short CLOCAL;
unsigned short CREAD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
unsigned short HUPCL;
int IGNBRK;
int IGNPAR;
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, ...);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int main(int argc, char **argv) {
  int fd, rc;
  char *c;
  struct termios tty;
  unsigned short flags = (CREAD | CLOCAL | HUPCL);
  if (argc < 2) {
    printf(" give /dev/ttyS0 or /dev/ttyS1 as an argument!\n");
    exit(-1);
  }
  fd = open(argv[1], O_RDWR | O_NONBLOCK);
  printf(" fd = %d from opening %s\n", fd, argv[1]);
  rc = fcntl(fd, F_GETFL);
  rc = fcntl(fd, F_SETFL, O_RDWR);
  rc = tcgetattr(fd, &tty);
  printf("rc = %d from tcsetattr\n", rc);
  tty.c_iflag = IGNBRK | IGNPAR;
  tty.c_oflag = 0;
  tty.c_lflag = 0;
  tty.c_line = 0;
  tty.c_cc[VTIME] = 0;
  tty.c_cc[VMIN] = 1;
  tty.c_cflag = flags | B1200;
  c = "*n";
  rc = write(fd, c, 2);
  printf("rc = %d from write\n", rc);
  usleep(100000);
  rc = tcsetattr(fd, TCSAFLUSH, &tty);
  printf("rc = %d from tcsetattr\n", rc);
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e23(%rip),%rax
movzwl (%rax),%eax
mov    0x2dc1(%rip),%rcx
movzwl (%rcx),%ecx
or     %ecx,%eax
mov    0x2dcd(%rip),%rcx
movzwl (%rcx),%ecx
or     %ecx,%eax
mov    %ax,-0x52(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11ee <main+0x5e>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d1b(%rip),%rax
mov    (%rax),%eax
mov    0x2cfa(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x50(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x48(%rbp),%rax
mov    0x2ce2(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x48(%rbp),%rax
mov    0x2cfd(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
movzwl -0x52(%rbp),%eax
mov    0x2cc8(%rip),%rcx
movzwl (%rcx),%ecx
or     %ecx,%eax
mov    %ax,-0x40(%rbp)
lea    0xd72(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x2,%edx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x186a0,%edi
call   10b0 <usleep@plt>
mov    -0x14(%rbp),%edi
mov    0x2cb3(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xd05(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
cmp    $0x1,%edi
jg     11c7 <main+0x27>
lea    0xe91(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    %rsi,%rbx
mov    0x2dff(%rip),%rax
mov    0x2da0(%rip),%rcx
movzwl (%rcx),%ebp
or     (%rax),%bp
mov    0x2dab(%rip),%rax
or     (%rax),%bp
mov    0x8(%rsi),%rdi
mov    0x2df5(%rip),%r14
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
call   10a0 <open@plt>
mov    %eax,%r12d
mov    0x8(%rbx),%rdx
lea    0xdee(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%esi
mov    (%r14),%edx
mov    %r12d,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    %rsp,%r14
mov    %r12d,%edi
mov    %r14,%rsi
call   1080 <tcgetattr@plt>
lea    0xdc1(%rip),%r15
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d34(%rip),%rax
mov    0x2d15(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movq   $0x0,0x28(%rsp)
mov    0x8(%rsp),%rax
mov    0x2d01(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d20(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2cef(%rip),%rax
or     (%rax),%bp
mov    %bp,0x10(%rsp)
lea    0xd6a(%rip),%rsi
mov    %r12d,%edi
mov    $0x2,%edx
call   1050 <write@plt>
lea    0xd59(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x186a0,%edi
call   10c0 <usleep@plt>
mov    0x2cea(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   1090 <tcsetattr@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32, i32*, i16, i64, i64, i64 }
@CREAD = global i16 0
@CLOCAL = global i16 0
@HUPCL = global i16 0
@.str = [48 x i8] c" give /dev/ttyS0 or /dev/ttyS1 as an argument!\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [26 x i8] c" fd = %d from opening %s\0A\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@.str.2 = [24 x i8] c"rc = %d from tcsetattr\0A\00"
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@B1200 = global i16 0
@.str.3 = [3 x i8] c"*n\00"
@.str.4 = [20 x i8] c"rc = %d from write\0A\00"
@TCSAFLUSH = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.termios
%10 = alloca i16
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i16, i16* @CREAD
%12 = zext i16 %11 to i32
%13 = load i16, i16* @CLOCAL
%14 = zext i16 %13 to i32
%15 = or i32 %12, %14
%16 = load i16, i16* @HUPCL
%17 = zext i16 %16 to i32
%18 = or i32 %15, %17
%19 = trunc i32 %18 to i16
store i16 %19, i16* %10
%20 = load i32, i32* %4
%21 = icmp slt i32 %20, 2
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @O_RDWR
%30 = load i32, i32* @O_NONBLOCK
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %28, i32 %31)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %33, i8* %36)
%38 = load i32, i32* %6
%39 = load i32, i32* @F_GETFL
%40 = call i32 (i32, i32, ...) @fcntl(i32 %38, i32 %39)
store i32 %40, i32* %7
%41 = load i32, i32* %6
%42 = load i32, i32* @F_SETFL
%43 = load i32, i32* @O_RDWR
%44 = call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %42, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %6
%46 = call i32 @tcgetattr(i32 %45, %struct.termios* %9)
store i32 %46, i32* %7
%47 = load i32, i32* %7
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = load i32, i32* @IGNBRK
%50 = load i32, i32* @IGNPAR
%51 = or i32 %49, %50
%52 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
store i32 %51, i32* %52
%53 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 5
store i64 0, i64* %53
%54 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 4
store i64 0, i64* %54
%55 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 3
store i64 0, i64* %55
%56 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 1
%57 = load i32*, i32** %56
%58 = load i64, i64* @VTIME
%59 = getelementptr inbounds i32, i32* %57, i64 %58
store i32 0, i32* %59
%60 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 1
%61 = load i32*, i32** %60
%62 = load i64, i64* @VMIN
%63 = getelementptr inbounds i32, i32* %61, i64 %62
store i32 1, i32* %63
%64 = load i16, i16* %10
%65 = zext i16 %64 to i32
%66 = load i16, i16* @B1200
%67 = zext i16 %66 to i32
%68 = or i32 %65, %67
%69 = trunc i32 %68 to i16
%70 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 2
store i16 %69, i16* %70
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %8
%71 = load i32, i32* %6
%72 = load i8*, i8** %8
%73 = call i32 @write(i32 %71, i8* %72, i32 2)
store i32 %73, i32* %7
%74 = load i32, i32* %7
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %74)
%76 = call i32 @usleep(i32 100000)
%77 = load i32, i32* %6
%78 = load i32, i32* @TCSAFLUSH
%79 = call i32 @tcsetattr(i32 %77, i32 %78, %struct.termios* %9)
store i32 %79, i32* %7
%80 = load i32, i32* %7
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %80)
%82 = load i32, i32* %6
%83 = call i32 @close(i32 %82)
%84 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.termios = type { i32, i32*, i16, i64, i64, i64 }
@CREAD = global i16 0
@CLOCAL = global i16 0
@HUPCL = global i16 0
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [26 x i8] c" fd = %d from opening %s\0A\00"
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@.str.2 = [24 x i8] c"rc = %d from tcsetattr\0A\00"
@IGNBRK = global i32 0
@IGNPAR = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@B1200 = global i16 0
@.str.3 = [3 x i8] c"*n\00"
@.str.4 = [20 x i8] c"rc = %d from write\0A\00"
@TCSAFLUSH = global i32 0
@str = [47 x i8] c" give /dev/ttyS0 or /dev/ttyS1 as an argument!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios
%4 = bitcast %struct.termios* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 -1)
unreachable
9: ; preds = %2
%10 = load i16, i16* @CREAD
%11 = load i16, i16* @CLOCAL
%12 = or i16 %11, %10
%13 = load i16, i16* @HUPCL
%14 = or i16 %12, %13
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %17
%20 = tail call i32 @open(i8* %16, i32 %19)
%21 = load i8*, i8** %15
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %21)
%23 = load i32, i32* @F_GETFL
%24 = tail call i32 (i32, i32, ...) @fcntl(i32 %20, i32 %23)
%25 = load i32, i32* @F_SETFL
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 (i32, i32, ...) @fcntl(i32 %20, i32 %25, i32 %26)
%28 = call i32 @tcgetattr(i32 %20, %struct.termios* %3)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%30 = load i32, i32* @IGNBRK
%31 = load i32, i32* @IGNPAR
%32 = or i32 %31, %30
%33 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
store i32 %32, i32* %33
%34 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 3
%35 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 1
%36 = bitcast i64* %34 to i8*
%37 = load i32*, i32** %35
%38 = load i64, i64* @VTIME
%39 = getelementptr inbounds i32, i32* %37, i64 %38
store i32 0, i32* %39
%40 = load i64, i64* @VMIN
%41 = getelementptr inbounds i32, i32* %37, i64 %40
store i32 1, i32* %41
%42 = load i16, i16* @B1200
%43 = or i16 %14, %42
%44 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 2
store i16 %43, i16* %44
%45 = call i32 @write(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 2)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = call i32 @usleep(i32 100000)
%48 = load i32, i32* @TCSAFLUSH
%49 = call i32 @tcsetattr(i32 %20, i32 %48, %struct.termios* %3)
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %49)
%51 = call i32 @close(i32 %20)
%52 = call i32 @exit(i32 0)
unreachable
}