#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 */
typedef int uint64_t;
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ convert_to_phi(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
int process_buffer(int /*<<< orphan*/ *, int, int);
int read(int /*<<< orphan*/, char *, int);
void read_from_stdin(void) {
  char buffer[65536 * 2];
  int n;
  int used = 0;
  uint64_t offset = 0;
  while ((n = read(0, buffer + used, sizeof(buffer) - used)) > 0) {
    int processed;
    convert_to_phi((uint16_t *)(buffer + (used & ~1)), ((used & 1) + n) / 2);
    used += n;
    processed = process_buffer((uint16_t *)buffer, used / 2, offset);
    used -= processed * 2;
    offset += processed;
    if (used > 0) {
      memmove(buffer, buffer + processed * 2, used);
    }
  }
}


x86 assembly (clang, -O0):
<read_from_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20010,%rsp
movl   $0x0,-0x20008(%rbp)
movl   $0x0,-0x2000c(%rbp)
lea    -0x20000(%rbp),%rsi
movslq -0x20008(%rbp),%rax
add    %rax,%rsi
movslq -0x20008(%rbp),%rcx
mov    $0x20000,%eax
sub    %rcx,%rax
mov    %eax,%edx
xor    %edi,%edi
call   1030 <read@plt>
mov    %eax,-0x20004(%rbp)
cmp    $0x0,%eax
jle    1266 <read_from_stdin+0x126>
lea    -0x20000(%rbp),%rdi
mov    -0x20008(%rbp),%eax
and    $0xfffffffe,%eax
cltq
add    %rax,%rdi
mov    -0x20008(%rbp),%eax
and    $0x1,%eax
add    -0x20004(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
call   1050 <convert_to_phi@plt>
mov    -0x20004(%rbp),%eax
add    -0x20008(%rbp),%eax
mov    %eax,-0x20008(%rbp)
lea    -0x20000(%rbp),%rdi
mov    -0x20008(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%esi
mov    -0x2000c(%rbp),%edx
call   1040 <process_buffer@plt>
mov    %eax,-0x20010(%rbp)
mov    -0x20010(%rbp),%ecx
shl    $0x1,%ecx
mov    -0x20008(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20008(%rbp)
mov    -0x20010(%rbp),%eax
add    -0x2000c(%rbp),%eax
mov    %eax,-0x2000c(%rbp)
cmpl   $0x0,-0x20008(%rbp)
jle    1261 <read_from_stdin+0x121>
lea    -0x20000(%rbp),%rdi
lea    -0x20000(%rbp),%rsi
mov    -0x20010(%rbp),%eax
shl    $0x1,%eax
cltq
add    %rax,%rsi
mov    -0x20008(%rbp),%edx
call   1060 <memmove@plt>
jmp    115f <read_from_stdin+0x1f>
add    $0x20010,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_stdin>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20000,%rsp
xor    %ebx,%ebx
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x20000,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    11dd <read_from_stdin+0x9d>
mov    %eax,%ebp
mov    %rsp,%r14
xor    %r15d,%r15d
jmp    1191 <read_from_stdin+0x51>
xchg   %ax,%ax
add    %r15d,%r12d
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rsi
mov    $0x20000,%edx
sub    %ebx,%edx
xor    %edi,%edi
call   1030 <read@plt>
mov    %eax,%ebp
mov    %r12d,%r15d
test   %eax,%eax
jle    11dd <read_from_stdin+0x9d>
mov    %ebx,%eax
and    $0xfffffffe,%eax
cltq
lea    (%rsp,%rax,1),%rdi
mov    %ebx,%esi
and    $0x1,%esi
add    %ebp,%esi
shr    %esi
call   1050 <convert_to_phi@plt>
add    %ebp,%ebx
mov    %ebx,%esi
shr    $0x1f,%esi
add    %ebx,%esi
sar    %esi
mov    %r14,%rdi
mov    %r15d,%edx
call   1040 <process_buffer@plt>
mov    %eax,%r12d
lea    (%r12,%r12,1),%eax
sub    %eax,%ebx
jle    1170 <read_from_stdin+0x30>
cltq
lea    (%rsp,%rax,1),%rsi
mov    %r14,%rdi
mov    %ebx,%edx
call   1060 <memmove@plt>
jmp    1170 <read_from_stdin+0x30>
add    $0x20000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @read_from_stdin() {
%1 = alloca [131072 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %3
store i32 0, i32* %4
br label %6
6: ; preds = %57, %0
%7 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 0
%8 = load i32, i32* %3
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %7, i64 %9
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = sub i64 131072, %12
%14 = trunc i64 %13 to i32
%15 = call i32 @read(i32 0, i8* %10, i32 %14)
store i32 %15, i32* %2
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %58
17: ; preds = %6
%18 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 0
%19 = load i32, i32* %3
%20 = and i32 %19, -2
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %18, i64 %21
%23 = bitcast i8* %22 to i32*
%24 = load i32, i32* %3
%25 = and i32 %24, 1
%26 = load i32, i32* %2
%27 = add nsw i32 %25, %26
%28 = sdiv i32 %27, 2
%29 = call i32 @convert_to_phi(i32* %23, i32 %28)
%30 = load i32, i32* %2
%31 = load i32, i32* %3
%32 = add nsw i32 %31, %30
store i32 %32, i32* %3
%33 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 0
%34 = bitcast i8* %33 to i32*
%35 = load i32, i32* %3
%36 = sdiv i32 %35, 2
%37 = load i32, i32* %4
%38 = call i32 @process_buffer(i32* %34, i32 %36, i32 %37)
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = mul nsw i32 %39, 2
%41 = load i32, i32* %3
%42 = sub nsw i32 %41, %40
store i32 %42, i32* %3
%43 = load i32, i32* %5
%44 = load i32, i32* %4
%45 = add nsw i32 %44, %43
store i32 %45, i32* %4
%46 = load i32, i32* %3
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %57
48: ; preds = %17
%49 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 0
%50 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 0
%51 = load i32, i32* %5
%52 = mul nsw i32 %51, 2
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %50, i64 %53
%55 = load i32, i32* %3
%56 = call i32 @memmove(i8* %49, i8* %54, i32 %55)
br label %57
57: ; preds = %48, %17
br label %6
58: ; preds = %6
ret void
}

LLVM IR (-O2):
define void @read_from_stdin() {
%1 = alloca [131072 x i8]
%2 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 0
%3 = call i32 @read(i32 0, i8* %2, i32 131072)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %36
5: ; preds = %0
%6 = bitcast [131072 x i8]* %1 to i32*
br label %7
7: ; preds = %5, %30
%8 = phi i32 [ %3, %5 ], [ %34, %30 ]
%9 = phi i32 [ 0, %5 ], [ %23, %30 ]
%10 = phi i32 [ 0, %5 ], [ %24, %30 ]
%11 = and i32 %9, -2
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 %12
%14 = bitcast i8* %13 to i32*
%15 = and i32 %9, 1
%16 = add nuw nsw i32 %15, %8
%17 = lshr i32 %16, 1
%18 = call i32 @convert_to_phi(i32* %14, i32 %17)
%19 = add nsw i32 %8, %9
%20 = sdiv i32 %19, 2
%21 = call i32 @process_buffer(i32* %6, i32 %20, i32 %10)
%22 = shl nsw i32 %21, 1
%23 = sub nsw i32 %19, %22
%24 = add nsw i32 %21, %10
%25 = icmp sgt i32 %23, 0
br i1 %25, label %26, label %30
26: ; preds = %7
%27 = sext i32 %22 to i64
%28 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 %27
%29 = call i32 @memmove(i8* %2, i8* %28, i32 %23)
br label %30
30: ; preds = %26, %7
%31 = sext i32 %23 to i64
%32 = getelementptr inbounds [131072 x i8], [131072 x i8]* %1, i64 0, i64 %31
%33 = sub i32 131072, %23
%34 = call i32 @read(i32 0, i8* %32, i32 %33)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %7, label %36
36: ; preds = %30, %0
ret void
}