#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_lflag;
};

/* Variables and functions */
int ECHO;
int ICANON;
int ISIG;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ TIOCMBIC;
int /*<<< orphan*/ TIOCMSET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, unsigned int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  int uart_file1;
  unsigned int line_val;
  char buf[5];
  struct termios mxc, old;
  int retval;
  printf("Test: MXC UART!\n");
  printf("Usage: mxc_uart_test <UART device name, opens UART2 if no dev name "
         "is specified>\n");
  if (argc == 1) {
    if ((uart_file1 = open("/dev/ttymxc/1", O_RDWR)) == -1) {
      printf("Error opening UART 2\n");
      exit(1);
    } else {
      printf("Test: UART 2 opened\n");
    }
  } else {
    if ((uart_file1 = open(*++argv, O_RDWR)) == -1) {
      printf("Error opening %s\n", *argv);
      exit(1);
    } else {
      printf("%s opened\n", *argv);
    }
  }
  tcgetattr(uart_file1, &old);
  mxc = old;
  mxc.c_lflag &= ~(ICANON | ECHO | ISIG);
  retval = tcsetattr(uart_file1, TCSANOW, &mxc);
  printf("Attributes set\n");
  line_val = 0x8000;
  ioctl(uart_file1, TIOCMSET, &line_val);
  printf("Test: IOCTL Set\n");
  write(uart_file1, "Test\0", 5);
  printf("Data Written= Test\n");
  read(uart_file1, buf, 5);
  printf("Data Read back= %s\n", buf);
  sleep(2);
  ioctl(uart_file1, TIOCMBIC, &line_val);
  retval = tcsetattr(uart_file1, TCSAFLUSH, &old);
  close(uart_file1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jne    1228 <main+0x88>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
lea    0xe77(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1215 <main+0x75>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128e <main+0xee>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
mov    0x8(%rax),%rdi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1274 <main+0xd4>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    128e <main+0xee>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <tcgetattr@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d01(%rip),%rax
mov    (%rax),%eax
mov    0x2d18(%rip),%rcx
or     (%rcx),%eax
mov    0x2d07(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d02(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    %eax,-0x34(%rbp)
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x8000,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cdd(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xde4(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x5,%edx
call   1070 <read@plt>
lea    -0x1d(%rbp),%rsi
lea    0xd9c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2,%edi
call   10c0 <sleep@plt>
mov    -0x14(%rbp),%edi
mov    0x2c43(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x14(%rbp),%edi
mov    0x2c5e(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1090 <tcsetattr@plt>
mov    %eax,-0x34(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x1,%ebx
jne    120b <main+0x5b>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
lea    0xe13(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     1341 <main+0x191>
mov    %eax,%ebx
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
jmp    123a <main+0x8a>
mov    0x8(%r14),%rdi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    0x8(%r14),%rsi
cmp    $0xffffffff,%eax
je     1357 <main+0x1a7>
mov    %eax,%ebx
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <tcgetattr@plt>
mov    0x2d5a(%rip),%rax
mov    0x2d73(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d60(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   10a0 <tcsetattr@plt>
lea    0xe19(%rip),%rdi
call   1030 <puts@plt>
movl   $0x8000,0xc(%rsp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%r15
mov    %ebx,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
lea    0xe3c(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1040 <write@plt>
lea    0xded(%rip),%rdi
call   1030 <puts@plt>
lea    0x13(%rsp),%r12
mov    %ebx,%edi
mov    %r12,%rsi
mov    $0x5,%edx
call   1080 <read@plt>
lea    0xd39(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x2,%edi
call   10d0 <sleep@plt>
mov    0x2ca3(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r15,%rdx
call   1060 <ioctl@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   10a0 <tcsetattr@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd9e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xcb0(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [17 x i8] c"Test: MXC UART!\0A\00"
@.str.1 = [82 x i8] c"Usage: mxc_uart_test <UART device name, opens UART2 if no dev name is specified>\0A\00"
@.str.2 = [14 x i8] c"/dev/ttymxc/1\00"
@O_RDWR = global i32 0
@.str.3 = [22 x i8] c"Error opening UART 2\0A\00"
@.str.4 = [21 x i8] c"Test: UART 2 opened\0A\00"
@.str.5 = [18 x i8] c"Error opening %s\0A\00"
@.str.6 = [11 x i8] c"%s opened\0A\00"
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSANOW = global i32 0
@.str.7 = [16 x i8] c"Attributes set\0A\00"
@TIOCMSET = global i32 0
@.str.8 = [17 x i8] c"Test: IOCTL Set\0A\00"
@.str.9 = [6 x i8] c"Test\00\00"
@.str.10 = [20 x i8] c"Data Written= Test\0A\00"
@.str.11 = [20 x i8] c"Data Read back= %s\0A\00"
@TIOCMBIC = global i32 0
@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 [5 x i8]
%9 = alloca %struct.termios
%10 = alloca %struct.termios
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([82 x i8], [82 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %26
16: ; preds = %2
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %17)
store i32 %18, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %25
25: ; preds = %23
br label %43
26: ; preds = %2
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i32 1
store i8** %28, i8*** %5
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i8**, i8*** %5
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %26
%39 = load i8**, i8*** %5
%40 = load i8*, i8** %39
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %38
br label %43
43: ; preds = %42, %25
%44 = load i32, i32* %6
%45 = call i32 @tcgetattr(i32 %44, %struct.termios* %10)
%46 = bitcast %struct.termios* %9 to i8*
%47 = bitcast %struct.termios* %10 to i8*
%48 = load i32, i32* @ICANON
%49 = load i32, i32* @ECHO
%50 = or i32 %48, %49
%51 = load i32, i32* @ISIG
%52 = or i32 %50, %51
%53 = xor i32 %52, -1
%54 = getelementptr inbounds %struct.termios, %struct.termios* %9, i32 0, i32 0
%55 = load i32, i32* %54
%56 = and i32 %55, %53
store i32 %56, i32* %54
%57 = load i32, i32* %6
%58 = load i32, i32* @TCSANOW
%59 = call i32 @tcsetattr(i32 %57, i32 %58, %struct.termios* %9)
store i32 %59, i32* %11
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0))
store i32 32768, i32* %7
%61 = load i32, i32* %6
%62 = load i32, i32* @TIOCMSET
%63 = call i32 @ioctl(i32 %61, i32 %62, i32* %7)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.8, i64 0, i64 0))
%65 = load i32, i32* %6
%66 = call i32 @write(i32 %65, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32 5)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.10, i64 0, i64 0))
%68 = load i32, i32* %6
%69 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 5)
%71 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.11, i64 0, i64 0), i8* %71)
%73 = call i32 @sleep(i32 2)
%74 = load i32, i32* %6
%75 = load i32, i32* @TIOCMBIC
%76 = call i32 @ioctl(i32 %74, i32 %75, i32* %7)
%77 = load i32, i32* %6
%78 = load i32, i32* @TCSAFLUSH
%79 = call i32 @tcsetattr(i32 %77, i32 %78, %struct.termios* %10)
store i32 %79, i32* %11
%80 = load i32, i32* %6
%81 = call i32 @close(i32 %80)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str.2 = [14 x i8] c"/dev/ttymxc/1\00"
@O_RDWR = global i32 0
@.str.5 = [18 x i8] c"Error opening %s\0A\00"
@.str.6 = [11 x i8] c"%s opened\0A\00"
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSANOW = global i32 0
@TIOCMSET = global i32 0
@.str.9 = [6 x i8] c"Test\00\00"
@.str.11 = [20 x i8] c"Data Read back= %s\0A\00"
@TIOCMBIC = global i32 0
@TCSAFLUSH = global i32 0
@str = [16 x i8] c"Test: MXC UART!\00"
@str.12 = [81 x i8] c"Usage: mxc_uart_test <UART device name, opens UART2 if no dev name is specified>\00"
@str.13 = [15 x i8] c"Attributes set\00"
@str.14 = [16 x i8] c"Test: IOCTL Set\00"
@str.15 = [19 x i8] c"Data Written= Test\00"
@str.16 = [20 x i8] c"Test: UART 2 opened\00"
@str.17 = [21 x i8] c"Error opening UART 2\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [5 x i8]
%5 = alloca %struct.termios
%6 = alloca %struct.termios
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.termios* %5 to i8*
%10 = bitcast %struct.termios* %6 to i8*
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([81 x i8], [81 x i8]* @str.12, i64 0, i64 0))
%13 = icmp eq i32 %0, 1
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = load i32, i32* @O_RDWR
%16 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.17, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.16, i64 0, i64 0))
br label %35
23: ; preds = %2
%24 = getelementptr inbounds i8*, i8** %1, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 @open(i8* %25, i32 %26)
%28 = icmp eq i32 %27, -1
%29 = load i8*, i8** %24
br i1 %28, label %30, label %33
30: ; preds = %23
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i8* %29)
%32 = tail call i32 @exit(i32 1)
unreachable
33: ; preds = %23
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %29)
br label %35
35: ; preds = %33, %21
%36 = phi i32 [ %16, %21 ], [ %27, %33 ]
%37 = call i32 @tcgetattr(i32 %36, %struct.termios* %6)
%38 = getelementptr inbounds %struct.termios, %struct.termios* %6, i64 0, i32 0
%39 = getelementptr inbounds %struct.termios, %struct.termios* %5, i64 0, i32 0
%40 = load i32, i32* %38
%41 = load i32, i32* @ICANON
%42 = load i32, i32* @ECHO
%43 = or i32 %42, %41
%44 = load i32, i32* @ISIG
%45 = or i32 %43, %44
%46 = xor i32 %45, -1
%47 = and i32 %40, %46
store i32 %47, i32* %39
%48 = load i32, i32* @TCSANOW
%49 = call i32 @tcsetattr(i32 %36, i32 %48, %struct.termios* %5)
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.13, i64 0, i64 0))
store i32 32768, i32* %3
%51 = load i32, i32* @TIOCMSET
%52 = call i32 @ioctl(i32 %36, i32 %51, i32* %3)
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.14, i64 0, i64 0))
%54 = call i32 @write(i32 %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0), i32 5)
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.15, i64 0, i64 0))
%56 = call i32 @read(i32 %36, i8* %8, i32 5)
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.11, i64 0, i64 0), i8* %8)
%58 = call i32 @sleep(i32 2)
%59 = load i32, i32* @TIOCMBIC
%60 = call i32 @ioctl(i32 %36, i32 %59, i32* %3)
%61 = load i32, i32* @TCSAFLUSH
%62 = call i32 @tcsetattr(i32 %36, i32 %61, %struct.termios* %6)
%63 = call i32 @close(i32 %36)
ret i32 0
}