#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 dummy;
};
struct serial_struct {
  int custom_divisor;
  int /*<<< orphan*/ flags;
};

/* Variables and functions */
int /*<<< orphan*/ ASYNC_SPD_CUST;
int /*<<< orphan*/ B38400;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
int /*<<< orphan*/ TIOCSSERIAL;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ cfsetispeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct serial_struct *);
int open(char const *, int);
int /*<<< orphan*/ printc_err(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int open_olimex_iso(const char *device) {
  int fd = open(device, O_RDWR | O_NOCTTY);
  struct termios attr;
  struct serial_struct serial_info;
  if (fd < 0)
    return -1;
  tcgetattr(fd, &attr);
  cfmakeraw(&attr);
  cfsetispeed(&attr, B38400);
  cfsetospeed(&attr, B38400);
  serial_info.flags = ASYNC_SPD_CUST;
  serial_info.custom_divisor = 120;
  if (ioctl(fd, TIOCSSERIAL, &serial_info) < 0) {
    printc_err("open_olimex_iso: can't do ioctl TIOCSSERIAL: %s\n",
               strerror(errno));
    return -1;
  }
  if (tcsetattr(fd, TCSAFLUSH, &attr) < 0)
    return -1;
  return fd;
}


x86 assembly (clang, -O0):
<open_olimex_iso>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e10(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d0 <open_olimex_iso+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <open_olimex_iso+0x104>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
lea    -0x18(%rbp),%rdi
call   1060 <cfmakeraw@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   10a0 <cfsetispeed@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdi
call   1030 <cfsetospeed@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x78,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <ioctl@plt>
cmp    $0x0,%rax
jge    1263 <open_olimex_iso+0xd3>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%esi
lea    0xdae(%rip),%rdi
call   1040 <printc_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <open_olimex_iso+0x104>
mov    -0x14(%rbp),%edi
mov    0x2d63(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
jge    128e <open_olimex_iso+0xfe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1294 <open_olimex_iso+0x104>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_olimex_iso>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e37(%rip),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1265 <open_olimex_iso+0xd5>
mov    %eax,%ebp
lea    0x8(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
call   1070 <tcgetattr@plt>
mov    %rbx,%rdi
call   1060 <cfmakeraw@plt>
mov    0x2dc9(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   10a0 <cfsetispeed@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1030 <cfsetospeed@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movl   $0x78,0x10(%rsp)
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
mov    %ebp,%edi
call   1050 <ioctl@plt>
test   %rax,%rax
js     1249 <open_olimex_iso+0xb9>
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1080 <tcsetattr@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovs  %eax,%ebp
mov    %ebp,%r14d
jmp    1265 <open_olimex_iso+0xd5>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xda2(%rip),%rdi
mov    %eax,%esi
call   1040 <printc_err@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.serial_struct = type { i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B38400 = global i32 0
@ASYNC_SPD_CUST = global i32 0
@TIOCSSERIAL = global i32 0
@.str = [49 x i8] c"open_olimex_iso: can't do ioctl TIOCSSERIAL: %s\0A\00"
@errno = global i32 0
@TCSAFLUSH = global i32 0
define i32 @open_olimex_iso(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.termios
%6 = alloca %struct.serial_struct
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %7, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i32 -1, i32* %2
br label %42
15: ; preds = %1
%16 = load i32, i32* %4
%17 = call i32 @tcgetattr(i32 %16, %struct.termios* %5)
%18 = call i32 @cfmakeraw(%struct.termios* %5)
%19 = load i32, i32* @B38400
%20 = call i32 @cfsetispeed(%struct.termios* %5, i32 %19)
%21 = load i32, i32* @B38400
%22 = call i32 @cfsetospeed(%struct.termios* %5, i32 %21)
%23 = load i32, i32* @ASYNC_SPD_CUST
%24 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %6, i32 0, i32 1
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %6, i32 0, i32 0
store i32 120, i32* %25
%26 = load i32, i32* %4
%27 = load i32, i32* @TIOCSSERIAL
%28 = call i64 @ioctl(i32 %26, i32 %27, %struct.serial_struct* %6)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %15
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @printc_err(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %32)
store i32 -1, i32* %2
br label %42
34: ; preds = %15
%35 = load i32, i32* %4
%36 = load i32, i32* @TCSAFLUSH
%37 = call i64 @tcsetattr(i32 %35, i32 %36, %struct.termios* %5)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 -1, i32* %2
br label %42
40: ; preds = %34
%41 = load i32, i32* %4
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %39, %30, %14
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.serial_struct = type { i32, i32 }
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@B38400 = global i32 0
@ASYNC_SPD_CUST = global i32 0
@TIOCSSERIAL = global i32 0
@.str = [49 x i8] c"open_olimex_iso: can't do ioctl TIOCSSERIAL: %s\0A\00"
@errno = global i32 0
@TCSAFLUSH = global i32 0
define i32 @open_olimex_iso(i8* nocapture %0) {
%2 = alloca %struct.termios
%3 = alloca %struct.serial_struct
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = bitcast %struct.termios* %2 to i8*
%9 = bitcast %struct.serial_struct* %3 to i8*
%10 = icmp slt i32 %7, 0
br i1 %10, label %33, label %11
11: ; preds = %1
%12 = call i32 @tcgetattr(i32 %7, %struct.termios* %2)
%13 = call i32 @cfmakeraw(%struct.termios* %2)
%14 = load i32, i32* @B38400
%15 = call i32 @cfsetispeed(%struct.termios* %2, i32 %14)
%16 = load i32, i32* @B38400
%17 = call i32 @cfsetospeed(%struct.termios* %2, i32 %16)
%18 = load i32, i32* @ASYNC_SPD_CUST
%19 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %3, i64 0, i32 0
store i32 120, i32* %20
%21 = load i32, i32* @TIOCSSERIAL
%22 = call i64 @ioctl(i32 %7, i32 %21, %struct.serial_struct* %3)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %11
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 @printc_err(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %33
28: ; preds = %11
%29 = load i32, i32* @TCSAFLUSH
%30 = call i64 @tcsetattr(i32 %7, i32 %29, %struct.termios* %2)
%31 = icmp slt i64 %30, 0
%32 = select i1 %31, i32 -1, i32 %7
br label %33
33: ; preds = %28, %1, %24
%34 = phi i32 [ -1, %24 ], [ -1, %1 ], [ %32, %28 ]
ret i32 %34
}