#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 /*<<< orphan*/ Die(char *, ...);
int /*<<< orphan*/ IsInt(char *);
int /*<<< orphan*/ IsReal(char *);
char *fgets(char *, int, int /*<<< orphan*/ *);
char *strchr(char *, char);
int /*<<< orphan*/ strlen(char *);
char *strtok(char *, char *);
char *getword(FILE *fp, int type) {
  static char buffer[512];
  static char *sptr = NULL;
  if (sptr != NULL)
    sptr = strtok(NULL, " \t\n");
  while (sptr == NULL) {
    if ((sptr = fgets(buffer, 512, fp)) == NULL)
      return NULL;
    if ((sptr = strchr(buffer, '#')) != NULL)
      *sptr = '\0';
    sptr = strtok(buffer, " \t\n");
  }
  switch (type) {
  case 1:
    if (strlen(sptr) == 0)
      Die("Parse of prior failed: expected string, got nothing");
    break;
  case 0:
    if (!IsInt(sptr))
      Die("Parse of prior failed: expected integer, got %s", sptr);
    break;
  case 2:
    if (!IsReal(sptr))
      Die("Parse of prior failed: expected real value, got %s", sptr);
    break;
  }
  return sptr;
}


x86 assembly (clang, -O0):
<getword>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,0x30e9(%rip)
je     11a4 <getword+0x34>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe68(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,0x30cc(%rip)
jmp    11a9 <getword+0x39>
cmpq   $0x0,0x30bf(%rip)
jne    1235 <getword+0xc5>
mov    -0x10(%rbp),%rdx
lea    0x2eae(%rip),%rdi
mov    $0x200,%esi
call   1050 <fgets@plt>
mov    %rax,0x309d(%rip)
cmp    $0x0,%rax
jne    11ea <getword+0x7a>
movq   $0x0,-0x8(%rbp)
jmp    12fb <getword+0x18b>
lea    0x2e7f(%rip),%rdi
mov    $0x23,%esi
call   1040 <strchr@plt>
mov    %rax,0x306e(%rip)
cmp    $0x0,%rax
je     1216 <getword+0xa6>
mov    0x305d(%rip),%rax
movb   $0x0,(%rax)
lea    0x2e53(%rip),%rdi
lea    0xddc(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,0x3040(%rip)
jmp    11a9 <getword+0x39>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
test   %eax,%eax
je     1292 <getword+0x122>
jmp    1248 <getword+0xd8>
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
je     126a <getword+0xfa>
jmp    1259 <getword+0xe9>
mov    -0x18(%rbp),%eax
sub    $0x2,%eax
je     12c1 <getword+0x151>
jmp    12f0 <getword+0x180>
mov    0x2fff(%rip),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    128d <getword+0x11d>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1060 <Die@plt>
jmp    12f0 <getword+0x180>
mov    0x2fd7(%rip),%rdi
call   1080 <IsInt@plt>
cmp    $0x0,%eax
jne    12bc <getword+0x14c>
mov    0x2fc2(%rip),%rsi
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1060 <Die@plt>
jmp    12f0 <getword+0x180>
mov    0x2fa8(%rip),%rdi
call   1090 <IsReal@plt>
cmp    $0x0,%eax
jne    12eb <getword+0x17b>
mov    0x2f93(%rip),%rsi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1060 <Die@plt>
jmp    12f0 <getword+0x180>
mov    0x2f79(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getword>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
cmpq   $0x0,0x30ea(%rip)
je     11e2 <getword+0x72>
lea    0xe71(%rip),%rsi
xor    %edi,%edi
call   1070 <strtok@plt>
mov    %rax,0x30d3(%rip)
test   %rax,%rax
je     11e2 <getword+0x72>
test   %r14d,%r14d
je     1251 <getword+0xe1>
cmp    $0x2,%r14d
je     126d <getword+0xfd>
cmp    $0x1,%r14d
jne    128e <getword+0x11e>
mov    %rax,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jne    128e <getword+0x11e>
lea    0xe2e(%rip),%rdi
xor    %eax,%eax
call   1060 <Die@plt>
jmp    128e <getword+0x11e>
lea    0x2e87(%rip),%rbp
xor    %r15d,%r15d
lea    0xe0d(%rip),%r12
jmp    1217 <getword+0xa7>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <strtok@plt>
mov    %rax,0x305e(%rip)
test   %rax,%rax
jne    11a2 <getword+0x32>
mov    %rbp,%rdi
mov    $0x200,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
mov    %rax,0x3042(%rip)
test   %rax,%rax
je     1295 <getword+0x125>
mov    %rbp,%rdi
mov    $0x23,%esi
call   1040 <strchr@plt>
mov    %rax,0x3029(%rip)
test   %rax,%rax
je     1200 <getword+0x90>
movb   $0x0,(%rax)
jmp    1200 <getword+0x90>
mov    %rax,%rdi
call   1080 <IsInt@plt>
test   %eax,%eax
jne    128e <getword+0x11e>
mov    0x300c(%rip),%rsi
lea    0xdcd(%rip),%rdi
jmp    1287 <getword+0x117>
mov    %rax,%rdi
call   1090 <IsReal@plt>
test   %eax,%eax
jne    128e <getword+0x11e>
mov    0x2ff0(%rip),%rsi
lea    0xde1(%rip),%rdi
xor    %eax,%eax
call   1060 <Die@plt>
mov    0x2fdb(%rip),%r15
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@getword.buffer = internal global [512 x i8] zeroinitializer
@getword.sptr = internal global i8* null
@.str = [4 x i8] c" \09\0A\00"
@.str.1 = [52 x i8] c"Parse of prior failed: expected string, got nothing\00"
@.str.2 = [48 x i8] c"Parse of prior failed: expected integer, got %s\00"
@.str.3 = [51 x i8] c"Parse of prior failed: expected real value, got %s\00"
define i8* @getword(i32* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%6 = load i8*, i8** @getword.sptr
%7 = icmp ne i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** @getword.sptr
br label %10
10: ; preds = %8, %2
br label %11
11: ; preds = %24, %10
%12 = load i8*, i8** @getword.sptr
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %26
14: ; preds = %11
%15 = load i32*, i32** %4
%16 = call i8* @fgets(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @getword.buffer, i64 0, i64 0), i32 512, i32* %15)
store i8* %16, i8** @getword.sptr
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %14
store i8* null, i8** %3
br label %53
19: ; preds = %14
%20 = call i8* @strchr(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @getword.buffer, i64 0, i64 0), i8 signext 35)
store i8* %20, i8** @getword.sptr
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = load i8*, i8** @getword.sptr
store i8 0, i8* %23
br label %24
24: ; preds = %22, %19
%25 = call i8* @strtok(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @getword.buffer, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** @getword.sptr
br label %11
26: ; preds = %11
%27 = load i32, i32* %5
switch i32 %27, label %51 [
i32 1, label %28
i32 0, label %35
i32 2, label %43
]
28: ; preds = %26
%29 = load i8*, i8** @getword.sptr
%30 = call i32 @strlen(i8* %29)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 (i8*, ...) @Die(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %28
br label %51
35: ; preds = %26
%36 = load i8*, i8** @getword.sptr
%37 = call i32 @IsInt(i8* %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %35
%40 = load i8*, i8** @getword.sptr
%41 = call i32 (i8*, ...) @Die(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %39, %35
br label %51
43: ; preds = %26
%44 = load i8*, i8** @getword.sptr
%45 = call i32 @IsReal(i8* %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %43
%48 = load i8*, i8** @getword.sptr
%49 = call i32 (i8*, ...) @Die(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %47, %43
br label %51
51: ; preds = %26, %50, %42, %34
%52 = load i8*, i8** @getword.sptr
store i8* %52, i8** %3
br label %53
53: ; preds = %51, %18
%54 = load i8*, i8** %3
ret i8* %54
}

LLVM IR (-O2):
@getword.buffer = internal global [512 x i8] zeroinitializer
@getword.sptr = internal unnamed_addr global i8* null
@.str = [4 x i8] c" \09\0A\00"
@.str.1 = [52 x i8] c"Parse of prior failed: expected string, got nothing\00"
@.str.2 = [48 x i8] c"Parse of prior failed: expected integer, got %s\00"
@.str.3 = [51 x i8] c"Parse of prior failed: expected real value, got %s\00"
define i8* @getword(i32* nocapture %0, i32 %1) {
%3 = load i8*, i8** @getword.sptr
%4 = icmp eq i8* %3, null
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %6, i8** @getword.sptr
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %19
8: ; preds = %2, %5
br label %9
9: ; preds = %8, %16
%10 = tail call i8* @fgets(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @getword.buffer, i64 0, i64 0), i32 512, i32* %0)
store i8* %10, i8** @getword.sptr
%11 = icmp eq i8* %10, null
br i1 %11, label %40, label %12
12: ; preds = %9
%13 = tail call i8* @strchr(i8* dereferenceable(1) getelementptr inbounds ([512 x i8], [512 x i8]* @getword.buffer, i64 0, i64 0), i8 signext 35)
store i8* %13, i8** @getword.sptr
%14 = icmp eq i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i8 0, i8* %13
br label %16
16: ; preds = %15, %12
%17 = tail call i8* @strtok(i8* getelementptr inbounds ([512 x i8], [512 x i8]* @getword.buffer, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** @getword.sptr
%18 = icmp eq i8* %17, null
br i1 %18, label %9, label %19
19: ; preds = %16, %5
%20 = phi i8* [ %6, %5 ], [ %17, %16 ]
switch i32 %1, label %38 [
i32 1, label %21
i32 0, label %26
i32 2, label %32
]
21: ; preds = %19
%22 = tail call i32 @strlen(i8* %20)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %38
24: ; preds = %21
%25 = tail call i32 (i8*, ...) @Die(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
br label %38
26: ; preds = %19
%27 = tail call i32 @IsInt(i8* %20)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i8*, i8** @getword.sptr
%31 = tail call i32 (i8*, ...) @Die(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %38
32: ; preds = %19
%33 = tail call i32 @IsReal(i8* %20)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i8*, i8** @getword.sptr
%37 = tail call i32 (i8*, ...) @Die(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.3, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %32, %35, %26, %29, %21, %24, %19
%39 = load i8*, i8** @getword.sptr
br label %40
40: ; preds = %9, %38
%41 = phi i8* [ %39, %38 ], [ null, %9 ]
ret i8* %41
}