#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 */
char EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(void *);
char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ isalpha(char);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
int stem(char *, int /*<<< orphan*/, size_t);
char tolower(char);
int /*<<< orphan*/ ungetc(char, int /*<<< orphan*/ *);
void stemfile(FILE *f) {
  char *s;
  char ch;
  size_t i;
  s = (char *)malloc(1024);
  if (s == NULL) {
    fprintf(stderr, "error: out of memory\n");
    exit(EXIT_FAILURE);
  }
  while ((ch = getc(f)) != EOF) {
    if (!isalpha(ch)) {
      (void)putchar(ch);
      continue;
    }
    i = 0;
    while (true) {
      if (i == 1024) {
        fprintf(stderr, "error: out of buffer space\n");
        exit(EXIT_FAILURE);
      }
      s[i++] = tolower(ch);
      ch = getc(f);
      if (!isalpha(ch)) {
        (void)ungetc(ch, f);
        break;
      }
    }
    s[i] = '\0';
    s[stem(s, 0, i - 1) + 1] = '\0';
    printf("%s", s);
  }
  free((void *)s);
}


x86 assembly (clang, -O0):
<stemfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x400,%edi
call   1090 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f8 <stemfile+0x48>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    11fd <stemfile+0x4d>
mov    -0x8(%rbp),%rdi
call   10d0 <getc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
mov    0x2dd5(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     131b <stemfile+0x16b>
movsbl -0x11(%rbp),%edi
call   10b0 <isalpha@plt>
cmp    $0x0,%eax
jne    123e <stemfile+0x8e>
movsbl -0x11(%rbp),%edi
call   1050 <putchar@plt>
jmp    11fd <stemfile+0x4d>
movq   $0x0,-0x20(%rbp)
cmpq   $0x400,-0x20(%rbp)
jne    1277 <stemfile+0xc7>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movsbl -0x11(%rbp),%edi
call   1080 <tolower@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsi
add    $0x1,%rsi
mov    %rsi,-0x20(%rbp)
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   10d0 <getc@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%edi
call   10b0 <isalpha@plt>
cmp    $0x0,%eax
jne    12ca <stemfile+0x11a>
mov    -0x11(%rbp),%al
mov    -0x8(%rbp),%rsi
movsbl %al,%edi
call   10a0 <ungetc@plt>
jmp    12cf <stemfile+0x11f>
jmp    1246 <stemfile+0x96>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rdx
sub    $0x1,%rdx
xor    %esi,%esi
call   1030 <stem@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xd21(%rip),%rdi
call   1060 <printf@plt>
jmp    11fd <stemfile+0x4d>
mov    -0x10(%rbp),%rdi
call   1040 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stemfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x400,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     12d0 <stemfile+0x120>
mov    %rax,%r12
mov    %r14,%rdi
call   10d0 <getc@plt>
mov    0x2e05(%rip),%r15
cmp    (%r15),%al
jne    11fe <stemfile+0x4e>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <free@plt>
mov    %eax,%r13d
jmp    1227 <stemfile+0x77>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
call   1050 <putchar@plt>
mov    %r14,%rdi
call   10d0 <getc@plt>
mov    %eax,%r13d
cmp    (%r15),%al
je     11e8 <stemfile+0x38>
movsbl %r13b,%ebx
mov    %ebx,%edi
call   10b0 <isalpha@plt>
test   %eax,%eax
je     1210 <stemfile+0x60>
mov    $0xffffffffffffffff,%rbx
nopl   (%rax)
cmp    $0x3ff,%rbx
je     12ad <stemfile+0xfd>
movsbl %r13b,%edi
call   1080 <tolower@plt>
mov    %al,0x1(%r12,%rbx,1)
mov    %r14,%rdi
call   10d0 <getc@plt>
mov    %eax,%r13d
movsbl %al,%ebp
mov    %ebp,%edi
call   10b0 <isalpha@plt>
add    $0x1,%rbx
test   %eax,%eax
jne    1240 <stemfile+0x90>
mov    %ebp,%edi
mov    %r14,%rsi
call   10a0 <ungetc@plt>
movb   $0x0,0x1(%r12,%rbx,1)
mov    %r12,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1030 <stem@plt>
cltq
movb   $0x0,0x1(%rax,%r12,1)
lea    0xd92(%rip),%rdi
mov    %r12,%rsi
call   1060 <printf@plt>
jmp    1217 <stemfile+0x67>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
lea    0xd59(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
lea    0xd20(%rip),%rsi
jmp    12bd <stemfile+0x10d>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"error: out of memory\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
@.str.1 = [28 x i8] c"error: out of buffer space\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define void @stemfile(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i64
store i32* %0, i32** %2
%6 = call i64 @malloc(i32 1024)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %15
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %1
br label %16
16: ; preds = %56, %27, %15
%17 = load i32*, i32** %2
%18 = call signext i8 @getc(i32* %17)
store i8 %18, i8* %4
%19 = sext i8 %18 to i32
%20 = load i8, i8* @EOF
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %19, %21
br i1 %22, label %23, label %70
23: ; preds = %16
%24 = load i8, i8* %4
%25 = call i32 @isalpha(i8 signext %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i8, i8* %4
%29 = call i32 @putchar(i8 signext %28)
br label %16
30: ; preds = %23
store i64 0, i64* %5
br label %31
31: ; preds = %30, %55
%32 = load i64, i64* %5
%33 = icmp eq i64 %32, 1024
br i1 %33, label %34, label %39
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %31
%40 = load i8, i8* %4
%41 = call signext i8 @tolower(i8 signext %40)
%42 = load i8*, i8** %3
%43 = load i64, i64* %5
%44 = add i64 %43, 1
store i64 %44, i64* %5
%45 = getelementptr inbounds i8, i8* %42, i64 %43
store i8 %41, i8* %45
%46 = load i32*, i32** %2
%47 = call signext i8 @getc(i32* %46)
store i8 %47, i8* %4
%48 = load i8, i8* %4
%49 = call i32 @isalpha(i8 signext %48)
%50 = icmp ne i32 %49, 0
br i1 %50, label %55, label %51
51: ; preds = %39
%52 = load i8, i8* %4
%53 = load i32*, i32** %2
%54 = call i32 @ungetc(i8 signext %52, i32* %53)
br label %56
55: ; preds = %39
br label %31
56: ; preds = %51
%57 = load i8*, i8** %3
%58 = load i64, i64* %5
%59 = getelementptr inbounds i8, i8* %57, i64 %58
store i8 0, i8* %59
%60 = load i8*, i8** %3
%61 = load i8*, i8** %3
%62 = load i64, i64* %5
%63 = sub i64 %62, 1
%64 = call i32 @stem(i8* %61, i32 0, i64 %63)
%65 = add nsw i32 %64, 1
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %60, i64 %66
store i8 0, i8* %67
%68 = load i8*, i8** %3
%69 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %68)
br label %16
70: ; preds = %16
%71 = load i8*, i8** %3
%72 = call i32 @free(i8* %71)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"error: out of memory\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i8 0
@.str.1 = [28 x i8] c"error: out of buffer space\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define void @stemfile(i32* nocapture %0) {
%2 = tail call i64 @malloc(i32 1024)
%3 = inttoptr i64 %2 to i8*
%4 = icmp eq i64 %2, 0
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = tail call signext i8 @getc(i32* %0)
%7 = load i8, i8* @EOF
%8 = icmp eq i8 %6, %7
br i1 %8, label %48, label %14
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %5, %20
%15 = phi i8 [ %21, %20 ], [ %6, %5 ]
%16 = tail call i32 @isalpha(i8 signext %15)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = tail call i32 @putchar(i8 signext %15)
br label %20
20: ; preds = %18, %40
%21 = tail call signext i8 @getc(i32* %0)
%22 = load i8, i8* @EOF
%23 = icmp eq i8 %21, %22
br i1 %23, label %48, label %14
24: ; preds = %14, %33
%25 = phi i8 [ %37, %33 ], [ %15, %14 ]
%26 = phi i64 [ %35, %33 ], [ 0, %14 ]
%27 = icmp eq i64 %26, 1024
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = tail call signext i8 @tolower(i8 signext %25)
%35 = add nuw nsw i64 %26, 1
%36 = getelementptr inbounds i8, i8* %3, i64 %26
store i8 %34, i8* %36
%37 = tail call signext i8 @getc(i32* %0)
%38 = tail call i32 @isalpha(i8 signext %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %24
40: ; preds = %33
%41 = tail call i32 @ungetc(i8 signext %37, i32* %0)
%42 = getelementptr inbounds i8, i8* %3, i64 %35
store i8 0, i8* %42
%43 = tail call i32 @stem(i8* %3, i32 0, i64 %26)
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %3, i64 %45
store i8 0, i8* %46
%47 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %3)
br label %20
48: ; preds = %20, %5
%49 = tail call i32 @free(i8* %3)
ret void
}