#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 */
typedef int /*<<< orphan*/ host;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ bzero(char *, int);
int openCliSocket(char *, int, int);
int opentty(char *);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
int openDevice(char *device) {
  int fd;
  char *dptr;
  if (device[0] != '/' && (dptr = strchr(device, ':'))) {
    char host[4096];
    int port;
    port = atoi(dptr + 1);
    bzero(host, sizeof(host));
    strncpy(host, device, dptr - device);
    fd = openCliSocket(host, port, 1);
  } else
    fd = opentty(device);
  if (fd < 0) {
    return (-1);
  }
  return (fd);
}


x86 assembly (clang, -O0):
<openDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11fb <openDevice+0x9b>
mov    -0x10(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11fb <openDevice+0x9b>
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x1024(%rbp)
lea    -0x1020(%rbp),%rdi
mov    $0x1000,%esi
call   1070 <bzero@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
lea    -0x1020(%rbp),%rdi
mov    -0x1024(%rbp),%esi
mov    $0x1,%edx
call   1060 <openCliSocket@plt>
mov    %eax,-0x14(%rbp)
jmp    1207 <openDevice+0xa7>
mov    -0x10(%rbp),%rdi
call   1040 <opentty@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    121d <openDevice+0xbd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1223 <openDevice+0xc3>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openDevice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rdi,%rbx
cmpb   $0x2f,(%rdi)
je     11cc <openDevice+0x6c>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11cc <openDevice+0x6c>
mov    %rax,%r14
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1000,%esi
call   1070 <bzero@plt>
sub    %ebx,%r14d
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <strncpy@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
call   1060 <openCliSocket@plt>
jmp    11d4 <openDevice+0x74>
mov    %rbx,%rdi
call   1040 <opentty@plt>
test   %eax,%eax
mov    $0xffffffff,%ecx
cmovns %eax,%ecx
mov    %ecx,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @openDevice(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [4096 x i8]
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 47
br i1 %12, label %13, label %35
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i8* @strchr(i8* %14, i8 signext 58)
store i8* %15, i8** %5
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %35
17: ; preds = %13
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %7
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%22 = call i32 @bzero(i8* %21, i32 4096)
%23 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%24 = load i8*, i8** %3
%25 = load i8*, i8** %5
%26 = load i8*, i8** %3
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = trunc i64 %29 to i32
%31 = call i32 @strncpy(i8* %23, i8* %24, i32 %30)
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%33 = load i32, i32* %7
%34 = call i32 @openCliSocket(i8* %32, i32 %33, i32 1)
store i32 %34, i32* %4
br label %38
35: ; preds = %13, %1
%36 = load i8*, i8** %3
%37 = call i32 @opentty(i8* %36)
store i32 %37, i32* %4
br label %38
38: ; preds = %35, %17
%39 = load i32, i32* %4
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 -1, i32* %2
br label %44
42: ; preds = %38
%43 = load i32, i32* %4
store i32 %43, i32* %2
br label %44
44: ; preds = %42, %41
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
define i32 @openDevice(i8* %0) {
%2 = alloca [4096 x i8]
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 47
br i1 %4, label %19, label %5
5: ; preds = %1
%6 = tail call i8* @strchr(i8* %0, i8 signext 58)
%7 = icmp eq i8* %6, null
br i1 %7, label %19, label %8
8: ; preds = %5
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = tail call i32 @atoi(i8* %10)
%12 = call i32 @bzero(i8* %9, i32 4096)
%13 = ptrtoint i8* %6 to i64
%14 = ptrtoint i8* %0 to i64
%15 = sub i64 %13, %14
%16 = trunc i64 %15 to i32
%17 = call i32 @strncpy(i8* %9, i8* %0, i32 %16)
%18 = call i32 @openCliSocket(i8* %9, i32 %11, i32 1)
br label %21
19: ; preds = %5, %1
%20 = tail call i32 @opentty(i8* %0)
br label %21
21: ; preds = %19, %8
%22 = phi i32 [ %18, %8 ], [ %20, %19 ]
%23 = icmp sgt i32 %22, -1
%24 = select i1 %23, i32 %22, i32 -1
ret i32 %24
}