#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 */

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ uart_close(int);
int /*<<< orphan*/ uart_open(int, int *);
int /*<<< orphan*/ uart_set_baudrate(int, unsigned int);
int /*<<< orphan*/ uart_set_flowcontrol(int, int, int);
int /*<<< orphan*/ uart_set_mode(int, int, char *, int);
int /*<<< orphan*/ uart_write(int, char *, int);
int iotbus_uart_test() {
  int fd;
  int ret;
  char c = 'D';
  int port = 3;
  unsigned int baud = 115200;
  int bytesize = 8;
  char *parity = "none";
  int stopbits = 1;
  int xonxoff = 1;
  int rtscts = 0;
  printf("<<< iotbus_uart_test >>>\n");
  printf("port path: /dev/ttySAC%d (being used at artik)\n", port);
  printf("set baudrate: %d\n", baud);
  printf("set mode: bytesize(%d), parity(%s), stopbits(%d)\n", bytesize, parity,
         stopbits);
  printf("set flow control: xonxoff(%d), rtscts(%d)\n", xonxoff, rtscts);
  uart_open(port, &fd);
  uart_set_baudrate(fd, baud);
  uart_set_mode(fd, bytesize, parity, stopbits);
  uart_set_flowcontrol(fd, xonxoff, rtscts);
  while (c != 'q') {
    ret = read(STDIN_FILENO, &c, 1);
    if (ret > 0) {
      uart_write(fd, &c, 1);
    }
  }
  uart_close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<iotbus_uart_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movb   $0x44,-0x9(%rbp)
movl   $0x3,-0x10(%rbp)
movl   $0x1c200,-0x14(%rbp)
movl   $0x8,-0x18(%rbp)
lea    0xe58(%rip),%rax
mov    %rax,-0x20(%rbp)
movl   $0x1,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x4(%rbp),%rsi
call   10a0 <uart_open@plt>
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <uart_set_baudrate@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1030 <uart_set_mode@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1060 <uart_set_flowcontrol@plt>
movsbl -0x9(%rbp),%eax
cmp    $0x71,%eax
je     129b <iotbus_uart_test+0x11b>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1296 <iotbus_uart_test+0x116>
mov    -0x4(%rbp),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1080 <uart_write@plt>
jmp    1254 <iotbus_uart_test+0xd4>
mov    -0x4(%rbp),%edi
call   1090 <uart_close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iotbus_uart_test>:
push   %r14
push   %rbx
push   %rax
movb   $0x44,0x3(%rsp)
lea    0xf04(%rip),%rdi
call   1030 <puts@plt>
lea    0xe59(%rip),%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe76(%rip),%rdi
mov    $0x1c200,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe75(%rip),%rdi
lea    0xe27(%rip),%rbx
mov    $0x8,%esi
mov    %rbx,%rdx
mov    $0x1,%ecx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe85(%rip),%rdi
mov    $0x1,%esi
xor    %edx,%edx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x4(%rsp),%rsi
mov    $0x3,%edi
call   10b0 <uart_open@plt>
mov    0x4(%rsp),%edi
mov    $0x1c200,%esi
call   1050 <uart_set_baudrate@plt>
mov    0x4(%rsp),%edi
mov    $0x8,%esi
mov    %rbx,%rdx
mov    $0x1,%ecx
call   1040 <uart_set_mode@plt>
mov    0x4(%rsp),%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <uart_set_flowcontrol@plt>
mov    0x2d94(%rip),%r14
lea    0x3(%rsp),%rbx
jmp    1267 <iotbus_uart_test+0xd7>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmpb   $0x71,0x3(%rsp)
je     128e <iotbus_uart_test+0xfe>
mov    (%r14),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1260 <iotbus_uart_test+0xd0>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1090 <uart_write@plt>
jmp    1260 <iotbus_uart_test+0xd0>
mov    0x4(%rsp),%edi
call   10a0 <uart_close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [5 x i8] c"none\00"
@.str.1 = [26 x i8] c"<<< iotbus_uart_test >>>\0A\00"
@.str.2 = [48 x i8] c"port path: /dev/ttySAC%d (being used at artik)\0A\00"
@.str.3 = [18 x i8] c"set baudrate: %d\0A\00"
@.str.4 = [50 x i8] c"set mode: bytesize(%d), parity(%s), stopbits(%d)\0A\00"
@.str.5 = [43 x i8] c"set flow control: xonxoff(%d), rtscts(%d)\0A\00"
@STDIN_FILENO = global i32 0
define i32 @iotbus_uart_test() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8 68, i8* %3
store i32 3, i32* %4
store i32 115200, i32* %5
store i32 8, i32* %6
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %7
store i32 1, i32* %8
store i32 1, i32* %9
store i32 0, i32* %10
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %5
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %6
%17 = load i8*, i8** %7
%18 = load i32, i32* %8
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0), i32 %16, i8* %17, i32 %18)
%20 = load i32, i32* %9
%21 = load i32, i32* %10
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %20, i32 %21)
%23 = load i32, i32* %4
%24 = call i32 @uart_open(i32 %23, i32* %1)
%25 = load i32, i32* %1
%26 = load i32, i32* %5
%27 = call i32 @uart_set_baudrate(i32 %25, i32 %26)
%28 = load i32, i32* %1
%29 = load i32, i32* %6
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = call i32 @uart_set_mode(i32 %28, i32 %29, i8* %30, i32 %31)
%33 = load i32, i32* %1
%34 = load i32, i32* %9
%35 = load i32, i32* %10
%36 = call i32 @uart_set_flowcontrol(i32 %33, i32 %34, i32 %35)
br label %37
37: ; preds = %49, %0
%38 = load i8, i8* %3
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 113
br i1 %40, label %41, label %50
41: ; preds = %37
%42 = load i32, i32* @STDIN_FILENO
%43 = call i32 @read(i32 %42, i8* %3, i32 1)
store i32 %43, i32* %2
%44 = load i32, i32* %2
%45 = icmp sgt i32 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32, i32* %1
%48 = call i32 @uart_write(i32 %47, i8* %3, i32 1)
br label %49
49: ; preds = %46, %41
br label %37
50: ; preds = %37
%51 = load i32, i32* %1
%52 = call i32 @uart_close(i32 %51)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"none\00"
@.str.2 = [48 x i8] c"port path: /dev/ttySAC%d (being used at artik)\0A\00"
@.str.3 = [18 x i8] c"set baudrate: %d\0A\00"
@.str.4 = [50 x i8] c"set mode: bytesize(%d), parity(%s), stopbits(%d)\0A\00"
@.str.5 = [43 x i8] c"set flow control: xonxoff(%d), rtscts(%d)\0A\00"
@STDIN_FILENO = global i32 0
@str = [25 x i8] c"<<< iotbus_uart_test >>>\00"
define i32 @iotbus_uart_test() {
%1 = alloca i32
%2 = alloca i8
%3 = bitcast i32* %1 to i8*
store i8 68, i8* %2
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i32 3)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 115200)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0), i32 8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 0)
%9 = call i32 @uart_open(i32 3, i32* %1)
%10 = load i32, i32* %1
%11 = call i32 @uart_set_baudrate(i32 %10, i32 115200)
%12 = load i32, i32* %1
%13 = call i32 @uart_set_mode(i32 %12, i32 8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 1)
%14 = load i32, i32* %1
%15 = call i32 @uart_set_flowcontrol(i32 %14, i32 1, i32 0)
br label %16
16: ; preds = %0, %23
%17 = load i32, i32* @STDIN_FILENO
%18 = call i32 @read(i32 %17, i8* %2, i32 1)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %1
%22 = call i32 @uart_write(i32 %21, i8* %2, i32 1)
br label %23
23: ; preds = %20, %16
%24 = load i8, i8* %2
%25 = icmp eq i8 %24, 113
br i1 %25, label %26, label %16
26: ; preds = %23
%27 = load i32, i32* %1
%28 = call i32 @uart_close(i32 %27)
ret i32 0
}