#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
char *read_line();
int mygetc(FILE *f) {
  static char *p;
  char ch;
  if (!isatty(0)) {
    return getc(f);
  }
  if (p == NULL || *p == 0) {
    char *s = read_line();
    p = s;
  }
  ch = *p;
  p++;
  return ch;
}


x86 assembly (clang, -O0):
<mygetc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %edi,%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    115d <mygetc+0x2d>
mov    -0x10(%rbp),%rdi
call   1050 <getc@plt>
mov    %eax,-0x4(%rbp)
jmp    11b9 <mygetc+0x89>
cmpq   $0x0,0x2edb(%rip)
je     117e <mygetc+0x4e>
mov    0x2ece(%rip),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1194 <mygetc+0x64>
mov    $0x0,%al
call   1040 <read_line@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,0x2eac(%rip)
mov    0x2ea5(%rip),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
mov    0x2e99(%rip),%rax
add    $0x1,%rax
mov    %rax,0x2e8e(%rip)
movsbl -0x11(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mygetc>:
push   %rbx
mov    %rdi,%rbx
xor    %edi,%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     116a <mygetc+0x3a>
mov    0x2efa(%rip),%rax
test   %rax,%rax
je     1151 <mygetc+0x21>
mov    (%rax),%cl
test   %cl,%cl
jne    115a <mygetc+0x2a>
xor    %eax,%eax
call   1040 <read_line@plt>
mov    (%rax),%cl
add    $0x1,%rax
mov    %rax,0x2edb(%rip)
movsbl %cl,%eax
pop    %rbx
ret
mov    %rbx,%rdi
pop    %rbx
jmp    1050 <getc@plt>

LLVM IR (-O0):
@mygetc.p = internal global i8* null
define i32 @mygetc(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i8
%5 = alloca i8*
store i32* %0, i32** %3
%6 = call i32 @isatty(i32 0)
%7 = icmp ne i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %1
%9 = load i32*, i32** %3
%10 = call i32 @getc(i32* %9)
store i32 %10, i32* %2
br label %29
11: ; preds = %1
%12 = load i8*, i8** @mygetc.p
%13 = icmp eq i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = load i8*, i8** @mygetc.p
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14, %11
%20 = call i8* (...) @read_line()
store i8* %20, i8** %5
%21 = load i8*, i8** %5
store i8* %21, i8** @mygetc.p
br label %22
22: ; preds = %19, %14
%23 = load i8*, i8** @mygetc.p
%24 = load i8, i8* %23
store i8 %24, i8* %4
%25 = load i8*, i8** @mygetc.p
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** @mygetc.p
%27 = load i8, i8* %4
%28 = sext i8 %27 to i32
store i32 %28, i32* %2
br label %29
29: ; preds = %22, %8
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@mygetc.p = internal unnamed_addr global i8* null
define i32 @mygetc(i32* nocapture %0) {
%2 = tail call i32 @isatty(i32 0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @getc(i32* %0)
br label %20
6: ; preds = %1
%7 = load i8*, i8** @mygetc.p
%8 = icmp eq i8* %7, null
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = load i8, i8* %7
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %9, %6
%13 = tail call i8* (...) @read_line()
%14 = load i8, i8* %13
br label %15
15: ; preds = %12, %9
%16 = phi i8 [ %14, %12 ], [ %10, %9 ]
%17 = phi i8* [ %13, %12 ], [ %7, %9 ]
%18 = getelementptr inbounds i8, i8* %17, i64 1
store i8* %18, i8** @mygetc.p
%19 = sext i8 %16 to i32
br label %20
20: ; preds = %15, %4
%21 = phi i32 [ %19, %15 ], [ %5, %4 ]
ret i32 %21
}