#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 ssize_t;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int read(int, char **, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int fatal_get(char *name, int fd) {
  char buf[10] = {0};
  ssize_t result = read(fd, &buf, 10);
  errno = 0;
  if (result == -1 || errno != 0) {
    fprintf(stderr, "%s: read failed: %s\n", name, strerror(errno));
    exit(2);
  } else if (result >= 10) {
    fprintf(stderr, "%s: too many characters\n", name);
    exit(2);
  }
  int total = 0;
  unsigned int offset = 0;
  unsigned int current;
  while (buf[offset] != '\0') {
    current = (buf[offset] - '0');
    if (current > 9 || current < 0) {
      fprintf(stderr, "%s: unexpected character ('%lc' in '%s')\n", name,
              buf[offset], buf);
      exit(2);
    }
    total *= 10;
    total += current;
    offset++;
  }
  return total;
}


x86 assembly (clang, -O0):
<fatal_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x16(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1030 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x16(%rbp),%rsi
mov    $0xa,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e4e(%rip),%rax
movq   $0x0,(%rax)
cmpl   $0xffffffff,-0x1c(%rbp)
je     11ac <fatal_get+0x5c>
mov    0x2e36(%rip),%rax
cmpq   $0x0,(%rax)
je     11f1 <fatal_get+0xa1>
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe20(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
cmpl   $0xa,-0x1c(%rbp)
jl     1220 <fatal_get+0xd0>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
jmp    1225 <fatal_get+0xd5>
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
movsbl -0x16(%rbp,%rax,1),%eax
cmp    $0x0,%eax
je     12b5 <fatal_get+0x165>
mov    -0x24(%rbp),%eax
movsbl -0x16(%rbp,%rax,1),%eax
sub    $0x30,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x9,-0x28(%rbp)
ja     1266 <fatal_get+0x116>
cmpl   $0x0,-0x28(%rbp)
jae    1297 <fatal_get+0x147>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x24(%rbp),%eax
movsbl -0x16(%rbp,%rax,1),%ecx
lea    -0x16(%rbp),%r8
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
imul   $0xa,-0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1233 <fatal_get+0xe3>
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fatal_get>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%eax
mov    %rdi,%rbx
movw   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xa,%edx
call   1030 <read@plt>
mov    0x2e65(%rip),%rcx
movq   $0x0,(%rcx)
cmp    $0xffffffff,%eax
je     11ef <fatal_get+0xaf>
cmp    $0x9,%eax
jg     121f <fatal_get+0xdf>
mov    0x8(%rsp),%cl
xor    %eax,%eax
test   %cl,%cl
je     11bf <fatal_get+0x7f>
mov    $0x1,%edx
nopw   0x0(%rax,%rax,1)
movsbl %cl,%ecx
lea    -0x30(%rcx),%esi
cmp    $0xa,%esi
jae    11c6 <fatal_get+0x86>
lea    (%rax,%rax,4),%eax
lea    (%rsi,%rax,2),%eax
mov    %edx,%ecx
movzbl 0x8(%rsp,%rcx,1),%ecx
add    $0x1,%edx
test   %cl,%cl
jne    11a0 <fatal_get+0x60>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
lea    0x8(%rsp),%r8
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%ebp
xor    %edi,%edi
call   1060 <strerror@plt>
lea    0xdfa(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: read failed: %s\0A\00"
@.str.1 = [25 x i8] c"%s: too many characters\0A\00"
@.str.2 = [42 x i8] c"%s: unexpected character ('%lc' in '%s')\0A\00"
define i32 @fatal_get(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [10 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = bitcast [10 x i8]* %5 to i8*
%11 = load i32, i32* %4
%12 = bitcast [10 x i8]* %5 to i8**
%13 = call i32 @read(i32 %11, i8** %12, i32 10)
store i32 %13, i32* %6
store i64 0, i64* @errno
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %16, %2
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %3
%22 = load i64, i64* @errno
%23 = call i8* @strerror(i64 %22)
%24 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %21, i8* %23)
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %16
%27 = load i32, i32* %6
%28 = icmp sge i32 %27, 10
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = load i8*, i8** %3
%32 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %26
br label %35
35: ; preds = %34
store i32 0, i32* %7
store i32 0, i32* %8
br label %36
36: ; preds = %66, %35
%37 = load i32, i32* %8
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %74
43: ; preds = %36
%44 = load i32, i32* %8
%45 = zext i32 %44 to i64
%46 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = sub nsw i32 %48, 48
store i32 %49, i32* %9
%50 = load i32, i32* %9
%51 = icmp ugt i32 %50, 9
br i1 %51, label %55, label %52
52: ; preds = %43
%53 = load i32, i32* %9
%54 = icmp ult i32 %53, 0
br i1 %54, label %55, label %66
55: ; preds = %52, %43
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %3
%58 = load i32, i32* %8
%59 = zext i32 %58 to i64
%60 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = getelementptr inbounds [10 x i8], [10 x i8]* %5, i64 0, i64 0
%64 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %57, i32 %62, i8* %63)
%65 = call i32 @exit(i32 2)
unreachable
66: ; preds = %52
%67 = load i32, i32* %7
%68 = mul nsw i32 %67, 10
store i32 %68, i32* %7
%69 = load i32, i32* %9
%70 = load i32, i32* %7
%71 = add i32 %70, %69
store i32 %71, i32* %7
%72 = load i32, i32* %8
%73 = add i32 %72, 1
store i32 %73, i32* %8
br label %36
74: ; preds = %36
%75 = load i32, i32* %7
ret i32 %75
}

LLVM IR (-O2):
@errno = global i64 0
@stderr = global i32 0
@.str = [21 x i8] c"%s: read failed: %s\0A\00"
@.str.1 = [25 x i8] c"%s: too many characters\0A\00"
@.str.2 = [42 x i8] c"%s: unexpected character ('%lc' in '%s')\0A\00"
define i32 @fatal_get(i8* %0, i32 %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = bitcast [10 x i8]* %3 to i8**
%6 = call i32 @read(i32 %1, i8** %5, i32 10)
store i64 0, i64* @errno
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i8* @strerror(i64 0)
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %2
%14 = icmp sgt i32 %6, 9
br i1 %14, label %18, label %15
15: ; preds = %13
%16 = load i8, i8* %4
%17 = icmp eq i8 %16, 0
br i1 %17, label %41, label %22
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %15, %33
%23 = phi i8 [ %39, %33 ], [ %16, %15 ]
%24 = phi i32 [ %35, %33 ], [ 0, %15 ]
%25 = phi i32 [ %36, %33 ], [ 0, %15 ]
%26 = sext i8 %23 to i32
%27 = add nsw i32 %26, -48
%28 = icmp ugt i32 %27, 9
br i1 %28, label %29, label %33
29: ; preds = %22
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %26, i8* %4)
%32 = call i32 @exit(i32 2)
unreachable
33: ; preds = %22
%34 = mul nsw i32 %24, 10
%35 = add i32 %27, %34
%36 = add i32 %25, 1
%37 = zext i32 %36 to i64
%38 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = icmp eq i8 %39, 0
br i1 %40, label %41, label %22
41: ; preds = %33, %15
%42 = phi i32 [ 0, %15 ], [ %35, %33 ]
ret i32 %42
}