#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 */
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int ntohs(short);
scalar_t__ print_encoded_msg(int, char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int coded_buffered_printer(int infd) {
  int i, lastlast;
  char spaces[50];
  static char mybuffer[1500];
  static int size = 0, last = 0;
  memcpy(spaces, " ", 2);
  do {
    lastlast = 1500 - last;
    i = read(infd, &mybuffer[last], lastlast);
    printf("read i=%d\n", i);
    if (i == 0)
      break;
    if (size == 0) {
      size =
          (ntohs(((short *)(mybuffer))[1]) + ntohs(((short *)(mybuffer))[2]));
      printf("size=%d\n", size);
      last += i;
    }
    if (last >= size) {
      printf("should print message: last=%d, size=%d\n", last, size);
      if (print_encoded_msg(1, mybuffer, spaces) < 0) {
        printf("Unable to print encoded msg\n");
        return -1;
      }
      if (last > size) {
        memmove(mybuffer, &mybuffer[size], last - size);
        last = last - size;
      } else
        last = 0;
      size = 0;
    }
  } while (i > 0 && i == lastlast);
  if (i == 0)
    return 0;
  else
    return 1;
}


x86 assembly (clang, -O0):
<coded_buffered_printer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe8a(%rip),%rsi
mov    $0x2,%edx
call   1050 <memcpy@plt>
mov    $0x5dc,%eax
sub    0x34b5(%rip),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
movslq 0x34a8(%rip),%rax
lea    0x2ec1(%rip),%rsi
add    %rax,%rsi
mov    -0x10(%rbp),%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    11cd <coded_buffered_printer+0x6d>
jmp    131b <coded_buffered_printer+0x1bb>
cmpl   $0x0,0x3468(%rip)
jne    1225 <coded_buffered_printer+0xc5>
movswl 0x2e81(%rip),%edi
call   1060 <ntohs@plt>
mov    %eax,-0x54(%rbp)
movswl 0x2e74(%rip),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
add    %ecx,%eax
mov    %eax,0x343a(%rip)
mov    0x3434(%rip),%esi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    0x3421(%rip),%eax
mov    %eax,0x341b(%rip)
mov    0x3415(%rip),%eax
cmp    0x340b(%rip),%eax
jl     12f0 <coded_buffered_printer+0x190>
mov    0x3403(%rip),%esi
mov    0x33f9(%rip),%edx
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x50(%rbp),%rdx
mov    $0x1,%edi
lea    0x2dff(%rip),%rsi
call   1080 <print_encoded_msg@plt>
cmp    $0x0,%rax
jge    128a <coded_buffered_printer+0x12a>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <coded_buffered_printer+0x1d8>
mov    0x33b0(%rip),%eax
cmp    0x33a6(%rip),%eax
jle    12dc <coded_buffered_printer+0x17c>
movslq 0x3399(%rip),%rax
lea    0x2db6(%rip),%rsi
add    %rax,%rsi
mov    0x338d(%rip),%edx
sub    0x3383(%rip),%edx
lea    0x2da0(%rip),%rdi
call   1070 <memmove@plt>
mov    0x3375(%rip),%eax
sub    0x336b(%rip),%eax
mov    %eax,0x3369(%rip)
jmp    12e6 <coded_buffered_printer+0x186>
movl   $0x0,0x335a(%rip)
movl   $0x0,0x334c(%rip)
jmp    12f5 <coded_buffered_printer+0x195>
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x55(%rbp)
jle    1310 <coded_buffered_printer+0x1b0>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
sete   %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    1180 <coded_buffered_printer+0x20>
cmpl   $0x0,-0xc(%rbp)
jne    1331 <coded_buffered_printer+0x1d1>
movl   $0x0,-0x4(%rbp)
jmp    1338 <coded_buffered_printer+0x1d8>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<coded_buffered_printer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebp
lea    0xe79(%rip),%rsi
mov    %rsp,%rdi
mov    $0x2,%edx
call   1060 <memcpy@plt>
mov    0x34b6(%rip),%esi
lea    0x2ecf(%rip),%rbx
lea    0xe5a(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    $0x5dc,%r12d
sub    %esi,%r12d
movslq %esi,%rsi
add    %rbx,%rsi
mov    %ebp,%edi
mov    %r12d,%edx
call   1050 <read@plt>
mov    %eax,%r13d
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r13d,%r13d
je     12bf <coded_buffered_printer+0x14f>
mov    0x3465(%rip),%edx
test   %edx,%edx
je     1200 <coded_buffered_printer+0x90>
mov    0x345f(%rip),%esi
cmp    %edx,%esi
jge    124f <coded_buffered_printer+0xdf>
jmp    12b1 <coded_buffered_printer+0x141>
nopw   0x0(%rax,%rax,1)
movswl 0x2e6b(%rip),%edi
call   1070 <ntohs@plt>
mov    %eax,%r14d
movswl 0x2e5e(%rip),%edi
call   1070 <ntohs@plt>
add    %eax,%r14d
mov    %r14d,0x3427(%rip)
lea    0xe00(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x3414(%rip),%esi
add    %r13d,%esi
mov    %esi,0x340b(%rip)
mov    0x3401(%rip),%edx
cmp    %edx,%esi
jl     12b1 <coded_buffered_printer+0x141>
lea    0xdb7(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %rsp,%rdx
call   1090 <print_encoded_msg@plt>
test   %rax,%rax
js     12c9 <coded_buffered_printer+0x159>
mov    0x33d8(%rip),%edx
movslq 0x33cd(%rip),%rax
xor    %esi,%esi
cmp    %eax,%edx
jle    12a1 <coded_buffered_printer+0x131>
mov    %rax,%rsi
add    %rbx,%rsi
sub    %eax,%edx
mov    %rbx,%rdi
call   1080 <memmove@plt>
mov    0x33b5(%rip),%esi
sub    0x33ab(%rip),%esi
mov    %esi,0x33a9(%rip)
movl   $0x0,0x339b(%rip)
test   %r13d,%r13d
jle    12bf <coded_buffered_printer+0x14f>
cmp    %r12d,%r13d
je     11b0 <coded_buffered_printer+0x40>
xor    %eax,%eax
test   %r13d,%r13d
setne  %al
jmp    12da <coded_buffered_printer+0x16a>
lea    0xd65(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@coded_buffered_printer.mybuffer = internal global [1500 x i8] zeroinitializer
@coded_buffered_printer.size = internal global i32 0
@coded_buffered_printer.last = internal global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [11 x i8] c"read i=%d\0A\00"
@.str.2 = [9 x i8] c"size=%d\0A\00"
@.str.3 = [40 x i8] c"should print message: last=%d, size=%d\0A\00"
@.str.4 = [29 x i8] c"Unable to print encoded msg\0A\00"
define i32 @coded_buffered_printer(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [50 x i8]
store i32 %0, i32* %3
%7 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%8 = call i32 @memcpy(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 2)
br label %9
9: ; preds = %75, %1
%10 = load i32, i32* @coded_buffered_printer.last
%11 = sub nsw i32 1500, %10
store i32 %11, i32* %5
%12 = load i32, i32* %3
%13 = load i32, i32* @coded_buffered_printer.last
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 %14
%16 = load i32, i32* %5
%17 = call i32 @read(i32 %12, i8* %15, i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %9
br label %77
23: ; preds = %9
%24 = load i32, i32* @coded_buffered_printer.size
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %23
%27 = load i16, i16* getelementptr inbounds (i16, i16* bitcast ([1500 x i8]* @coded_buffered_printer.mybuffer to i16*), i64 1)
%28 = call i32 @ntohs(i16 signext %27)
%29 = load i16, i16* getelementptr inbounds (i16, i16* bitcast ([1500 x i8]* @coded_buffered_printer.mybuffer to i16*), i64 2)
%30 = call i32 @ntohs(i16 signext %29)
%31 = add nsw i32 %28, %30
store i32 %31, i32* @coded_buffered_printer.size
%32 = load i32, i32* @coded_buffered_printer.size
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %4
%35 = load i32, i32* @coded_buffered_printer.last
%36 = add nsw i32 %35, %34
store i32 %36, i32* @coded_buffered_printer.last
br label %37
37: ; preds = %26, %23
%38 = load i32, i32* @coded_buffered_printer.last
%39 = load i32, i32* @coded_buffered_printer.size
%40 = icmp sge i32 %38, %39
br i1 %40, label %41, label %67
41: ; preds = %37
%42 = load i32, i32* @coded_buffered_printer.last
%43 = load i32, i32* @coded_buffered_printer.size
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %42, i32 %43)
%45 = getelementptr inbounds [50 x i8], [50 x i8]* %6, i64 0, i64 0
%46 = call i64 @print_encoded_msg(i32 1, i8* getelementptr inbounds ([1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 0), i8* %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %82
50: ; preds = %41
%51 = load i32, i32* @coded_buffered_printer.last
%52 = load i32, i32* @coded_buffered_printer.size
%53 = icmp sgt i32 %51, %52
br i1 %53, label %54, label %65
54: ; preds = %50
%55 = load i32, i32* @coded_buffered_printer.size
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds [1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 %56
%58 = load i32, i32* @coded_buffered_printer.last
%59 = load i32, i32* @coded_buffered_printer.size
%60 = sub nsw i32 %58, %59
%61 = call i32 @memmove(i8* getelementptr inbounds ([1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 0), i8* %57, i32 %60)
%62 = load i32, i32* @coded_buffered_printer.last
%63 = load i32, i32* @coded_buffered_printer.size
%64 = sub nsw i32 %62, %63
store i32 %64, i32* @coded_buffered_printer.last
br label %66
65: ; preds = %50
store i32 0, i32* @coded_buffered_printer.last
br label %66
66: ; preds = %65, %54
store i32 0, i32* @coded_buffered_printer.size
br label %67
67: ; preds = %66, %37
br label %68
68: ; preds = %67
%69 = load i32, i32* %4
%70 = icmp sgt i32 %69, 0
br i1 %70, label %71, label %75
71: ; preds = %68
%72 = load i32, i32* %4
%73 = load i32, i32* %5
%74 = icmp eq i32 %72, %73
br label %75
75: ; preds = %71, %68
%76 = phi i1 [ false, %68 ], [ %74, %71 ]
br i1 %76, label %9, label %77
77: ; preds = %75, %22
%78 = load i32, i32* %4
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %77
store i32 0, i32* %2
br label %82
81: ; preds = %77
store i32 1, i32* %2
br label %82
82: ; preds = %81, %80, %48
%83 = load i32, i32* %2
ret i32 %83
}

LLVM IR (-O2):
@coded_buffered_printer.mybuffer = internal global [1500 x i8] zeroinitializer
@coded_buffered_printer.size = internal unnamed_addr global i32 0
@coded_buffered_printer.last = internal unnamed_addr global i32 0
@.str = [2 x i8] c" \00"
@.str.1 = [11 x i8] c"read i=%d\0A\00"
@.str.2 = [9 x i8] c"size=%d\0A\00"
@.str.3 = [40 x i8] c"should print message: last=%d, size=%d\0A\00"
@str = [28 x i8] c"Unable to print encoded msg\00"
define i32 @coded_buffered_printer(i32 %0) {
%2 = alloca [50 x i8]
%3 = getelementptr inbounds [50 x i8], [50 x i8]* %2, i64 0, i64 0
%4 = call i32 @memcpy(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 2)
%5 = load i32, i32* @coded_buffered_printer.last
br label %6
6: ; preds = %53, %1
%7 = phi i32 [ %54, %53 ], [ %5, %1 ]
%8 = sub nsw i32 1500, %7
%9 = sext i32 %7 to i64
%10 = getelementptr inbounds [1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 %9
%11 = call i32 @read(i32 %0, i8* %10, i32 %8)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %11, 0
br i1 %13, label %58, label %14
14: ; preds = %6
%15 = load i32, i32* @coded_buffered_printer.size
%16 = icmp eq i32 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = load i32, i32* @coded_buffered_printer.last
br label %29
19: ; preds = %14
%20 = load i16, i16* bitcast (i8* getelementptr inbounds ([1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 2) to i16*)
%21 = call i32 @ntohs(i16 signext %20)
%22 = load i16, i16* bitcast (i8* getelementptr inbounds ([1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 4) to i16*)
%23 = call i32 @ntohs(i16 signext %22)
%24 = add nsw i32 %23, %21
store i32 %24, i32* @coded_buffered_printer.size
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @coded_buffered_printer.last
%27 = add nsw i32 %26, %11
store i32 %27, i32* @coded_buffered_printer.last
%28 = load i32, i32* @coded_buffered_printer.size
br label %29
29: ; preds = %17, %19
%30 = phi i32 [ %15, %17 ], [ %28, %19 ]
%31 = phi i32 [ %18, %17 ], [ %27, %19 ]
%32 = icmp slt i32 %31, %30
br i1 %32, label %53, label %33
33: ; preds = %29
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %31, i32 %30)
%35 = call i64 @print_encoded_msg(i32 1, i8* getelementptr inbounds ([1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 0), i8* %3)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %61
39: ; preds = %33
%40 = load i32, i32* @coded_buffered_printer.last
%41 = load i32, i32* @coded_buffered_printer.size
%42 = icmp sgt i32 %40, %41
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = sext i32 %41 to i64
%45 = getelementptr inbounds [1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 %44
%46 = sub nsw i32 %40, %41
%47 = call i32 @memmove(i8* getelementptr inbounds ([1500 x i8], [1500 x i8]* @coded_buffered_printer.mybuffer, i64 0, i64 0), i8* %45, i32 %46)
%48 = load i32, i32* @coded_buffered_printer.last
%49 = load i32, i32* @coded_buffered_printer.size
%50 = sub nsw i32 %48, %49
br label %51
51: ; preds = %39, %43
%52 = phi i32 [ %50, %43 ], [ 0, %39 ]
store i32 %52, i32* @coded_buffered_printer.last
store i32 0, i32* @coded_buffered_printer.size
br label %53
53: ; preds = %29, %51
%54 = phi i32 [ %31, %29 ], [ %52, %51 ]
%55 = icmp sgt i32 %11, 0
%56 = icmp eq i32 %11, %8
%57 = and i1 %55, %56
br i1 %57, label %6, label %58
58: ; preds = %6, %53
%59 = xor i1 %13, true
%60 = zext i1 %59 to i32
br label %61
61: ; preds = %58, %37
%62 = phi i32 [ -1, %37 ], [ %60, %58 ]
ret i32 %62
}