#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_cflag;
};
typedef int speed_t;
typedef int /*<<< orphan*/ newtio;

/* Variables and functions */
int B115200;
int CLOCAL;
int CREAD;
int CS8;
int CSIZE;
int CSTOPB;
int O_NOCTTY;
int O_RDONLY;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
int cfgetospeed(struct termios *);
int /*<<< orphan*/ cfmakeraw(struct termios *);
int cfsetispeed(struct termios *, int);
int cfsetospeed(struct termios *, int);
int errno;
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int tcflush(int, int /*<<< orphan*/);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_mtig() {
  int fd, ret;
  struct termios newtio;
  printf("call open ... ");
  fd = open("/dev/cu.usbserial-ftE01UU0", O_RDONLY | O_NOCTTY);
  if (fd < 0)
    return 0;
  printf("fd is %d\n", fd);
  memset(&newtio, 0, sizeof(newtio));
  ret = tcgetattr(fd, &newtio);
  speed_t s = cfgetospeed(&newtio);
  newtio.c_cflag |= CLOCAL | CREAD;
  cfmakeraw(&newtio);
  ret = cfsetispeed(&newtio, B115200);
  ret = cfsetospeed(&newtio, B115200);
  printf("setospeed(%d) return %d, errno %d\n", B115200, ret, errno);
  newtio.c_cflag &= ~CSIZE;
  newtio.c_cflag |= CS8;
  newtio.c_cflag &= ~CSTOPB;
  s = cfgetospeed(&newtio);
  printf("set speed to %d\n", s);
  ret = tcflush(fd, TCIOFLUSH);
  if (ret != 0) {
    fd = -1;
    return fd;
  }
  ret = tcsetattr(fd, TCSANOW, &newtio);
  if (ret != 0) {
    fd = -2;
    return fd;
  }
  tcgetattr(fd, &newtio);
  s = cfgetospeed(&newtio);
  printf("now speed is %d\n", s);
  return fd;
}


x86 assembly (clang, -O0):
<open_mtig>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
mov    0x2df2(%rip),%rax
or     (%rax),%esi
lea    0xe40(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11ed <open_mtig+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    137a <open_mtig+0x1da>
mov    -0x8(%rbp),%esi
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1090 <tcgetattr@plt>
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rdi
call   1080 <cfgetospeed@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    0x2d9f(%rip),%rcx
or     (%rcx),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
call   1070 <cfmakeraw@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   10c0 <cfsetispeed@plt>
mov    %eax,-0xc(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    %eax,-0xc(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2d29(%rip),%rax
mov    (%rax),%ecx
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d20(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2cd1(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
call   1080 <cfgetospeed@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2cb7(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     1317 <open_mtig+0x177>
movl   $0xffffffff,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    137a <open_mtig+0x1da>
mov    -0x8(%rbp),%edi
mov    0x2cbf(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   10a0 <tcsetattr@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     134b <open_mtig+0x1ab>
movl   $0xfffffffe,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    137a <open_mtig+0x1da>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1090 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
call   1080 <cfgetospeed@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xcfb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_mtig>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe51(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e29(%rip),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe3e(%rip),%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     1314 <open_mtig+0x174>
mov    %eax,%r14d
lea    0xe42(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1090 <tcgetattr@plt>
mov    %rbx,%rdi
call   1080 <cfgetospeed@plt>
mov    0x2d69(%rip),%rax
mov    0x2db2(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
or     %ecx,0x8(%rsp)
mov    %rbx,%rdi
call   1070 <cfmakeraw@plt>
mov    0x2d63(%rip),%rbp
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   10c0 <cfsetispeed@plt>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1030 <cfsetospeed@plt>
mov    0x0(%rbp),%esi
mov    0x2d53(%rip),%rcx
mov    (%rcx),%ecx
lea    0xdce(%rip),%rdi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2d4b(%rip),%rcx
or     (%rcx),%eax
mov    0x2d02(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
and    %eax,%ecx
mov    %ecx,0x8(%rsp)
mov    %rbx,%rdi
call   1080 <cfgetospeed@plt>
lea    0xdb0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <tcflush@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    1314 <open_mtig+0x174>
mov    0x2d0f(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   10a0 <tcsetattr@plt>
mov    $0xfffffffe,%ebx
test   %eax,%eax
jne    1314 <open_mtig+0x174>
lea    0x8(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1090 <tcgetattr@plt>
mov    %rbx,%rdi
call   1080 <cfgetospeed@plt>
lea    0xd60(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [15 x i8] c"call open ... \00"
@.str.1 = [27 x i8] c"/dev/cu.usbserial-ftE01UU0\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.2 = [10 x i8] c"fd is %d\0A\00"
@CLOCAL = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@.str.3 = [35 x i8] c"setospeed(%d) return %d, errno %d\0A\00"
@errno = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CSTOPB = global i32 0
@.str.4 = [17 x i8] c"set speed to %d\0A\00"
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.5 = [17 x i8] c"now speed is %d\0A\00"
define i32 @open_mtig() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
%5 = alloca i32
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NOCTTY
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %0
store i32 0, i32* %1
br label %75
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%17 = call i32 @memset(%struct.termios* %4, i32 0, i32 4)
%18 = load i32, i32* %2
%19 = call i32 @tcgetattr(i32 %18, %struct.termios* %4)
store i32 %19, i32* %3
%20 = call i32 @cfgetospeed(%struct.termios* %4)
store i32 %20, i32* %5
%21 = load i32, i32* @CLOCAL
%22 = load i32, i32* @CREAD
%23 = or i32 %21, %22
%24 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = or i32 %25, %23
store i32 %26, i32* %24
%27 = call i32 @cfmakeraw(%struct.termios* %4)
%28 = load i32, i32* @B115200
%29 = call i32 @cfsetispeed(%struct.termios* %4, i32 %28)
store i32 %29, i32* %3
%30 = load i32, i32* @B115200
%31 = call i32 @cfsetospeed(%struct.termios* %4, i32 %30)
store i32 %31, i32* %3
%32 = load i32, i32* @B115200
%33 = load i32, i32* %3
%34 = load i32, i32* @errno
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %32, i32 %33, i32 %34)
%36 = load i32, i32* @CSIZE
%37 = xor i32 %36, -1
%38 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%39 = load i32, i32* %38
%40 = and i32 %39, %37
store i32 %40, i32* %38
%41 = load i32, i32* @CS8
%42 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%43 = load i32, i32* %42
%44 = or i32 %43, %41
store i32 %44, i32* %42
%45 = load i32, i32* @CSTOPB
%46 = xor i32 %45, -1
%47 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%48 = load i32, i32* %47
%49 = and i32 %48, %46
store i32 %49, i32* %47
%50 = call i32 @cfgetospeed(%struct.termios* %4)
store i32 %50, i32* %5
%51 = load i32, i32* %5
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %2
%54 = load i32, i32* @TCIOFLUSH
%55 = call i32 @tcflush(i32 %53, i32 %54)
store i32 %55, i32* %3
%56 = load i32, i32* %3
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %14
store i32 -1, i32* %2
%59 = load i32, i32* %2
store i32 %59, i32* %1
br label %75
60: ; preds = %14
%61 = load i32, i32* %2
%62 = load i32, i32* @TCSANOW
%63 = call i32 @tcsetattr(i32 %61, i32 %62, %struct.termios* %4)
store i32 %63, i32* %3
%64 = load i32, i32* %3
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %60
store i32 -2, i32* %2
%67 = load i32, i32* %2
store i32 %67, i32* %1
br label %75
68: ; preds = %60
%69 = load i32, i32* %2
%70 = call i32 @tcgetattr(i32 %69, %struct.termios* %4)
%71 = call i32 @cfgetospeed(%struct.termios* %4)
store i32 %71, i32* %5
%72 = load i32, i32* %5
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %72)
%74 = load i32, i32* %2
store i32 %74, i32* %1
br label %75
75: ; preds = %68, %66, %58, %13
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [15 x i8] c"call open ... \00"
@.str.1 = [27 x i8] c"/dev/cu.usbserial-ftE01UU0\00"
@O_RDONLY = global i32 0
@O_NOCTTY = global i32 0
@.str.2 = [10 x i8] c"fd is %d\0A\00"
@CLOCAL = global i32 0
@CREAD = global i32 0
@B115200 = global i32 0
@.str.3 = [35 x i8] c"setospeed(%d) return %d, errno %d\0A\00"
@errno = global i32 0
@CSIZE = global i32 0
@CS8 = global i32 0
@CSTOPB = global i32 0
@.str.4 = [17 x i8] c"set speed to %d\0A\00"
@TCIOFLUSH = global i32 0
@TCSANOW = global i32 0
@.str.5 = [17 x i8] c"now speed is %d\0A\00"
define i32 @open_mtig() {
%1 = alloca %struct.termios
%2 = bitcast %struct.termios* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @O_RDONLY
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %50, label %9
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %7)
%11 = call i32 @memset(%struct.termios* %1, i32 0, i32 4)
%12 = call i32 @tcgetattr(i32 %7, %struct.termios* %1)
%13 = call i32 @cfgetospeed(%struct.termios* %1)
%14 = load i32, i32* @CLOCAL
%15 = load i32, i32* @CREAD
%16 = or i32 %15, %14
%17 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%18 = load i32, i32* %17
%19 = or i32 %16, %18
store i32 %19, i32* %17
%20 = call i32 @cfmakeraw(%struct.termios* %1)
%21 = load i32, i32* @B115200
%22 = call i32 @cfsetispeed(%struct.termios* %1, i32 %21)
%23 = load i32, i32* @B115200
%24 = call i32 @cfsetospeed(%struct.termios* %1, i32 %23)
%25 = load i32, i32* @B115200
%26 = load i32, i32* @errno
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 %24, i32 %26)
%28 = load i32, i32* @CSIZE
%29 = xor i32 %28, -1
%30 = load i32, i32* %17
%31 = and i32 %30, %29
%32 = load i32, i32* @CS8
%33 = or i32 %32, %31
%34 = load i32, i32* @CSTOPB
%35 = xor i32 %34, -1
%36 = and i32 %33, %35
store i32 %36, i32* %17
%37 = call i32 @cfgetospeed(%struct.termios* %1)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = load i32, i32* @TCIOFLUSH
%40 = call i32 @tcflush(i32 %7, i32 %39)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %9
%43 = load i32, i32* @TCSANOW
%44 = call i32 @tcsetattr(i32 %7, i32 %43, %struct.termios* %1)
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = call i32 @tcgetattr(i32 %7, %struct.termios* %1)
%48 = call i32 @cfgetospeed(%struct.termios* %1)
%49 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %46, %9, %42, %0
%51 = phi i32 [ 0, %0 ], [ %7, %46 ], [ -1, %9 ], [ -2, %42 ]
ret i32 %51
}