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

/* Variables and functions */
long LONG_MAX;
long LONG_MIN;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ scanf(char *, char **);
long strtol(char *, char **, int);
bool read_long(long *value, int base) {
  bool result = false;
  char *buffer;
  char *end;
  long converted;
  scanf("%ms", &buffer);
  if (buffer == NULL) {
    goto fail;
  }
  converted = strtol(buffer, &end, base);
  if (converted == LONG_MIN || converted == LONG_MAX || *end != '\0') {
    goto fail;
  }
  *value = converted;
  result = true;
fail:
  if (buffer != NULL) {
    free(buffer);
  }
  return result;
}


x86 assembly (clang, -O0):
<read_long>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xeb3(%rip),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <scanf@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1166 <read_long+0x36>
jmp    11c9 <read_long+0x99>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1040 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e63(%rip),%rcx
cmp    (%rcx),%rax
je     11b2 <read_long+0x82>
mov    -0x28(%rbp),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
je     11b2 <read_long+0x82>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11b7 <read_long+0x87>
jmp    11c9 <read_long+0x99>
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x10(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11dd <read_long+0xad>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_long>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xebd(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1050 <scanf@plt>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11a0 <read_long+0x70>
lea    0x10(%rsp),%rsi
mov    %ebx,%edx
call   1040 <strtol@plt>
mov    0x2e7e(%rip),%rcx
xor    %ebx,%ebx
cmp    (%rcx),%rax
je     118f <read_long+0x5f>
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%rax
je     118f <read_long+0x5f>
mov    0x10(%rsp),%rcx
cmpb   $0x0,(%rcx)
jne    118f <read_long+0x5f>
mov    %rax,(%r14)
mov    $0x1,%ebx
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11a2 <read_long+0x72>
call   1030 <free@plt>
jmp    11a2 <read_long+0x72>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%ms\00"
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
define i32 @read_long(i64* %0, i32 %1) {
%3 = alloca i64*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
store i64* %0, i64** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%9 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %6)
%10 = load i8*, i8** %6
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
br label %33
13: ; preds = %2
%14 = load i8*, i8** %6
%15 = load i32, i32* %4
%16 = call i64 @strtol(i8* %14, i8** %7, i32 %15)
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = load i64, i64* @LONG_MIN
%19 = icmp eq i64 %17, %18
br i1 %19, label %29, label %20
20: ; preds = %13
%21 = load i64, i64* %8
%22 = load i64, i64* @LONG_MAX
%23 = icmp eq i64 %21, %22
br i1 %23, label %29, label %24
24: ; preds = %20
%25 = load i8*, i8** %7
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24, %20, %13
br label %33
30: ; preds = %24
%31 = load i64, i64* %8
%32 = load i64*, i64** %3
store i64 %31, i64* %32
store i32 1, i32* %5
br label %33
33: ; preds = %30, %29, %12
%34 = load i8*, i8** %6
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %33
%37 = load i8*, i8** %6
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %36, %33
%40 = load i32, i32* %5
ret i32 %40
}

LLVM IR (-O2):
@.str = [4 x i8] c"%ms\00"
@LONG_MIN = global i64 0
@LONG_MAX = global i64 0
define i32 @read_long(i64* nocapture writeonly %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = bitcast i8** %3 to i8*
%6 = bitcast i8** %4 to i8*
%7 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %3)
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %28, label %10
10: ; preds = %2
%11 = call i64 @strtol(i8* %8, i8** %4, i32 %1)
%12 = load i64, i64* @LONG_MIN
%13 = icmp eq i64 %11, %12
%14 = load i64, i64* @LONG_MAX
%15 = icmp eq i64 %11, %14
%16 = select i1 %13, i1 true, i1 %15
br i1 %16, label %22, label %17
17: ; preds = %10
%18 = load i8*, i8** %4
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i64 %11, i64* %0
br label %22
22: ; preds = %10, %17, %21
%23 = phi i32 [ 1, %21 ], [ 0, %17 ], [ 0, %10 ]
%24 = load i8*, i8** %3
%25 = icmp eq i8* %24, null
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 @free(i8* %24)
br label %28
28: ; preds = %2, %26, %22
%29 = phi i32 [ %23, %26 ], [ %23, %22 ], [ 0, %2 ]
ret i32 %29
}