#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ B9600;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int set_port_attr(int, int /*<<< orphan*/, int, char *, char, int, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  int len, ret;
  char buf[] = "hello heihei";
  fd = open("/dev/ttymxc1", O_RDWR | O_NOCTTY);
  if (fd < 0) {
    perror("open uart device error\n");
  }
  ret = set_port_attr(fd, B9600, 8, "1", 'N', 8, 100);
  if (ret < 0) {
    printf("set uart arrt faile \n");
    exit(-1);
  }
  len = write(fd, buf, sizeof(buf));
  if (len < 0) {
    printf("write data error!\n");
    return -1;
  }
  printf("The output data :  %s \n", buf);
  memset(buf, 0, sizeof(buf));
  len = read(fd, buf, sizeof(buf));
  printf("len :  %d \n", len);
  if (len < 0) {
    printf("read data error!\n");
    return -1;
  }
  printf("The read data : %s \n", buf);
  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)
mov    0xe63(%rip),%rax
mov    %rax,-0x29(%rbp)
mov    0xe61(%rip),%eax
mov    %eax,-0x21(%rbp)
mov    0xe5c(%rip),%al
mov    %al,-0x1d(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    0x2e0d(%rip),%rax
or     (%rax),%esi
lea    0xe41(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ea <main+0x6a>
lea    0xe35(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%r9d
lea    0xe15(%rip),%rcx
mov    $0x4e,%r8d
mov    %r9d,%edx
movl   $0x64,(%rsp)
call   1070 <set_port_attr@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    123d <main+0xbd>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0xd,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1275 <main+0xf5>
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <main+0x179>
lea    -0x29(%rbp),%rsi
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x29(%rbp),%rdi
xor    %esi,%esi
mov    $0xd,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0xd,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jge    12e0 <main+0x160>
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f9 <main+0x179>
lea    -0x29(%rbp),%rsi
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
movabs $0x69656869656820,%rax
mov    %rax,0x15(%rsp)
movabs $0x6568206f6c6c6568,%rax
20 68 65
mov    %rax,0x10(%rsp)
mov    0x2e2b(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12b3 <main+0x123>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
movl   $0x64,(%rsp)
lea    0xe23(%rip),%rcx
mov    %ebx,%edi
mov    $0x8,%edx
mov    $0x4e,%r8d
mov    $0x8,%r9d
call   1080 <set_port_attr@plt>
test   %eax,%eax
js     12c4 <main+0x134>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xd,%edx
call   1040 <write@plt>
test   %eax,%eax
js     128c <main+0xfc>
lea    0xe01(%rip),%rdi
xor    %r14d,%r14d
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xd,%edx
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xd,%edx
call   1070 <read@plt>
mov    %eax,%ebx
lea    0xde0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
js     1295 <main+0x105>
lea    0xdd8(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12a7 <main+0x117>
lea    0xde9(%rip),%rdi
jmp    129c <main+0x10c>
lea    0xdcf(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd60(%rip),%rdi
call   10a0 <perror@plt>
jmp    11de <main+0x4e>
lea    0xdc3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@__const.main.buf = [13 x i8] c"hello heihei\00"
@.str = [13 x i8] c"/dev/ttymxc1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [24 x i8] c"open uart device error\0A\00"
@B9600 = global i32 0
@.str.2 = [2 x i8] c"1\00"
@.str.3 = [22 x i8] c"set uart arrt faile \0A\00"
@.str.4 = [19 x i8] c"write data error!\0A\00"
@.str.5 = [24 x i8] c"The output data : %s \0A\00"
@.str.6 = [12 x i8] c"len : %d \0A\00"
@.str.7 = [18 x i8] c"read data error!\0A\00"
@.str.8 = [21 x i8] c"The read data : %s \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 [13 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [13 x i8]* %9 to i8*
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = load i32, i32* %6
%21 = load i32, i32* @B9600
%22 = call i32 @set_port_attr(i32 %20, i32 %21, i32 8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8 signext 78, i32 8, i32 100)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %19
%29 = load i32, i32* %6
%30 = getelementptr inbounds [13 x i8], [13 x i8]* %9, i64 0, i64 0
%31 = call i32 @write(i32 %29, i8* %30, i32 13)
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
36: ; preds = %28
%37 = getelementptr inbounds [13 x i8], [13 x i8]* %9, i64 0, i64 0
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %37)
%39 = getelementptr inbounds [13 x i8], [13 x i8]* %9, i64 0, i64 0
%40 = call i32 @memset(i8* %39, i32 0, i32 13)
%41 = load i32, i32* %6
%42 = getelementptr inbounds [13 x i8], [13 x i8]* %9, i64 0, i64 0
%43 = call i32 @read(i32 %41, i8* %42, i32 13)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %36
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
50: ; preds = %36
%51 = getelementptr inbounds [13 x i8], [13 x i8]* %9, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* %51)
store i32 0, i32* %3
br label %53
53: ; preds = %50, %48, %34
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
@__const.main.buf = [13 x i8] c"hello heihei\00"
@.str = [13 x i8] c"/dev/ttymxc1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [24 x i8] c"open uart device error\0A\00"
@B9600 = global i32 0
@.str.2 = [2 x i8] c"1\00"
@.str.5 = [24 x i8] c"The output data : %s \0A\00"
@.str.6 = [12 x i8] c"len : %d \0A\00"
@.str.8 = [21 x i8] c"The read data : %s \0A\00"
@str = [17 x i8] c"read data error!\00"
@str.9 = [18 x i8] c"write data error!\00"
@str.10 = [21 x i8] c"set uart arrt faile \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [13 x i8]
%4 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOCTTY
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = load i32, i32* @B9600
%14 = tail call i32 @set_port_attr(i32 %8, i32 %13, i32 8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8 signext 78, i32 8, i32 100)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.10, i64 0, i64 0))
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %12
%20 = call i32 @write(i32 %8, i8* %4, i32 13)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.9, i64 0, i64 0))
br label %34
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %4)
%26 = call i32 @memset(i8* %4, i32 0, i32 13)
%27 = call i32 @read(i32 %8, i8* %4, i32 13)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %27)
%29 = icmp slt i32 %27, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %34
32: ; preds = %24
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i8* %4)
br label %34
34: ; preds = %32, %30, %22
%35 = phi i32 [ -1, %22 ], [ -1, %30 ], [ 0, %32 ]
ret i32 %35
}