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

/* Variables and functions */
char check_code(char *, int);
int /*<<< orphan*/ dump_buffer(char *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int write(int, char *, int);
int write_buffer(int tty, char *out_buffer, char *in_buffer) {
  int res = -1;
  char buffer[17];
  if (out_buffer) {
    memcpy(buffer, out_buffer, 17 - 1);
    buffer[17 - 1] = check_code(buffer, 17 - 1);
    printf("->");
    dump_buffer(buffer);
    res = write(tty, buffer, 17);
    if (res < 0) {
      printf("?? Can't write, result is %d\n", res);
      return res;
    }
  }
  if (in_buffer) {
    printf("<-");
    memset(buffer, 0, 17);
    int pos = 0;
    while (pos < 17) {
      res = read(tty, buffer + pos, 17 - pos);
      if (res < 0) {
        printf("?? Can't read = %d\n", res);
        return res;
      }
      pos += res;
    }
    dump_buffer(buffer);
    if (out_buffer && memcmp(out_buffer, buffer, 2) != 0) {
      printf("?? Different operations\n");
    }
    memcpy(in_buffer, buffer, 17);
    return pos;
  }
  return res;
}


x86 assembly (clang, -O0):
<write_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     121e <write_buffer+0x9e>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1080 <memcpy@plt>
lea    -0x30(%rbp),%rdi
mov    $0x10,%esi
call   10a0 <check_code@plt>
mov    %al,-0x20(%rbp)
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
call   1090 <dump_buffer@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x11,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1219 <write_buffer+0x99>
mov    -0x1c(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    130b <write_buffer+0x18b>
jmp    121e <write_buffer+0x9e>
cmpq   $0x0,-0x18(%rbp)
je     1305 <write_buffer+0x185>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1050 <memset@plt>
movl   $0x0,-0x34(%rbp)
cmpl   $0x11,-0x34(%rbp)
jge    12aa <write_buffer+0x12a>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
movslq -0x34(%rbp),%rax
add    %rax,%rsi
mov    $0x11,%edx
sub    -0x34(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    129c <write_buffer+0x11c>
mov    -0x1c(%rbp),%esi
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    130b <write_buffer+0x18b>
mov    -0x1c(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    124e <write_buffer+0xce>
lea    -0x30(%rbp),%rdi
call   1090 <dump_buffer@plt>
cmpq   $0x0,-0x10(%rbp)
je     12e8 <write_buffer+0x168>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x2,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     12e8 <write_buffer+0x168>
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x11,%edx
call   1080 <memcpy@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    130b <write_buffer+0x18b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buffer>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%r12d
test   %rsi,%rsi
je     120f <write_buffer+0x7f>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1090 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x10,%esi
call   10b0 <check_code@plt>
mov    %al,0x10(%rsp)
lea    0xe2b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   10a0 <dump_buffer@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x11,%edx
call   1040 <write@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1214 <write_buffer+0x84>
lea    0xe02(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    12b5 <write_buffer+0x125>
mov    $0xffffffff,%ebx
test   %r14,%r14
je     12b5 <write_buffer+0x125>
lea    0xdfd(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x11,%edx
call   1060 <memset@plt>
nopl   0x0(%rax)
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    $0x11,%edx
sub    %eax,%edx
mov    %r12d,%edi
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12a3 <write_buffer+0x113>
add    %ebp,%ebx
cmp    $0x11,%ebx
jl     1240 <write_buffer+0xb0>
mov    %rsp,%rdi
call   10a0 <dump_buffer@plt>
test   %r15,%r15
je     1291 <write_buffer+0x101>
mov    %rsp,%rsi
mov    %r15,%rdi
mov    $0x2,%edx
call   1080 <memcmp@plt>
test   %rax,%rax
je     1291 <write_buffer+0x101>
lea    0xdac(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x11,%edx
call   1090 <memcpy@plt>
jmp    12b5 <write_buffer+0x125>
lea    0xd7a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"->\00"
@.str.1 = [30 x i8] c"?? Can't write, result is %d\0A\00"
@.str.2 = [3 x i8] c"<-\00"
@.str.3 = [20 x i8] c"?? Can't read = %d\0A\00"
@.str.4 = [25 x i8] c"?? Different operations\0A\00"
define i32 @write_buffer(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [17 x i8]
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 -1, i32* %8
%11 = load i8*, i8** %6
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %33
13: ; preds = %3
%14 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%15 = load i8*, i8** %6
%16 = call i32 @memcpy(i8* %14, i8* %15, i32 16)
%17 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%18 = call signext i8 @check_code(i8* %17, i32 16)
%19 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 16
store i8 %18, i8* %19
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%21 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%22 = call i32 @dump_buffer(i8* %21)
%23 = load i32, i32* %5
%24 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%25 = call i32 @write(i32 %23, i8* %24, i32 17)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %13
%29 = load i32, i32* %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %81
32: ; preds = %13
br label %33
33: ; preds = %32, %3
%34 = load i8*, i8** %7
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %79
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%38 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%39 = call i32 @memset(i8* %38, i32 0, i32 17)
store i32 0, i32* %10
br label %40
40: ; preds = %58, %36
%41 = load i32, i32* %10
%42 = icmp slt i32 %41, 17
br i1 %42, label %43, label %62
43: ; preds = %40
%44 = load i32, i32* %5
%45 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%46 = load i32, i32* %10
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i32, i32* %10
%50 = sub nsw i32 17, %49
%51 = call i32 @read(i32 %44, i8* %48, i32 %50)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %43
%55 = load i32, i32* %8
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %81
58: ; preds = %43
%59 = load i32, i32* %8
%60 = load i32, i32* %10
%61 = add nsw i32 %60, %59
store i32 %61, i32* %10
br label %40
62: ; preds = %40
%63 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%64 = call i32 @dump_buffer(i8* %63)
%65 = load i8*, i8** %6
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %74
67: ; preds = %62
%68 = load i8*, i8** %6
%69 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%70 = call i64 @memcmp(i8* %68, i8* %69, i32 2)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %67
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %74
74: ; preds = %72, %67, %62
%75 = load i8*, i8** %7
%76 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%77 = call i32 @memcpy(i8* %75, i8* %76, i32 17)
%78 = load i32, i32* %10
store i32 %78, i32* %4
br label %81
79: ; preds = %33
%80 = load i32, i32* %8
store i32 %80, i32* %4
br label %81
81: ; preds = %79, %74, %54, %28
%82 = load i32, i32* %4
ret i32 %82
}

LLVM IR (-O2):
@.str = [3 x i8] c"->\00"
@.str.1 = [30 x i8] c"?? Can't write, result is %d\0A\00"
@.str.2 = [3 x i8] c"<-\00"
@.str.3 = [20 x i8] c"?? Can't read = %d\0A\00"
@str = [24 x i8] c"?? Different operations\00"
define i32 @write_buffer(i32 %0, i8* %1, i8* %2) {
%4 = alloca [17 x i8]
%5 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%6 = icmp eq i8* %1, null
br i1 %6, label %17, label %7
7: ; preds = %3
%8 = call i32 @memcpy(i8* %5, i8* %1, i32 16)
%9 = call signext i8 @check_code(i8* %5, i32 16)
%10 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 16
store i8 %9, i8* %10
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @dump_buffer(i8* %5)
%13 = call i32 @write(i32 %0, i8* %5, i32 17)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %44
17: ; preds = %7, %3
%18 = phi i32 [ %13, %7 ], [ -1, %3 ]
%19 = icmp eq i8* %2, null
br i1 %19, label %44, label %20
20: ; preds = %17
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @memset(i8* %5, i32 0, i32 17)
br label %23
23: ; preds = %20, %32
%24 = phi i32 [ 0, %20 ], [ %33, %32 ]
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 %25
%27 = sub nsw i32 17, %24
%28 = call i32 @read(i32 %0, i8* %26, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %28)
br label %44
32: ; preds = %23
%33 = add nsw i32 %28, %24
%34 = icmp slt i32 %33, 17
br i1 %34, label %23, label %35
35: ; preds = %32
%36 = call i32 @dump_buffer(i8* %5)
br i1 %6, label %42, label %37
37: ; preds = %35
%38 = call i64 @memcmp(i8* %1, i8* %5, i32 2)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %42
42: ; preds = %40, %37, %35
%43 = call i32 @memcpy(i8* %2, i8* %5, i32 17)
br label %44
44: ; preds = %17, %30, %42, %15
%45 = phi i32 [ %13, %15 ], [ %28, %30 ], [ %33, %42 ], [ %18, %17 ]
ret i32 %45
}