#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 serial_port {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ *memchr(char const *, char, size_t);
int /*<<< orphan*/ serial_port_write(struct serial_port *, char const *,
                                     size_t);
size_t strlen(char const *);
bool ez_writer_write_track(struct serial_port *sport, unsigned track,
                           const char *trackdata, size_t len) {
  char track_begin[] = {'\x1b', track & (((1) << ((1) & (1 | 2 | 3))) |
                                         ((1) << ((2) & (1 | 2 | 3))) |
                                         ((1) << ((3) & (1 | 2 | 3))))};
  if (!serial_port_write(sport, track_begin,
                         sizeof track_begin / sizeof track_begin[0]))
    return (false);
  if (memchr(trackdata, '\0', len) != NULL)
    len = strlen(trackdata);
  if (len == 0)
    return (true);
  if (!serial_port_write(sport, trackdata + 1, len - 2))
    return (false);
  return (true);
}


x86 assembly (clang, -O0):
<ez_writer_write_track>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movb   $0x1b,-0x2a(%rbp)
mov    -0x14(%rbp),%eax
and    $0xe,%eax
mov    %al,-0x29(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x2a(%rbp),%rsi
mov    $0x2,%edx
call   1050 <serial_port_write@plt>
cmp    $0x0,%eax
jne    117b <ez_writer_write_track+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    11ed <ez_writer_write_track+0xbd>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1040 <memchr@plt>
cmp    $0x0,%rax
je     11a1 <ez_writer_write_track+0x71>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b8 <ez_writer_write_track+0x88>
movl   $0x1,-0x4(%rbp)
jmp    11ed <ez_writer_write_track+0xbd>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
add    $0x1,%rsi
mov    -0x28(%rbp),%rdx
sub    $0x2,%rdx
call   1050 <serial_port_write@plt>
cmp    $0x0,%eax
jne    11e6 <ez_writer_write_track+0xb6>
movl   $0x0,-0x4(%rbp)
jmp    11ed <ez_writer_write_track+0xbd>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ez_writer_write_track>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rdi,%r14
movb   $0x1b,0xe(%rsp)
and    $0xe,%sil
mov    %sil,0xf(%rsp)
lea    0xe(%rsp),%rsi
mov    $0x2,%edx
call   1050 <serial_port_write@plt>
test   %eax,%eax
je     11a6 <ez_writer_write_track+0x76>
mov    %r15,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memchr@plt>
test   %rax,%rax
je     1180 <ez_writer_write_track+0x50>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     11aa <ez_writer_write_track+0x7a>
add    $0x1,%r15
add    $0xfffffffffffffffe,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <serial_port_write@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
setne  %al
jmp    11af <ez_writer_write_track+0x7f>
xor    %eax,%eax
jmp    11af <ez_writer_write_track+0x7f>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.serial_port = type { i32 }
define i32 @ez_writer_write_track(%struct.serial_port* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca %struct.serial_port*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca [2 x i8]
store %struct.serial_port* %0, %struct.serial_port** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
store i8 27, i8* %11
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = load i32, i32* %7
%14 = and i32 %13, 14
%15 = trunc i32 %14 to i8
store i8 %15, i8* %12
%16 = load %struct.serial_port*, %struct.serial_port** %6
%17 = getelementptr inbounds [2 x i8], [2 x i8]* %10, i64 0, i64 0
%18 = call i32 @serial_port_write(%struct.serial_port* %16, i8* %17, i64 2)
%19 = icmp ne i32 %18, 0
br i1 %19, label %21, label %20
20: ; preds = %4
store i32 0, i32* %5
br label %43
21: ; preds = %4
%22 = load i8*, i8** %8
%23 = load i64, i64* %9
%24 = call i32* @memchr(i8* %22, i8 signext 0, i64 %23)
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i8*, i8** %8
%28 = call i64 @strlen(i8* %27)
store i64 %28, i64* %9
br label %29
29: ; preds = %26, %21
%30 = load i64, i64* %9
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 1, i32* %5
br label %43
33: ; preds = %29
%34 = load %struct.serial_port*, %struct.serial_port** %6
%35 = load i8*, i8** %8
%36 = getelementptr inbounds i8, i8* %35, i64 1
%37 = load i64, i64* %9
%38 = sub i64 %37, 2
%39 = call i32 @serial_port_write(%struct.serial_port* %34, i8* %36, i64 %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %33
store i32 0, i32* %5
br label %43
42: ; preds = %33
store i32 1, i32* %5
br label %43
43: ; preds = %42, %41, %32, %20
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
%struct.serial_port = type { i32 }
define i32 @ez_writer_write_track(%struct.serial_port* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca [2 x i8]
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
store i8 27, i8* %6
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%8 = trunc i32 %1 to i8
%9 = and i8 %8, 14
store i8 %9, i8* %7
%10 = call i32 @serial_port_write(%struct.serial_port* %0, i8* %6, i64 2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %4
%13 = call i32* @memchr(i8* %2, i8 signext 0, i64 %3)
%14 = icmp eq i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i64 @strlen(i8* dereferenceable(1) %2)
br label %17
17: ; preds = %15, %12
%18 = phi i64 [ %16, %15 ], [ %3, %12 ]
%19 = icmp eq i64 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %17
%21 = getelementptr inbounds i8, i8* %2, i64 1
%22 = add i64 %18, -2
%23 = call i32 @serial_port_write(%struct.serial_port* %0, i8* %21, i64 %22)
%24 = icmp ne i32 %23, 0
%25 = zext i1 %24 to i32
br label %26
26: ; preds = %20, %17, %4
%27 = phi i32 [ 0, %4 ], [ 1, %17 ], [ %25, %20 ]
ret i32 %27
}