#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_string_katcl(struct katcl_line *, int, char *);
scalar_t__ append_unsigned_long_katcl(struct katcl_line *, int, int);
int arg_buffer_katcl(struct katcl_line *, int, void *, int);
int arg_count_katcl(struct katcl_line *);
scalar_t__ dispatch_client(struct katcl_line *, char *, int, unsigned int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ have_katcl(struct katcl_line *);
int /*<<< orphan*/ stderr;
int borph_read(struct katcl_line *l, char *regname, void *buffer, int len,
               unsigned int timeout) {
  int count, got;
  if (append_string_katcl(l, 0x01, "?read") < 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_unsigned_long_katcl(l, 0x02, len) < 0)
    return -1;
  if (dispatch_client(l, "!read", 1, timeout) < 0)
    return -1;
  count = arg_count_katcl(l);
  if (count < 2) {
    fprintf(stderr, "read: insufficient arguments in reply\n");
    return -1;
  }
  got = arg_buffer_katcl(l, 2, buffer, len);
  if (got < len) {
    fprintf(stderr, "read: partial data, wanted %d, got %d\n", len, got);
    return -1;
  }
  have_katcl(l);
  return len;
}


x86 assembly (clang, -O0):
<borph_read>:
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    0xe65(%rip),%rdx
call   1030 <append_string_katcl@plt>
cmp    $0x0,%rax
jge    11b6 <borph_read+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <borph_read+0x178>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
xor    %esi,%esi
call   1030 <append_string_katcl@plt>
cmp    $0x0,%rax
jge    11db <borph_read+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <borph_read+0x178>
mov    -0x10(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1080 <append_unsigned_long_katcl@plt>
cmp    $0x0,%rax
jge    11fe <borph_read+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <borph_read+0x178>
mov    -0x10(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    $0x2,%esi
call   1080 <append_unsigned_long_katcl@plt>
cmp    $0x0,%rax
jge    1225 <borph_read+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <borph_read+0x178>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%ecx
lea    0xdd3(%rip),%rsi
mov    $0x1,%edx
call   1050 <dispatch_client@plt>
cmp    $0x0,%rax
jge    1253 <borph_read+0xe3>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <borph_read+0x178>
mov    -0x10(%rbp),%rdi
call   1070 <arg_count_katcl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x2,-0x2c(%rbp)
jge    128c <borph_read+0x11c>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <borph_read+0x178>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
mov    $0x2,%esi
call   1060 <arg_buffer_katcl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    12d9 <borph_read+0x169>
mov    0x2d29(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    -0x30(%rbp),%ecx
lea    0xd6d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e8 <borph_read+0x178>
mov    -0x10(%rbp),%rdi
call   1090 <have_katcl@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<borph_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r13d
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe6f(%rip),%rdx
mov    $0x1,%esi
call   1030 <append_string_katcl@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
js     1271 <borph_read+0x101>
mov    %rbp,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <append_string_katcl@plt>
test   %rax,%rax
js     1271 <borph_read+0x101>
mov    %rbp,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <append_unsigned_long_katcl@plt>
test   %rax,%rax
js     1271 <borph_read+0x101>
mov    %rbp,%rdi
mov    $0x2,%esi
mov    %r14d,%edx
call   1080 <append_unsigned_long_katcl@plt>
test   %rax,%rax
js     1271 <borph_read+0x101>
lea    0xe11(%rip),%rsi
mov    %rbp,%rdi
mov    $0x1,%edx
mov    %r13d,%ecx
call   1050 <dispatch_client@plt>
test   %rax,%rax
js     1271 <borph_read+0x101>
mov    %rbp,%rdi
call   1070 <arg_count_katcl@plt>
cmp    $0x1,%eax
jg     1230 <borph_read+0xc0>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1271 <borph_read+0x101>
mov    %rbp,%rdi
mov    $0x2,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1060 <arg_buffer_katcl@plt>
cmp    %r14d,%eax
jge    1266 <borph_read+0xf6>
mov    0x2d91(%rip),%rcx
mov    (%rcx),%edi
lea    0xddb(%rip),%rsi
mov    %r14d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1271 <borph_read+0x101>
mov    %rbp,%rdi
call   1090 <have_katcl@plt>
mov    %r14d,%r12d
mov    %r12d,%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 = [6 x i8] c"?read\00"
@.str.1 = [6 x i8] c"!read\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"read: insufficient arguments in reply\0A\00"
@.str.3 = [39 x i8] c"read: partial data, wanted %d, got %d\0A\00"
define i32 @borph_read(%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
%12 = alloca i32
%13 = 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
%14 = load %struct.katcl_line*, %struct.katcl_line** %7
%15 = call i64 @append_string_katcl(%struct.katcl_line* %14, i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %5
store i32 -1, i32* %6
br label %66
18: ; preds = %5
%19 = load %struct.katcl_line*, %struct.katcl_line** %7
%20 = load i8*, i8** %8
%21 = call i64 @append_string_katcl(%struct.katcl_line* %19, i32 0, i8* %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %6
br label %66
24: ; preds = %18
%25 = load %struct.katcl_line*, %struct.katcl_line** %7
%26 = call i64 @append_unsigned_long_katcl(%struct.katcl_line* %25, i32 0, i32 0)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %24
store i32 -1, i32* %6
br label %66
29: ; preds = %24
%30 = load %struct.katcl_line*, %struct.katcl_line** %7
%31 = load i32, i32* %10
%32 = call i64 @append_unsigned_long_katcl(%struct.katcl_line* %30, i32 2, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
store i32 -1, i32* %6
br label %66
35: ; preds = %29
%36 = load %struct.katcl_line*, %struct.katcl_line** %7
%37 = load i32, i32* %11
%38 = call i64 @dispatch_client(%struct.katcl_line* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 -1, i32* %6
br label %66
41: ; preds = %35
%42 = load %struct.katcl_line*, %struct.katcl_line** %7
%43 = call i32 @arg_count_katcl(%struct.katcl_line* %42)
store i32 %43, i32* %12
%44 = load i32, i32* %12
%45 = icmp slt i32 %44, 2
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %6
br label %66
49: ; preds = %41
%50 = load %struct.katcl_line*, %struct.katcl_line** %7
%51 = load i8*, i8** %9
%52 = load i32, i32* %10
%53 = call i32 @arg_buffer_katcl(%struct.katcl_line* %50, i32 2, i8* %51, i32 %52)
store i32 %53, i32* %13
%54 = load i32, i32* %13
%55 = load i32, i32* %10
%56 = icmp slt i32 %54, %55
br i1 %56, label %57, label %62
57: ; preds = %49
%58 = load i32, i32* @stderr
%59 = load i32, i32* %10
%60 = load i32, i32* %13
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %59, i32 %60)
store i32 -1, i32* %6
br label %66
62: ; preds = %49
%63 = load %struct.katcl_line*, %struct.katcl_line** %7
%64 = call i32 @have_katcl(%struct.katcl_line* %63)
%65 = load i32, i32* %10
store i32 %65, i32* %6
br label %66
66: ; preds = %62, %57, %46, %40, %34, %28, %23, %17
%67 = load i32, i32* %6
ret i32 %67
}

LLVM IR (-O2):
%struct.katcl_line = type { i32 }
@.str = [6 x i8] c"?read\00"
@.str.1 = [6 x i8] c"!read\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"read: insufficient arguments in reply\0A\00"
@.str.3 = [39 x i8] c"read: partial data, wanted %d, got %d\0A\00"
define i32 @borph_read(%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 ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%7 = icmp slt i64 %6, 0
br i1 %7, label %34, 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 %34, 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 %34, label %14
14: ; preds = %11
%15 = tail call i64 @append_unsigned_long_katcl(%struct.katcl_line* %0, i32 2, i32 %3)
%16 = icmp slt i64 %15, 0
br i1 %16, label %34, label %17
17: ; preds = %14
%18 = tail call i64 @dispatch_client(%struct.katcl_line* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %34, label %20
20: ; preds = %17
%21 = tail call i32 @arg_count_katcl(%struct.katcl_line* %0)
%22 = icmp slt i32 %21, 2
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %34
26: ; preds = %20
%27 = tail call i32 @arg_buffer_katcl(%struct.katcl_line* %0, i32 2, i8* %2, i32 %3)
%28 = icmp slt i32 %27, %3
br i1 %28, label %29, label %32
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %3, i32 %27)
br label %34
32: ; preds = %26
%33 = tail call i32 @have_katcl(%struct.katcl_line* %0)
br label %34
34: ; preds = %17, %14, %11, %8, %5, %32, %29, %23
%35 = phi i32 [ -1, %23 ], [ -1, %29 ], [ %3, %32 ], [ -1, %5 ], [ -1, %8 ], [ -1, %11 ], [ -1, %14 ], [ -1, %17 ]
ret i32 %35
}