#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 SIZE_MAX;
scalar_t__ memcmp(char const *, char *, int);
int strlen(char const *);
char *strndup(char const *, int);
char *strrchr(char const *, char);
long strtol(char const *, char **, int);
int parse_distribution_fname(const char *filename, size_t *blocksize_out,
                             char **namebase_out) {
  const char *r = strrchr(filename, '.');
  const char *dot = r;
  if (strlen(r) < 4 || memcmp(r, ".bs", 3))
    return -1;
  r += 3;
  char *endptr = NULL;
  long bs = strtol(r, &endptr, 10);
  if (bs <= 0 || !endptr || *endptr || bs > SIZE_MAX)
    return -1;
  *blocksize_out = (size_t)bs;
  *namebase_out = strndup(filename, dot - filename);
  return 0;
}


x86 assembly (clang, -O0):
<parse_distribution_fname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x4,%eax
jl     11af <parse_distribution_fname+0x5f>
mov    -0x28(%rbp),%rdi
lea    0xe65(%rip),%rsi
mov    $0x3,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%rax
je     11bb <parse_distribution_fname+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <parse_distribution_fname+0x10d>
mov    -0x28(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jle    121f <parse_distribution_fname+0xcf>
cmpq   $0x0,-0x38(%rbp)
je     121f <parse_distribution_fname+0xcf>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    121f <parse_distribution_fname+0xcf>
mov    -0x40(%rbp),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jle    122b <parse_distribution_fname+0xdb>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <parse_distribution_fname+0x10d>
mov    -0x40(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%esi
call   1050 <strndup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_distribution_fname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
mov    $0x2e,%esi
call   1040 <strrchr@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <strlen@plt>
mov    $0xffffffff,%ebp
cmp    $0x4,%eax
jl     119d <parse_distribution_fname+0x4d>
lea    0xe75(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1060 <memcmp@plt>
test   %rax,%rax
je     11ac <parse_distribution_fname+0x5c>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
add    $0x3,%rdi
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jle    119d <parse_distribution_fname+0x4d>
mov    0x8(%rsp),%rcx
test   %rcx,%rcx
je     119d <parse_distribution_fname+0x4d>
cmpb   $0x0,(%rcx)
jne    119d <parse_distribution_fname+0x4d>
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
jg     119d <parse_distribution_fname+0x4d>
mov    %rax,(%r15)
sub    %r12d,%ebx
mov    %r12,%rdi
mov    %ebx,%esi
call   1050 <strndup@plt>
mov    %rax,(%r14)
xor    %ebp,%ebp
jmp    119d <parse_distribution_fname+0x4d>

LLVM IR (-O0):
@.str = [4 x i8] c".bs\00"
@SIZE_MAX = global i64 0
define i32 @parse_distribution_fname(i8* %0, i64* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %5
store i64* %1, i64** %6
store i8** %2, i8*** %7
%12 = load i8*, i8** %5
%13 = call i8* @strrchr(i8* %12, i8 signext 46)
store i8* %13, i8** %8
%14 = load i8*, i8** %8
store i8* %14, i8** %9
%15 = load i8*, i8** %8
%16 = call i32 @strlen(i8* %15)
%17 = icmp slt i32 %16, 4
br i1 %17, label %22, label %18
18: ; preds = %3
%19 = load i8*, i8** %8
%20 = call i64 @memcmp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %18, %3
store i32 -1, i32* %4
br label %55
23: ; preds = %18
%24 = load i8*, i8** %8
%25 = getelementptr inbounds i8, i8* %24, i64 3
store i8* %25, i8** %8
store i8* null, i8** %10
%26 = load i8*, i8** %8
%27 = call i64 @strtol(i8* %26, i8** %10, i32 10)
store i64 %27, i64* %11
%28 = load i64, i64* %11
%29 = icmp sle i64 %28, 0
br i1 %29, label %42, label %30
30: ; preds = %23
%31 = load i8*, i8** %10
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %42
33: ; preds = %30
%34 = load i8*, i8** %10
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = load i64, i64* %11
%40 = load i64, i64* @SIZE_MAX
%41 = icmp sgt i64 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %38, %33, %30, %23
store i32 -1, i32* %4
br label %55
43: ; preds = %38
%44 = load i64, i64* %11
%45 = load i64*, i64** %6
store i64 %44, i64* %45
%46 = load i8*, i8** %5
%47 = load i8*, i8** %9
%48 = load i8*, i8** %5
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %48 to i64
%51 = sub i64 %49, %50
%52 = trunc i64 %51 to i32
%53 = call i8* @strndup(i8* %46, i32 %52)
%54 = load i8**, i8*** %7
store i8* %53, i8** %54
store i32 0, i32* %4
br label %55
55: ; preds = %43, %42, %22
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@.str = [4 x i8] c".bs\00"
@SIZE_MAX = global i64 0
define i32 @parse_distribution_fname(i8* %0, i64* nocapture writeonly %1, i8** nocapture writeonly %2) {
%4 = alloca i8*
%5 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%6 = tail call i32 @strlen(i8* %5)
%7 = icmp slt i32 %6, 4
br i1 %7, label %33, label %8
8: ; preds = %3
%9 = tail call i64 @memcmp(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %33
11: ; preds = %8
%12 = getelementptr inbounds i8, i8* %5, i64 3
%13 = bitcast i8** %4 to i8*
store i8* null, i8** %4
%14 = call i64 @strtol(i8* %12, i8** %4, i32 10)
%15 = icmp sgt i64 %14, 0
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %31
19: ; preds = %11
%20 = load i8, i8* %16
%21 = icmp ne i8 %20, 0
%22 = load i64, i64* @SIZE_MAX
%23 = icmp sgt i64 %14, %22
%24 = select i1 %21, i1 true, i1 %23
br i1 %24, label %31, label %25
25: ; preds = %19
store i64 %14, i64* %1
%26 = ptrtoint i8* %5 to i64
%27 = ptrtoint i8* %0 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
%30 = tail call i8* @strndup(i8* %0, i32 %29)
store i8* %30, i8** %2
br label %31
31: ; preds = %11, %19, %25
%32 = phi i32 [ 0, %25 ], [ -1, %19 ], [ -1, %11 ]
br label %33
33: ; preds = %3, %8, %31
%34 = phi i32 [ %32, %31 ], [ -1, %8 ], [ -1, %3 ]
ret i32 %34
}