#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*/ __u_char;

/* Variables and functions */
scalar_t__ cbm_raw_write(int, void const *, size_t);
int /*<<< orphan*/ cbm_unlisten(int);
scalar_t__ cbmarch_open(int, int /*<<< orphan*/, int /*<<< orphan*/);
size_t strlen(void const *);
int cbm_open(int HandleDevice, __u_char DeviceAddress,
             __u_char SecondaryAddress, const void *Filename,
             size_t FilenameLength) {
  int returnValue;
  ;
  if (cbmarch_open(HandleDevice, DeviceAddress, SecondaryAddress) == 0) {
    returnValue = 0;
    if (Filename != NULL) {
      if (FilenameLength == 0) {
        ;
        FilenameLength = strlen(Filename);
      }
      if (FilenameLength > 0) {
        returnValue = (size_t)(cbm_raw_write(HandleDevice, Filename,
                                             FilenameLength)) != FilenameLength;
      }
      cbm_unlisten(HandleDevice);
    }
  } else {
    returnValue = -1;
  }
  return returnValue;
}


x86 assembly (clang, -O0):
<cbm_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1040 <cbmarch_open@plt>
cmp    $0x0,%rax
jne    11d2 <cbm_open+0x92>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11cd <cbm_open+0x8d>
cmpq   $0x0,-0x20(%rbp)
jne    119b <cbm_open+0x5b>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jbe    11c5 <cbm_open+0x85>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <cbm_raw_write@plt>
cmp    -0x20(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <cbm_unlisten@plt>
jmp    11d9 <cbm_open+0x99>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cbm_open>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r15
mov    %edi,%ebx
call   1040 <cbmarch_open@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1199 <cbm_open+0x59>
test   %r15,%r15
je     1197 <cbm_open+0x57>
test   %r14,%r14
jne    1178 <cbm_open+0x38>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %rax,%r14
test   %rax,%rax
je     11a6 <cbm_open+0x66>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1030 <cbm_raw_write@plt>
xor    %ebp,%ebp
cmp    %r14,%rax
setne  %bpl
mov    %ebx,%edi
call   1060 <cbm_unlisten@plt>
jmp    1199 <cbm_open+0x59>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    118e <cbm_open+0x4e>

LLVM IR (-O0):
define i32 @cbm_open(i32 %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%12 = load i32, i32* %6
%13 = load i32, i32* %7
%14 = load i32, i32* %8
%15 = call i64 @cbmarch_open(i32 %12, i32 %13, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %41
17: ; preds = %5
store i32 0, i32* %11
%18 = load i8*, i8** %9
%19 = icmp ne i8* %18, null
br i1 %19, label %20, label %40
20: ; preds = %17
%21 = load i64, i64* %10
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i8*, i8** %9
%25 = call i64 @strlen(i8* %24)
store i64 %25, i64* %10
br label %26
26: ; preds = %23, %20
%27 = load i64, i64* %10
%28 = icmp ugt i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i32, i32* %6
%31 = load i8*, i8** %9
%32 = load i64, i64* %10
%33 = call i64 @cbm_raw_write(i32 %30, i8* %31, i64 %32)
%34 = load i64, i64* %10
%35 = icmp ne i64 %33, %34
%36 = zext i1 %35 to i32
store i32 %36, i32* %11
br label %37
37: ; preds = %29, %26
%38 = load i32, i32* %6
%39 = call i32 @cbm_unlisten(i32 %38)
br label %40
40: ; preds = %37, %17
br label %42
41: ; preds = %5
store i32 -1, i32* %11
br label %42
42: ; preds = %41, %40
%43 = load i32, i32* %11
ret i32 %43
}

LLVM IR (-O2):
define i32 @cbm_open(i32 %0, i32 %1, i32 %2, i8* %3, i64 %4) {
%6 = tail call i64 @cbmarch_open(i32 %0, i32 %1, i32 %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %5
%9 = icmp eq i8* %3, null
br i1 %9, label %23, label %10
10: ; preds = %8
%11 = icmp eq i64 %4, 0
br i1 %11, label %12, label %15
12: ; preds = %10
%13 = tail call i64 @strlen(i8* %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %10, %12
%16 = phi i64 [ %13, %12 ], [ %4, %10 ]
%17 = tail call i64 @cbm_raw_write(i32 %0, i8* %3, i64 %16)
%18 = icmp ne i64 %17, %16
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %15, %12
%21 = phi i32 [ %19, %15 ], [ 0, %12 ]
%22 = tail call i32 @cbm_unlisten(i32 %0)
br label %23
23: ; preds = %5, %8, %20
%24 = phi i32 [ %21, %20 ], [ 0, %8 ], [ -1, %5 ]
ret i32 %24
}