#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 flock {
  scalar_t__ l_len;
  scalar_t__ l_start;
  int /*<<< orphan*/ l_whence;
  int /*<<< orphan*/ l_type;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ scrap;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EBUSY;
scalar_t__ EINTR;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_WRLCK;
int O_EXCL;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, struct flock *);
int open(char *, int);
int read(int, char **, int);
int unix_raw_mode(int, char **);
int unix_open_serial(char *devpath, int *outfd, char **errmsg, int *permfail) {
  int fd;
  char scrap[100];
  ssize_t sz;
  struct flock l;
  if ((fd = open(devpath, O_RDWR | O_EXCL | O_NOCTTY)) == -1) {
    *errmsg = "Could not open serial device.";
    if (errno != EAGAIN && errno != EBUSY && errno != EINTR)
      *permfail = 1;
    return (-1);
  }
  l.l_type = F_WRLCK;
  l.l_whence = SEEK_SET;
  l.l_start = l.l_len = 0;
  if (fcntl(fd, F_SETLKW, &l) == -1) {
    *errmsg = "Could not lock serial device.";
    return (-1);
  }
  if (unix_raw_mode(fd, errmsg) == -1) {
    (void)close(fd);
    *permfail = 1;
    return (-1);
  }
  do {
    sz = read(fd, &scrap, sizeof(scrap));
    if (sz == -1 && errno != EAGAIN) {
      *errmsg = "Failed to flush serial port before use.";
      (void)close(fd);
      return (-1);
    }
  } while (sz > 0);
  *outfd = fd;
  return (0);
}


x86 assembly (clang, -O0):
<unix_open_serial>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e72(%rip),%rax
mov    (%rax),%esi
mov    0x2e49(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    120d <unix_open_serial+0xbd>
mov    -0x20(%rbp),%rax
lea    0xe5a(%rip),%rcx
mov    %rcx,(%rax)
mov    0x2e10(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
je     1201 <unix_open_serial+0xb1>
mov    0x2df6(%rip),%rax
mov    (%rax),%rax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
je     1201 <unix_open_serial+0xb1>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
je     1201 <unix_open_serial+0xb1>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <unix_open_serial+0x1e9>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x9c(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa0(%rbp)
movq   $0x0,-0xb0(%rbp)
movq   $0x0,-0xa8(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
lea    -0xb0(%rbp),%rdx
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    127c <unix_open_serial+0x12c>
mov    -0x20(%rbp),%rax
lea    0xdb1(%rip),%rcx
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <unix_open_serial+0x1e9>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1030 <unix_raw_mode@plt>
cmp    $0xffffffff,%eax
jne    12af <unix_open_serial+0x15f>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
movl   $0x1,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <unix_open_serial+0x1e9>
jmp    12b4 <unix_open_serial+0x164>
mov    -0x2c(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x94(%rbp)
cmpl   $0xffffffff,-0x94(%rbp)
jne    1317 <unix_open_serial+0x1c7>
mov    0x2cde(%rip),%rax
mov    (%rax),%rax
mov    0x2ca4(%rip),%rcx
cmp    (%rcx),%rax
je     1317 <unix_open_serial+0x1c7>
mov    -0x20(%rbp),%rax
lea    0xd3c(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <unix_open_serial+0x1e9>
jmp    131c <unix_open_serial+0x1cc>
cmpl   $0x0,-0x94(%rbp)
jg     12b4 <unix_open_serial+0x164>
mov    -0x2c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unix_open_serial>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x90,%rsp
mov    0x2e82(%rip),%rbp
mov    0x2e5b(%rip),%rax
mov    (%rax),%eax
or     0x0(%rbp),%eax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%r14
mov    0x2e4e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11fa <unix_open_serial+0xaa>
mov    %eax,%ebp
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1040 <fcntl@plt>
cmp    $0xffffffff,%eax
je     1241 <unix_open_serial+0xf1>
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <unix_raw_mode@plt>
cmp    $0xffffffff,%eax
je     124e <unix_open_serial+0xfe>
lea    0x20(%rsp),%rbx
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     125e <unix_open_serial+0x10e>
test   %eax,%eax
jg     11e0 <unix_open_serial+0x90>
jmp    1274 <unix_open_serial+0x124>
lea    0xdff(%rip),%rax
mov    %rax,(%r12)
mov    0x2db4(%rip),%rax
mov    (%rax),%rcx
mov    0x2d7a(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
je     1292 <unix_open_serial+0x142>
mov    0x2d79(%rip),%rdx
cmp    (%rdx),%rcx
je     1292 <unix_open_serial+0x142>
mov    0x2d7d(%rip),%rdx
cmp    (%rdx),%rcx
je     1292 <unix_open_serial+0x142>
movl   $0x1,(%r15)
jmp    1292 <unix_open_serial+0x142>
lea    0xdd6(%rip),%rax
mov    %rax,(%r12)
jmp    128d <unix_open_serial+0x13d>
mov    %ebp,%edi
call   1050 <close@plt>
movl   $0x1,(%r15)
jmp    128d <unix_open_serial+0x13d>
mov    0x2d5b(%rip),%rax
mov    (%rax),%rax
mov    0x2d21(%rip),%rcx
cmp    (%rcx),%rax
jne    127b <unix_open_serial+0x12b>
mov    %ebp,(%r14)
xor    %eax,%eax
jmp    1292 <unix_open_serial+0x142>
lea    0xdba(%rip),%rax
mov    %rax,(%r12)
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@O_NOCTTY = global i32 0
@.str = [30 x i8] c"Could not open serial device.\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EBUSY = global i64 0
@EINTR = global i64 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [30 x i8] c"Could not lock serial device.\00"
@.str.2 = [40 x i8] c"Failed to flush serial port before use.\00"
define i32 @unix_open_serial(i8* %0, i32* %1, i8** %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8**
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [100 x i8]
%12 = alloca i32
%13 = alloca %struct.flock
store i8* %0, i8** %6
store i32* %1, i32** %7
store i8** %2, i8*** %8
store i32* %3, i32** %9
%14 = load i8*, i8** %6
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_EXCL
%17 = or i32 %15, %16
%18 = load i32, i32* @O_NOCTTY
%19 = or i32 %17, %18
%20 = call i32 @open(i8* %14, i32 %19)
store i32 %20, i32* %10
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %38
22: ; preds = %4
%23 = load i8**, i8*** %8
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8** %23
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp ne i64 %24, %25
br i1 %26, label %27, label %37
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = load i64, i64* @EBUSY
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = load i64, i64* @errno
%33 = load i64, i64* @EINTR
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i32*, i32** %9
store i32 1, i32* %36
br label %37
37: ; preds = %35, %31, %27, %22
store i32 -1, i32* %5
br label %82
38: ; preds = %4
%39 = load i32, i32* @F_WRLCK
%40 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 3
store i32 %39, i32* %40
%41 = load i32, i32* @SEEK_SET
%42 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 2
store i32 %41, i32* %42
%43 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 0
store i64 0, i64* %43
%44 = getelementptr inbounds %struct.flock, %struct.flock* %13, i32 0, i32 1
store i64 0, i64* %44
%45 = load i32, i32* %10
%46 = load i32, i32* @F_SETLKW
%47 = call i32 @fcntl(i32 %45, i32 %46, %struct.flock* %13)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %38
%50 = load i8**, i8*** %8
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8** %50
store i32 -1, i32* %5
br label %82
51: ; preds = %38
%52 = load i32, i32* %10
%53 = load i8**, i8*** %8
%54 = call i32 @unix_raw_mode(i32 %52, i8** %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %60
56: ; preds = %51
%57 = load i32, i32* %10
%58 = call i32 @close(i32 %57)
%59 = load i32*, i32** %9
store i32 1, i32* %59
store i32 -1, i32* %5
br label %82
60: ; preds = %51
br label %61
61: ; preds = %76, %60
%62 = load i32, i32* %10
%63 = bitcast [100 x i8]* %11 to i8**
%64 = call i32 @read(i32 %62, i8** %63, i32 100)
store i32 %64, i32* %12
%65 = load i32, i32* %12
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %75
67: ; preds = %61
%68 = load i64, i64* @errno
%69 = load i64, i64* @EAGAIN
%70 = icmp ne i64 %68, %69
br i1 %70, label %71, label %75
71: ; preds = %67
%72 = load i8**, i8*** %8
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8** %72
%73 = load i32, i32* %10
%74 = call i32 @close(i32 %73)
store i32 -1, i32* %5
br label %82
75: ; preds = %67, %61
br label %76
76: ; preds = %75
%77 = load i32, i32* %12
%78 = icmp sgt i32 %77, 0
br i1 %78, label %61, label %79
79: ; preds = %76
%80 = load i32, i32* %10
%81 = load i32*, i32** %7
store i32 %80, i32* %81
store i32 0, i32* %5
br label %82
82: ; preds = %79, %71, %56, %49, %37
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
%struct.flock = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@O_NOCTTY = global i32 0
@.str = [30 x i8] c"Could not open serial device.\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EBUSY = global i64 0
@EINTR = global i64 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLKW = global i32 0
@.str.1 = [30 x i8] c"Could not lock serial device.\00"
@.str.2 = [40 x i8] c"Failed to flush serial port before use.\00"
define i32 @unix_open_serial(i8* nocapture %0, i32* nocapture writeonly %1, i8** %2, i32* nocapture writeonly %3) {
%5 = alloca [100 x i8]
%6 = alloca %struct.flock
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%8 = bitcast %struct.flock* %6 to i8*
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_EXCL
%11 = or i32 %10, %9
%12 = load i32, i32* @O_NOCTTY
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %27
16: ; preds = %4
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8** %2
%17 = load i64, i64* @errno
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %17, %18
%20 = load i64, i64* @EBUSY
%21 = icmp eq i64 %17, %20
%22 = select i1 %19, i1 true, i1 %21
%23 = load i64, i64* @EINTR
%24 = icmp eq i64 %17, %23
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %56, label %26
26: ; preds = %16
store i32 1, i32* %3
br label %56
27: ; preds = %4
%28 = load i32, i32* @F_WRLCK
%29 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 3
store i32 %28, i32* %29
%30 = load i32, i32* @SEEK_SET
%31 = getelementptr inbounds %struct.flock, %struct.flock* %6, i64 0, i32 2
store i32 %30, i32* %31
%32 = bitcast %struct.flock* %6 to i8*
%33 = load i32, i32* @F_SETLKW
%34 = call i32 @fcntl(i32 %14, i32 %33, %struct.flock* %6)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %27
store i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8** %2
br label %56
37: ; preds = %27
%38 = call i32 @unix_raw_mode(i32 %14, i8** %2)
%39 = icmp eq i32 %38, -1
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = bitcast [100 x i8]* %5 to i8**
br label %44
42: ; preds = %37
%43 = call i32 @close(i32 %14)
store i32 1, i32* %3
br label %56
44: ; preds = %40, %53
%45 = call i32 @read(i32 %14, i8** %41, i32 100)
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %53
47: ; preds = %44
%48 = load i64, i64* @errno
%49 = load i64, i64* @EAGAIN
%50 = icmp eq i64 %48, %49
br i1 %50, label %55, label %51
51: ; preds = %47
store i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8** %2
%52 = call i32 @close(i32 %14)
br label %56
53: ; preds = %44
%54 = icmp sgt i32 %45, 0
br i1 %54, label %44, label %55
55: ; preds = %53, %47
store i32 %14, i32* %1
br label %56
56: ; preds = %16, %26, %55, %51, %42, %36
%57 = phi i32 [ -1, %36 ], [ -1, %42 ], [ -1, %51 ], [ 0, %55 ], [ -1, %26 ], [ -1, %16 ]
ret i32 %57
}