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

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
long strtol(char *, char **, int);
long readlong(char *prompt) {
  char buf[128];
  long num;
  char *ep;
  while (true) {
    printf("%s: ", prompt);
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf) - 1] = '\0';
    errno = 0;
    num = strtol(buf, &ep, 10);
    if (strlen(buf) < 1 || errno || *ep) {
      printf("Sorry, '%s' is not a valid integer. Try again.\n", buf);
    } else {
      break;
    }
  }
  return num;
}


x86 assembly (clang, -O0):
<readlong>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
lea    0xea6(%rip),%rdi
call   1040 <printf@plt>
lea    -0x90(%rbp),%rdi
mov    0x2e7b(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1050 <fgets@plt>
lea    -0x90(%rbp),%rdi
call   1030 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x90(%rbp,%rax,1)
mov    0x2e3f(%rip),%rax
movq   $0x0,(%rax)
lea    -0x90(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,-0x98(%rbp)
lea    -0x90(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
jl     11f8 <readlong+0xb8>
mov    0x2dfd(%rip),%rax
cmpq   $0x0,(%rax)
jne    11f8 <readlong+0xb8>
mov    -0xa0(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1210 <readlong+0xd0>
lea    -0x90(%rbp),%rsi
lea    0xdff(%rip),%rdi
call   1040 <printf@plt>
jmp    1215 <readlong+0xd5>
jmp    121a <readlong+0xda>
jmp    114f <readlong+0xf>
mov    -0x98(%rbp),%rax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readlong>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %rdi,%r15
lea    0x10(%rsp),%rbx
mov    0x2e78(%rip),%r14
lea    0x8(%rsp),%r12
lea    0xe99(%rip),%r13
jmp    117b <readlong+0x3b>
xchg   %ax,%ax
mov    %r13,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xe7e(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1050 <fgets@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
movq   $0x0,(%r14)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <strlen@plt>
test   %eax,%eax
jle    1170 <readlong+0x30>
cmpq   $0x0,(%r14)
jne    1170 <readlong+0x30>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
jne    1170 <readlong+0x30>
mov    %rbp,%rax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s: \00"
@stdin = global i32 0
@errno = global i64 0
@.str.1 = [48 x i8] c"Sorry, '%s' is not a valid integer. Try again.\0A\00"
define i64 @readlong(i8* %0) {
%2 = alloca i8*
%3 = alloca [128 x i8]
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %2
br label %6
6: ; preds = %1, %34
%7 = load i8*, i8** %2
%8 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* @stdin
%11 = call i32 @fgets(i8* %9, i32 128, i32 %10)
%12 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%13 = call i32 @strlen(i8* %12)
%14 = sub nsw i32 %13, 1
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %15
store i8 0, i8* %16
store i64 0, i64* @errno
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%18 = call i64 @strtol(i8* %17, i8** %5, i32 10)
store i64 %18, i64* %4
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
%21 = icmp slt i32 %20, 1
br i1 %21, label %30, label %22
22: ; preds = %6
%23 = load i64, i64* @errno
%24 = icmp ne i64 %23, 0
br i1 %24, label %30, label %25
25: ; preds = %22
%26 = load i8*, i8** %5
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25, %22, %6
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %34
33: ; preds = %25
br label %35
34: ; preds = %30
br label %6
35: ; preds = %33
%36 = load i64, i64* %4
ret i64 %36
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s: \00"
@stdin = global i32 0
@errno = global i64 0
@.str.1 = [48 x i8] c"Sorry, '%s' is not a valid integer. Try again.\0A\00"
define i64 @readlong(i8* %0) {
%2 = alloca [128 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%5 = bitcast i8** %3 to i8*
br label %6
6: ; preds = %24, %1
%7 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = load i32, i32* @stdin
%9 = call i32 @fgets(i8* %4, i32 128, i32 %8)
%10 = call i32 @strlen(i8* %4)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %12
store i8 0, i8* %13
store i64 0, i64* @errno
%14 = call i64 @strtol(i8* %4, i8** %3, i32 10)
%15 = call i32 @strlen(i8* %4)
%16 = icmp slt i32 %15, 1
%17 = load i64, i64* @errno
%18 = icmp ne i64 %17, 0
%19 = select i1 %16, i1 true, i1 %18
br i1 %19, label %24, label %20
20: ; preds = %6
%21 = load i8*, i8** %3
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20, %6
%25 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %6
26: ; preds = %20
ret i64 %14
}