#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 fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int mygetbuf(unsigned char buffer[], int *curr) {
  static int size = 0;
  if (size == 0)
    size = fread((char *)buffer, sizeof(unsigned char), 1024, stdin);
  else {
    int copy;
    for (copy = 0; *curr + copy < size; copy++)
      buffer[copy] = buffer[*curr + copy];
    size = copy + fread((char *)buffer + copy, sizeof(unsigned char),
                        1024 - copy, stdin);
  }
  if (size < 1024)
    buffer[size] = '\0';
  *curr = 0;
  return size;
}


x86 assembly (clang, -O0):
<mygetbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,0x2f05(%rip)
jne    1154 <mygetbuf+0x44>
mov    -0x8(%rbp),%rdi
mov    0x2eb0(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,0x2edd(%rip)
jmp    11d5 <mygetbuf+0xc5>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
add    -0x14(%rbp),%eax
cmp    0x2ec2(%rip),%eax
jge    119c <mygetbuf+0x8c>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
add    -0x14(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    115b <mygetbuf+0x4b>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    $0x400,%edx
sub    -0x14(%rbp),%edx
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
add    %ecx,%eax
mov    %eax,0x2e57(%rip)
cmpl   $0x400,0x2e4d(%rip)
jge    11f4 <mygetbuf+0xe4>
mov    -0x8(%rbp),%rax
movslq 0x2e3c(%rip),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
mov    0x2e28(%rip),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mygetbuf>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2f0b(%rip),%eax
test   %eax,%eax
je     1172 <mygetbuf+0x62>
mov    (%r14),%ecx
xor    %ebx,%ebx
cmp    %eax,%ecx
jge    114f <mygetbuf+0x3f>
xor    %ebx,%ebx
movslq %ecx,%rcx
movzbl (%r15,%rcx,1),%ecx
mov    %cl,(%r15,%rbx,1)
mov    (%r14),%edx
lea    (%rbx,%rdx,1),%ecx
add    $0x1,%ecx
add    $0x1,%rbx
add    %ebx,%edx
cmp    %eax,%edx
jl     1130 <mygetbuf+0x20>
mov    %ebx,%edi
add    %r15,%rdi
mov    $0x400,%edx
sub    %ebx,%edx
mov    0x2e86(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1030 <fread@plt>
add    %ebx,%eax
jmp    118d <mygetbuf+0x7d>
mov    0x2e6f(%rip),%rax
mov    (%rax),%ecx
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,0x2e99(%rip)
cmp    $0x3ff,%eax
jg     11a2 <mygetbuf+0x92>
movslq %eax,%rcx
movb   $0x0,(%r15,%rcx,1)
movl   $0x0,(%r14)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@mygetbuf.size = internal global i32 0
@stdin = global i32 0
define i32 @mygetbuf(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%6 = load i32, i32* @mygetbuf.size
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = load i8*, i8** %3
%10 = load i32, i32* @stdin
%11 = call i32 @fread(i8* %9, i32 1, i32 1024, i32 %10)
store i32 %11, i32* @mygetbuf.size
br label %47
12: ; preds = %2
store i32 0, i32* %5
br label %13
13: ; preds = %33, %12
%14 = load i32*, i32** %4
%15 = load i32, i32* %14
%16 = load i32, i32* %5
%17 = add nsw i32 %15, %16
%18 = load i32, i32* @mygetbuf.size
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %36
20: ; preds = %13
%21 = load i8*, i8** %3
%22 = load i32*, i32** %4
%23 = load i32, i32* %22
%24 = load i32, i32* %5
%25 = add nsw i32 %23, %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %21, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %3
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
br label %33
33: ; preds = %20
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
br label %13
36: ; preds = %13
%37 = load i32, i32* %5
%38 = load i8*, i8** %3
%39 = load i32, i32* %5
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i32, i32* %5
%43 = sub nsw i32 1024, %42
%44 = load i32, i32* @stdin
%45 = call i32 @fread(i8* %41, i32 1, i32 %43, i32 %44)
%46 = add nsw i32 %37, %45
store i32 %46, i32* @mygetbuf.size
br label %47
47: ; preds = %36, %8
%48 = load i32, i32* @mygetbuf.size
%49 = icmp slt i32 %48, 1024
br i1 %49, label %50, label %55
50: ; preds = %47
%51 = load i8*, i8** %3
%52 = load i32, i32* @mygetbuf.size
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
br label %55
55: ; preds = %50, %47
%56 = load i32*, i32** %4
store i32 0, i32* %56
%57 = load i32, i32* @mygetbuf.size
ret i32 %57
}

LLVM IR (-O2):
@mygetbuf.size = internal unnamed_addr global i32 0
@stdin = global i32 0
define i32 @mygetbuf(i8* %0, i32* nocapture %1) {
%3 = load i32, i32* @mygetbuf.size
%4 = icmp eq i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, %3
br i1 %7, label %11, label %25
8: ; preds = %2
%9 = load i32, i32* @stdin
%10 = tail call i32 @fread(i8* %0, i32 1, i32 1024, i32 %9)
br label %33
11: ; preds = %5, %11
%12 = phi i64 [ %18, %11 ], [ 0, %5 ]
%13 = phi i32 [ %21, %11 ], [ %6, %5 ]
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 %16, i8* %17
%18 = add nuw nsw i64 %12, 1
%19 = load i32, i32* %1
%20 = trunc i64 %18 to i32
%21 = add nsw i32 %19, %20
%22 = icmp slt i32 %21, %3
br i1 %22, label %11, label %23
23: ; preds = %11
%24 = trunc i64 %18 to i32
br label %25
25: ; preds = %23, %5
%26 = phi i32 [ 0, %5 ], [ %24, %23 ]
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = sub nsw i32 1024, %26
%30 = load i32, i32* @stdin
%31 = tail call i32 @fread(i8* %28, i32 1, i32 %29, i32 %30)
%32 = add nsw i32 %31, %26
br label %33
33: ; preds = %25, %8
%34 = phi i32 [ %32, %25 ], [ %10, %8 ]
store i32 %34, i32* @mygetbuf.size
%35 = icmp slt i32 %34, 1024
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = sext i32 %34 to i64
%38 = getelementptr inbounds i8, i8* %0, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %36, %33
store i32 0, i32* %1
ret i32 %34
}