#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*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int set_port_attr(int, int /*<<< orphan*/, int, char *, char,
                  int /*<<< orphan*/, int /*<<< orphan*/);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  int len, ret;
  char buf[] = "hello";
  fd = open("/dev/ttymxc1", O_RDWR | O_NOCTTY);
  if (fd < 0) {
    perror("Open device error\n");
  }
  printf("Device opened : %s\n", "/dev/ttymxc1");
  ret = set_port_attr(fd, B9600, 8, "1", 'N', 0, 0);
  if (ret < 0) {
    printf("Set attribute failed \n");
    exit(-1);
  }
  puts("Device attribute : (B9600 8n1)  (vtime 0)  (vmin 0)\n");
  len = write(fd, buf, sizeof(buf));
  if (len < 0) {
    printf("Write data error!\n");
    return -1;
  }
  printf("Output data : %s \n", buf);
  memset(buf, 0, sizeof(buf));
  len = read(fd, buf, sizeof(buf));
  if (len < 0) {
    printf("Read data error!\n");
    return -1;
  }
  printf("Read length : %d \n", len);
  printf("Read data : %s \n", buf);
  close(fd);
  printf("Device closed  : %s\n", "/dev/ttymxc1");
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe44(%rip),%eax
mov    %eax,-0x22(%rbp)
mov    0xe3e(%rip),%ax
mov    %ax,-0x1e(%rbp)
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
lea    0xe23(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1201 <main+0x61>
lea    0xe17(%rip),%rdi
call   10b0 <perror@plt>
lea    0xe1e(%rip),%rdi
lea    0xdf7(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    $0x8,%edx
lea    0xde3(%rip),%rcx
mov    $0x4e,%r8d
xor    %r9d,%r9d
movl   $0x0,(%rsp)
call   1090 <set_port_attr@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1268 <main+0xc8>
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    0xde2(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12ac <main+0x10c>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <main+0x1ad>
lea    -0x22(%rbp),%rsi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x22(%rbp),%rdi
xor    %esi,%esi
mov    $0x6,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x6,%edx
call   1080 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1306 <main+0x166>
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134d <main+0x1ad>
mov    -0x18(%rbp),%esi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x22(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    0xdaa(%rip),%rdi
lea    0xcc7(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movw   $0x6f,0xc(%rsp)
movl   $0x6c6c6568,0x8(%rsp)
6c
mov    0x2e2a(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe36(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12ec <main+0x14c>
lea    0xe40(%rip),%rdi
lea    0xe19(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
movl   $0x0,(%rsp)
lea    0xe06(%rip),%rcx
mov    %ebx,%edi
mov    $0x8,%edx
mov    $0x4e,%r8d
xor    %r9d,%r9d
call   1090 <set_port_attr@plt>
test   %eax,%eax
js     12fd <main+0x15d>
lea    0xe0b(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x6,%edx
call   1040 <write@plt>
test   %eax,%eax
js     12c5 <main+0x125>
lea    0xe1f(%rip),%rdi
xor    %r14d,%r14d
lea    0x8(%rsp),%rbp
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x6,%edx
call   1060 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x6,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12ce <main+0x12e>
lea    0xdf7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdfa(%rip),%rdi
lea    0x8(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xdf1(%rip),%rdi
lea    0xd4a(%rip),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12e0 <main+0x140>
lea    0xe00(%rip),%rdi
jmp    12d5 <main+0x135>
lea    0xde6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd20(%rip),%rdi
call   10b0 <perror@plt>
jmp    11df <main+0x3f>
lea    0xdda(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@__const.main.buf = [6 x i8] c"hello\00"
@.str = [13 x i8] c"/dev/ttymxc1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [19 x i8] c"Open device error\0A\00"
@.str.2 = [20 x i8] c"Device opened : %s\0A\00"
@B9600 = global i32 0
@.str.3 = [2 x i8] c"1\00"
@.str.4 = [23 x i8] c"Set attribute failed \0A\00"
@.str.5 = [53 x i8] c"Device attribute : (B9600 8n1) (vtime 0) (vmin 0)\0A\00"
@.str.6 = [19 x i8] c"Write data error!\0A\00"
@.str.7 = [19 x i8] c"Output data : %s \0A\00"
@.str.8 = [18 x i8] c"Read data error!\0A\00"
@.str.9 = [19 x i8] c"Read length : %d \0A\00"
@.str.10 = [17 x i8] c"Read data : %s \0A\00"
@.str.11 = [21 x i8] c"Device closed : %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 [6 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [6 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 ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%21 = load i32, i32* %6
%22 = load i32, i32* @B9600
%23 = call i32 @set_port_attr(i32 %21, i32 %22, i32 8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8 signext 78, i32 0, i32 0)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %19
%30 = call i32 @puts(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* %6
%32 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%33 = call i32 @write(i32 %31, i8* %32, i32 6)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
38: ; preds = %29
%39 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %39)
%41 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%42 = call i32 @memset(i8* %41, i32 0, i32 6)
%43 = load i32, i32* %6
%44 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%45 = call i32 @read(i32 %43, i8* %44, i32 6)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %38
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %3
br label %58
50: ; preds = %38
%51 = load i32, i32* %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i32 %51)
%53 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %58
58: ; preds = %50, %48, %36
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@__const.main.buf = [6 x i8] c"hello\00"
@.str = [13 x i8] c"/dev/ttymxc1\00"
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@.str.1 = [19 x i8] c"Open device error\0A\00"
@.str.2 = [20 x i8] c"Device opened : %s\0A\00"
@B9600 = global i32 0
@.str.3 = [2 x i8] c"1\00"
@.str.5 = [53 x i8] c"Device attribute : (B9600 8n1) (vtime 0) (vmin 0)\0A\00"
@.str.7 = [19 x i8] c"Output data : %s \0A\00"
@.str.9 = [19 x i8] c"Read length : %d \0A\00"
@.str.10 = [17 x i8] c"Read data : %s \0A\00"
@.str.11 = [21 x i8] c"Device closed : %s\0A\00"
@str = [17 x i8] c"Read data error!\00"
@str.12 = [18 x i8] c"Write data error!\00"
@str.13 = [22 x i8] c"Set attribute failed \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [6 x i8]
%4 = getelementptr inbounds [6 x i8], [6 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 ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @B9600
%15 = tail call i32 @set_port_attr(i32 %8, i32 %14, i32 8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8 signext 78, i32 0, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.13, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %12
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 @write(i32 %8, i8* %4, i32 6)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.12, i64 0, i64 0))
br label %38
26: ; preds = %20
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %4)
%28 = call i32 @memset(i8* %4, i32 0, i32 6)
%29 = call i32 @read(i32 %8, i8* %4, i32 6)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %38
33: ; preds = %26
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.9, i64 0, i64 0), i32 %29)
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0), i8* %4)
%36 = call i32 @close(i32 %8)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.11, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %33, %31, %24
%39 = phi i32 [ -1, %24 ], [ -1, %31 ], [ 0, %33 ]
ret i32 %39
}