#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 EINVAL;
int O_NDELAY;
int O_NOCTTY;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ configure_ser(int, int);
int errno;
int get_ai_status(int);
int is_k3(int);
int open(char const *, int);
int set_ai_status(int);
int /*<<< orphan*/ snprintf(char *, size_t, char *, ...);
char *strerror(int);
int k3open(const char *device, int speed, char *errbuf, size_t errbuf_len) {
  int fd;
  int saveerr;
  int err;
  if ((!device) || (!errbuf) || (errbuf_len <= 255)) {
    errno = EINVAL;
    return -1;
  }
  fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd < 0) {
    saveerr = errno;
    snprintf(errbuf, errbuf_len, "Unable to open serial device: %s\n",
             strerror(saveerr));
    errno = saveerr;
    return -1;
  }
  if (configure_ser(fd, speed) < 0) {
    saveerr = errno;
    close(fd);
    snprintf(errbuf, errbuf_len, "Unable to configure serial device: %s\n",
             strerror(saveerr));
    errno = saveerr;
    return -1;
  }
  err = is_k3(fd);
  if (err < 0) {
    switch (err) {
    case -2:
      snprintf(errbuf, errbuf_len,
               "Device connected on %s"
               "at %d baud is not a K3.\n",
               device, speed);
      break;
    case -1:
      snprintf(errbuf, errbuf_len, "Unable to determine if device is K3\n");
      break;
    default:
      break;
    }
    close(fd);
    return -1;
  }
  err = get_ai_status(fd);
  if (err < 0) {
    snprintf(errbuf, errbuf_len, "Unable to determine AI status of K3\n");
    close(fd);
    return -1;
  }
  err = set_ai_status(fd);
  if (err < 0) {
    snprintf(errbuf, errbuf_len, "Unable to set AI status of K3 to 0\n");
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<k3open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11bb <k3open+0x3b>
cmpq   $0x0,-0x20(%rbp)
je     11bb <k3open+0x3b>
cmpq   $0xff,-0x28(%rbp)
ja     11d9 <k3open+0x59>
mov    0x2dfe(%rip),%rax
mov    (%rax),%ecx
mov    0x2e05(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13e6 <k3open+0x266>
mov    -0x10(%rbp),%rdi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
or     (%rax),%esi
mov    0x2dd2(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    125f <k3open+0xdf>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%edi
call   10a0 <strerror@plt>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rcx
lea    0xdc0(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x30(%rbp),%ecx
mov    0x2d7f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13e6 <k3open+0x266>
mov    -0x2c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1090 <configure_ser@plt>
cmp    $0x0,%rax
jge    12d1 <k3open+0x151>
mov    0x2d55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%edi
call   10a0 <strerror@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rcx
lea    0xd70(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x30(%rbp),%ecx
mov    0x2d0d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13e6 <k3open+0x266>
mov    -0x2c(%rbp),%edi
call   1070 <is_k3@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1362 <k3open+0x1e2>
mov    -0x34(%rbp),%eax
mov    %eax,-0x5c(%rbp)
sub    $0xfffffffe,%eax
je     130b <k3open+0x18b>
jmp    12fa <k3open+0x17a>
mov    -0x5c(%rbp),%eax
sub    $0xffffffff,%eax
je     132e <k3open+0x1ae>
jmp    1349 <k3open+0x1c9>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
lea    0xd27(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    134e <k3open+0x1ce>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    0xd3b(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
jmp    134e <k3open+0x1ce>
jmp    134e <k3open+0x1ce>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e6 <k3open+0x266>
mov    -0x2c(%rbp),%edi
call   1040 <get_ai_status@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    13a1 <k3open+0x221>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    0xd17(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e6 <k3open+0x266>
mov    -0x2c(%rbp),%edi
call   1030 <set_ai_status@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    13e0 <k3open+0x260>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    0xcfd(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13e6 <k3open+0x266>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<k3open>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdi,%rdi
je     121d <k3open+0x9d>
mov    %rdx,%r12
test   %rdx,%rdx
je     121d <k3open+0x9d>
mov    %rcx,%r14
cmp    $0xff,%rcx
jbe    121d <k3open+0x9d>
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2e32(%rip),%rax
mov    0x2e1b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e00(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1234 <k3open+0xb4>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15d,%esi
call   1090 <configure_ser@plt>
test   %rax,%rax
js     1261 <k3open+0xe1>
mov    %ebp,%edi
call   1070 <is_k3@plt>
test   %eax,%eax
js     1295 <k3open+0x115>
mov    %ebp,%edi
call   1040 <get_ai_status@plt>
test   %eax,%eax
js     12bb <k3open+0x13b>
mov    %ebp,%edi
call   1030 <set_ai_status@plt>
test   %eax,%eax
jns    12e4 <k3open+0x164>
lea    0xeaa(%rip),%rdx
jmp    12cb <k3open+0x14b>
mov    0x2d9c(%rip),%rax
mov    (%rax),%eax
mov    0x2da3(%rip),%rcx
mov    %eax,(%rcx)
jmp    12df <k3open+0x15f>
mov    0x2d95(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xdb4(%rip),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %ebx,0x0(%rbp)
jmp    12df <k3open+0x15f>
mov    0x2d68(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xda2(%rip),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %ebx,(%r15)
jmp    12df <k3open+0x15f>
cmp    $0xffffffff,%eax
je     12c4 <k3open+0x144>
cmp    $0xfffffffe,%eax
jne    12d8 <k3open+0x158>
lea    0xda3(%rip),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
mov    %r15d,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
jmp    12d8 <k3open+0x158>
lea    0xddb(%rip),%rdx
jmp    12cb <k3open+0x14b>
lea    0xdad(%rip),%rdx
mov    %r12,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [34 x i8] c"Unable to open serial device: %s\0A\00"
@.str.1 = [39 x i8] c"Unable to configure serial device: %s\0A\00"
@.str.2 = [47 x i8] c"Device connected on %sat %d baud is not a K3.\0A\00"
@.str.3 = [37 x i8] c"Unable to determine if device is K3\0A\00"
@.str.4 = [37 x i8] c"Unable to determine AI status of K3\0A\00"
@.str.5 = [36 x i8] c"Unable to set AI status of K3 to 0\0A\00"
define i32 @k3open(i8* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = load i8*, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %21
15: ; preds = %4
%16 = load i8*, i8** %8
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i64, i64* %9
%20 = icmp ule i64 %19, 255
br i1 %20, label %21, label %23
21: ; preds = %18, %15, %4
%22 = load i32, i32* @EINVAL
store i32 %22, i32* @errno
store i32 -1, i32* %5
br label %101
23: ; preds = %18
%24 = load i8*, i8** %6
%25 = load i32, i32* @O_RDWR
%26 = load i32, i32* @O_NOCTTY
%27 = or i32 %25, %26
%28 = load i32, i32* @O_NDELAY
%29 = or i32 %27, %28
%30 = call i32 @open(i8* %24, i32 %29)
store i32 %30, i32* %10
%31 = load i32, i32* %10
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %23
%34 = load i32, i32* @errno
store i32 %34, i32* %11
%35 = load i8*, i8** %8
%36 = load i64, i64* %9
%37 = load i32, i32* %11
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %35, i64 %36, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %38)
%40 = load i32, i32* %11
store i32 %40, i32* @errno
store i32 -1, i32* %5
br label %101
41: ; preds = %23
%42 = load i32, i32* %10
%43 = load i32, i32* %7
%44 = call i64 @configure_ser(i32 %42, i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %41
%47 = load i32, i32* @errno
store i32 %47, i32* %11
%48 = load i32, i32* %10
%49 = call i32 @close(i32 %48)
%50 = load i8*, i8** %8
%51 = load i64, i64* %9
%52 = load i32, i32* %11
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %50, i64 %51, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %53)
%55 = load i32, i32* %11
store i32 %55, i32* @errno
store i32 -1, i32* %5
br label %101
56: ; preds = %41
%57 = load i32, i32* %10
%58 = call i32 @is_k3(i32 %57)
store i32 %58, i32* %12
%59 = load i32, i32* %12
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %77
61: ; preds = %56
%62 = load i32, i32* %12
switch i32 %62, label %73 [
i32 -2, label %63
i32 -1, label %69
]
63: ; preds = %61
%64 = load i8*, i8** %8
%65 = load i64, i64* %9
%66 = load i8*, i8** %6
%67 = load i32, i32* %7
%68 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %64, i64 %65, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %66, i32 %67)
br label %74
69: ; preds = %61
%70 = load i8*, i8** %8
%71 = load i64, i64* %9
%72 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %70, i64 %71, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %74
73: ; preds = %61
br label %74
74: ; preds = %73, %69, %63
%75 = load i32, i32* %10
%76 = call i32 @close(i32 %75)
store i32 -1, i32* %5
br label %101
77: ; preds = %56
%78 = load i32, i32* %10
%79 = call i32 @get_ai_status(i32 %78)
store i32 %79, i32* %12
%80 = load i32, i32* %12
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %88
82: ; preds = %77
%83 = load i8*, i8** %8
%84 = load i64, i64* %9
%85 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %83, i64 %84, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%86 = load i32, i32* %10
%87 = call i32 @close(i32 %86)
store i32 -1, i32* %5
br label %101
88: ; preds = %77
%89 = load i32, i32* %10
%90 = call i32 @set_ai_status(i32 %89)
store i32 %90, i32* %12
%91 = load i32, i32* %12
%92 = icmp slt i32 %91, 0
br i1 %92, label %93, label %99
93: ; preds = %88
%94 = load i8*, i8** %8
%95 = load i64, i64* %9
%96 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %94, i64 %95, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%97 = load i32, i32* %10
%98 = call i32 @close(i32 %97)
store i32 -1, i32* %5
br label %101
99: ; preds = %88
%100 = load i32, i32* %10
store i32 %100, i32* %5
br label %101
101: ; preds = %99, %93, %82, %74, %46, %33, %21
%102 = load i32, i32* %5
ret i32 %102
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@O_RDWR = global i32 0
@O_NOCTTY = global i32 0
@O_NDELAY = global i32 0
@.str = [34 x i8] c"Unable to open serial device: %s\0A\00"
@.str.1 = [39 x i8] c"Unable to configure serial device: %s\0A\00"
@.str.2 = [47 x i8] c"Device connected on %sat %d baud is not a K3.\0A\00"
@.str.3 = [37 x i8] c"Unable to determine if device is K3\0A\00"
@.str.4 = [37 x i8] c"Unable to determine AI status of K3\0A\00"
@.str.5 = [36 x i8] c"Unable to set AI status of K3 to 0\0A\00"
define i32 @k3open(i8* %0, i32 %1, i8* writeonly %2, i64 %3) {
%5 = icmp eq i8* %0, null
%6 = icmp eq i8* %2, null
%7 = or i1 %5, %6
%8 = icmp ult i64 %3, 256
%9 = or i1 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %54
12: ; preds = %4
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_NOCTTY
%15 = or i32 %14, %13
%16 = load i32, i32* @O_NDELAY
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %0, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %22)
store i32 %21, i32* @errno
br label %54
24: ; preds = %12
%25 = tail call i64 @configure_ser(i32 %18, i32 %1)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @errno
%29 = tail call i32 @close(i32 %18)
%30 = tail call i8* @strerror(i32 %28)
%31 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 %28, i32* @errno
br label %54
32: ; preds = %24
%33 = tail call i32 @is_k3(i32 %18)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %32
switch i32 %33, label %40 [
i32 -2, label %36
i32 -1, label %38
]
36: ; preds = %35
%37 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1)
br label %40
38: ; preds = %35
%39 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %35, %38, %36
%41 = tail call i32 @close(i32 %18)
br label %54
42: ; preds = %32
%43 = tail call i32 @get_ai_status(i32 %18)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
%47 = tail call i32 @close(i32 %18)
br label %54
48: ; preds = %42
%49 = tail call i32 @set_ai_status(i32 %18)
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 %3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%53 = tail call i32 @close(i32 %18)
br label %54
54: ; preds = %48, %51, %45, %40, %27, %20, %10
%55 = phi i32 [ -1, %10 ], [ -1, %20 ], [ -1, %27 ], [ -1, %40 ], [ -1, %45 ], [ -1, %51 ], [ %18, %48 ]
ret i32 %55
}