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

/* Variables and functions */
scalar_t__ append_buffer_katcl(struct katcl_line *, int, void *, int);
scalar_t__ append_string_katcl(struct katcl_line *, int, char *);
scalar_t__ append_unsigned_long_katcl(struct katcl_line *, int /*<<< orphan*/,
                                      int /*<<< orphan*/);
scalar_t__ dispatch_client(struct katcl_line *, char *, int, unsigned int);
int /*<<< orphan*/ have_katcl(struct katcl_line *);
int borph_write(struct katcl_line *l, char *regname, void *buffer, int len,
                unsigned int timeout) {
  if (append_string_katcl(l, 0x01, "?write") < 0)
    return -1;
  if (append_string_katcl(l, 0, regname) < 0)
    return -1;
  if (append_unsigned_long_katcl(l, 0, 0) < 0)
    return -1;
  if (append_buffer_katcl(l, 0x02, buffer, len) < 0)
    return -1;
  if (dispatch_client(l, "!write", 1, timeout) < 0)
    return -1;
  have_katcl(l);
  return 0;
}


x86 assembly (clang, -O0):
<borph_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
lea    0xe85(%rip),%rdx
call   1040 <append_string_katcl@plt>
cmp    $0x0,%rax
jge    1196 <borph_write+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <borph_write+0xf7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1040 <append_string_katcl@plt>
cmp    $0x0,%rax
jge    11bb <borph_write+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <borph_write+0xf7>
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <append_unsigned_long_katcl@plt>
cmp    $0x0,%rax
jge    11de <borph_write+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <borph_write+0xf7>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x2,%esi
call   1030 <append_buffer_katcl@plt>
cmp    $0x0,%rax
jge    1209 <borph_write+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <borph_write+0xf7>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%ecx
lea    0xdf0(%rip),%rsi
mov    $0x1,%edx
call   1050 <dispatch_client@plt>
cmp    $0x0,%rax
jge    1237 <borph_write+0xe7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <borph_write+0xf7>
mov    -0x10(%rbp),%rdi
call   1070 <have_katcl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<borph_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %ecx,%r15d
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xe8f(%rip),%rdx
mov    $0x1,%esi
call   1040 <append_string_katcl@plt>
mov    $0xffffffff,%r13d
test   %rax,%rax
js     11e8 <borph_write+0x98>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1040 <append_string_katcl@plt>
test   %rax,%rax
js     11e8 <borph_write+0x98>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <append_unsigned_long_katcl@plt>
test   %rax,%rax
js     11e8 <borph_write+0x98>
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %r12,%rdx
mov    %r15d,%ecx
call   1030 <append_buffer_katcl@plt>
test   %rax,%rax
js     11e8 <borph_write+0x98>
lea    0xe3f(%rip),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
mov    %r14d,%ecx
call   1050 <dispatch_client@plt>
test   %rax,%rax
js     11e8 <borph_write+0x98>
mov    %rbx,%rdi
call   1070 <have_katcl@plt>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.katcl_line = type { i32 }
@.str = [7 x i8] c"?write\00"
@.str.1 = [7 x i8] c"!write\00"
define i32 @borph_write(%struct.katcl_line* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.katcl_line*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store %struct.katcl_line* %0, %struct.katcl_line** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%12 = load %struct.katcl_line*, %struct.katcl_line** %7
%13 = call i64 @append_string_katcl(%struct.katcl_line* %12, i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %5
store i32 -1, i32* %6
br label %43
16: ; preds = %5
%17 = load %struct.katcl_line*, %struct.katcl_line** %7
%18 = load i8*, i8** %8
%19 = call i64 @append_string_katcl(%struct.katcl_line* %17, i32 0, i8* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 -1, i32* %6
br label %43
22: ; preds = %16
%23 = load %struct.katcl_line*, %struct.katcl_line** %7
%24 = call i64 @append_unsigned_long_katcl(%struct.katcl_line* %23, i32 0, i32 0)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 -1, i32* %6
br label %43
27: ; preds = %22
%28 = load %struct.katcl_line*, %struct.katcl_line** %7
%29 = load i8*, i8** %9
%30 = load i32, i32* %10
%31 = call i64 @append_buffer_katcl(%struct.katcl_line* %28, i32 2, i8* %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 -1, i32* %6
br label %43
34: ; preds = %27
%35 = load %struct.katcl_line*, %struct.katcl_line** %7
%36 = load i32, i32* %11
%37 = call i64 @dispatch_client(%struct.katcl_line* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 -1, i32* %6
br label %43
40: ; preds = %34
%41 = load %struct.katcl_line*, %struct.katcl_line** %7
%42 = call i32 @have_katcl(%struct.katcl_line* %41)
store i32 0, i32* %6
br label %43
43: ; preds = %40, %39, %33, %26, %21, %15
%44 = load i32, i32* %6
ret i32 %44
}

LLVM IR (-O2):
%struct.katcl_line = type { i32 }
@.str = [7 x i8] c"?write\00"
@.str.1 = [7 x i8] c"!write\00"
define i32 @borph_write(%struct.katcl_line* %0, i8* %1, i8* %2, i32 %3, i32 %4) {
%6 = tail call i64 @append_string_katcl(%struct.katcl_line* %0, i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = icmp slt i64 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %5
%9 = tail call i64 @append_string_katcl(%struct.katcl_line* %0, i32 0, i8* %1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %22, label %11
11: ; preds = %8
%12 = tail call i64 @append_unsigned_long_katcl(%struct.katcl_line* %0, i32 0, i32 0)
%13 = icmp slt i64 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %11
%15 = tail call i64 @append_buffer_katcl(%struct.katcl_line* %0, i32 2, i8* %2, i32 %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %22, label %17
17: ; preds = %14
%18 = tail call i64 @dispatch_client(%struct.katcl_line* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = tail call i32 @have_katcl(%struct.katcl_line* %0)
br label %22
22: ; preds = %17, %14, %11, %8, %5, %20
%23 = phi i32 [ 0, %20 ], [ -1, %5 ], [ -1, %8 ], [ -1, %11 ], [ -1, %14 ], [ -1, %17 ]
ret i32 %23
}