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

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char nie_convert_init_letter(char const *);
int nif_detect_type(char const *);
long strtol(char *, char **, int);
long int prepare_nif(const char *nif) {
  char buf_nif_no_letter[9];
  memset(buf_nif_no_letter, 0, sizeof(buf_nif_no_letter));
  for (size_t i = 0; i < sizeof(buf_nif_no_letter) - 1; ++i) {
    buf_nif_no_letter[i] = nif[i];
  }
  int nif_type = nif_detect_type(nif);
  if (nif_type == 20) {
    buf_nif_no_letter[0] = nie_convert_init_letter(nif);
  }
  char *error_str;
  long int result = strtol(buf_nif_no_letter, &error_str, 10);
  if (error_str[0]) {
    result = -20;
  }
  return result;
}


x86 assembly (clang, -O0):
<prepare_nif>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x11(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1030 <memset@plt>
movq   $0x0,-0x20(%rbp)
cmpq   $0x8,-0x20(%rbp)
jae    1193 <prepare_nif+0x53>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x20(%rbp),%rax
mov    %cl,-0x11(%rbp,%rax,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1164 <prepare_nif+0x24>
mov    -0x8(%rbp),%rdi
call   1050 <nif_detect_type@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x14,-0x24(%rbp)
jne    11b5 <prepare_nif+0x75>
mov    -0x8(%rbp),%rdi
call   1060 <nie_convert_init_letter@plt>
mov    %al,-0x11(%rbp)
lea    -0x11(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     11e0 <prepare_nif+0xa0>
movq   $0xffffffffffffffec,-0x38(%rbp)
ff
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prepare_nif>:
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1030 <memset@plt>
mov    (%rbx),%rax
mov    %rax,0x8(%rsp)
mov    %rbx,%rdi
call   1050 <nif_detect_type@plt>
cmp    $0x14,%eax
jne    117a <prepare_nif+0x3a>
mov    %rbx,%rdi
call   1060 <nie_convert_init_letter@plt>
mov    %al,0x8(%rsp)
lea    0x8(%rsp),%rdi
lea    0x18(%rsp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    0x18(%rsp),%rcx
cmpb   $0x0,(%rcx)
mov    $0xffffffffffffffec,%rcx
cmovne %rcx,%rax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i64 @prepare_nif(i8* %0) {
%2 = alloca i8*
%3 = alloca [9 x i8]
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %2
%8 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i32 0, i32 9)
store i64 0, i64* %4
br label %10
10: ; preds = %20, %1
%11 = load i64, i64* %4
%12 = icmp ult i64 %11, 8
br i1 %12, label %13, label %23
13: ; preds = %10
%14 = load i8*, i8** %2
%15 = load i64, i64* %4
%16 = getelementptr inbounds i8, i8* %14, i64 %15
%17 = load i8, i8* %16
%18 = load i64, i64* %4
%19 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 %18
store i8 %17, i8* %19
br label %20
20: ; preds = %13
%21 = load i64, i64* %4
%22 = add i64 %21, 1
store i64 %22, i64* %4
br label %10
23: ; preds = %10
%24 = load i8*, i8** %2
%25 = call i32 @nif_detect_type(i8* %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 20
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i8*, i8** %2
%30 = call signext i8 @nie_convert_init_letter(i8* %29)
%31 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
store i8 %30, i8* %31
br label %32
32: ; preds = %28, %23
%33 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%34 = call i64 @strtol(i8* %33, i8** %6, i32 10)
store i64 %34, i64* %7
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i64 0
%37 = load i8, i8* %36
%38 = icmp ne i8 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %32
store i64 -20, i64* %7
br label %40
40: ; preds = %39, %32
%41 = load i64, i64* %7
ret i64 %41
}

LLVM IR (-O2):
define i64 @prepare_nif(i8* %0) {
%2 = alloca [9 x i8]
%3 = alloca i8*
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 9)
%6 = bitcast i8* %0 to i64*
%7 = bitcast [9 x i8]* %2 to i64*
%8 = load i64, i64* %6
store i64 %8, i64* %7
%9 = call i32 @nif_detect_type(i8* %0)
%10 = icmp eq i32 %9, 20
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call signext i8 @nie_convert_init_letter(i8* %0)
store i8 %12, i8* %4
br label %13
13: ; preds = %11, %1
%14 = bitcast i8** %3 to i8*
%15 = call i64 @strtol(i8* %4, i8** %3, i32 10)
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
%19 = select i1 %18, i64 %15, i64 -20
ret i64 %19
}