#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int fcntl(int, int /*<<< orphan*/, int);
int open(char *, int);
int kiss_platform_serial_open(char *portname) {
  int stream;
  stream = open(portname, O_NDELAY | O_RDWR | O_NOCTTY);
  if (stream < 0)
    return -1;
  fcntl(stream, F_SETFL, ~O_NDELAY & fcntl(stream, F_GETFL, 0));
  return stream;
}


x86 assembly (clang, -O0):
<kiss_platform_serial_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2ea8(%rip),%rax
or     (%rax),%esi
mov    0x2e8f(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1169 <kiss_platform_serial_open+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b3 <kiss_platform_serial_open+0x93>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
and    %eax,%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kiss_platform_serial_open>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e9d(%rip),%rbp
mov    0x2eb6(%rip),%rax
mov    (%rax),%esi
or     0x0(%rbp),%esi
mov    0x2e9a(%rip),%rax
or     (%rax),%esi
call   1040 <open@plt>
test   %eax,%eax
js     117c <kiss_platform_serial_open+0x5c>
mov    %eax,%ebx
mov    0x2e7e(%rip),%rax
mov    (%rax),%r14d
mov    0x0(%rbp),%ebp
not    %ebp
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
and    %eax,%ebp
mov    %ebx,%edi
mov    %r14d,%esi
mov    %ebp,%edx
call   1030 <fcntl@plt>
jmp    1181 <kiss_platform_serial_open+0x61>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_NDELAY = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
define i32 @kiss_platform_serial_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = load i32, i32* @O_NDELAY
%7 = load i32, i32* @O_RDWR
%8 = or i32 %6, %7
%9 = load i32, i32* @O_NOCTTY
%10 = or i32 %8, %9
%11 = call i32 @open(i8* %5, 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 %26
15: ; preds = %1
%16 = load i32, i32* %4
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* @O_NDELAY
%19 = xor i32 %18, -1
%20 = load i32, i32* %4
%21 = load i32, i32* @F_GETFL
%22 = call i32 @fcntl(i32 %20, i32 %21, i32 0)
%23 = and i32 %19, %22
%24 = call i32 @fcntl(i32 %16, i32 %17, i32 %23)
%25 = load i32, i32* %4
store i32 %25, i32* %2
br label %26
26: ; preds = %15, %14
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@O_NDELAY = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
define i32 @kiss_platform_serial_open(i8* nocapture %0) {
%2 = load i32, i32* @O_NDELAY
%3 = load i32, i32* @O_RDWR
%4 = or i32 %3, %2
%5 = load i32, i32* @O_NOCTTY
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = load i32, i32* @F_SETFL
%11 = load i32, i32* @O_NDELAY
%12 = xor i32 %11, -1
%13 = load i32, i32* @F_GETFL
%14 = tail call i32 @fcntl(i32 %7, i32 %13, i32 0)
%15 = and i32 %14, %12
%16 = tail call i32 @fcntl(i32 %7, i32 %10, i32 %15)
br label %17
17: ; preds = %1, %9
%18 = phi i32 [ %7, %9 ], [ -1, %1 ]
ret i32 %18
}