#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 */
char *argdup(char *, char *, int, int);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
long strtol(char *, int /*<<< orphan*/ *, int);
long argtol(char *src, char *sep, int arg, int opt, int base) {
  char *str;
  long val;
  str = argdup(src, sep, arg, opt);
  if (!str)
    return (-1);
  errno = 0;
  val = strtol(str, NULL, base);
  if (errno)
    val = -1;
  free(str);
  return (val);
}


x86 assembly (clang, -O0):
<argtol>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1050 <argdup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1179 <argtol+0x49>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11c5 <argtol+0x95>
mov    0x2e60(%rip),%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1040 <strtol@plt>
mov    %rax,-0x38(%rbp)
mov    0x2e3e(%rip),%rax
cmpq   $0x0,(%rax)
je     11b4 <argtol+0x84>
movq   $0xffffffffffffffff,-0x38(%rbp)
ff
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<argtol>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8d,%ebp
call   1050 <argdup@plt>
test   %rax,%rax
je     1177 <argtol+0x47>
mov    %rax,%rbx
mov    0x2e92(%rip),%r14
movq   $0x0,(%r14)
xor    %r15d,%r15d
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <strtol@plt>
cmp    (%r14),%r15
sbb    %rbp,%rbp
or     %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    117e <argtol+0x4e>
mov    $0xffffffffffffffff,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
define i64 @argtol(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = load i8*, i8** %7
%15 = load i8*, i8** %8
%16 = load i32, i32* %9
%17 = load i32, i32* %10
%18 = call i8* @argdup(i8* %14, i8* %15, i32 %16, i32 %17)
store i8* %18, i8** %12
%19 = load i8*, i8** %12
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %5
store i64 -1, i64* %6
br label %33
22: ; preds = %5
store i64 0, i64* @errno
%23 = load i8*, i8** %12
%24 = load i32, i32* %11
%25 = call i64 @strtol(i8* %23, i32* null, i32 %24)
store i64 %25, i64* %13
%26 = load i64, i64* @errno
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %22
store i64 -1, i64* %13
br label %29
29: ; preds = %28, %22
%30 = load i8*, i8** %12
%31 = call i32 @free(i8* %30)
%32 = load i64, i64* %13
store i64 %32, i64* %6
br label %33
33: ; preds = %29, %21
%34 = load i64, i64* %6
ret i64 %34
}

LLVM IR (-O2):
@errno = global i64 0
define i64 @argtol(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i8* @argdup(i8* %0, i8* %1, i32 %2, i32 %3)
%7 = icmp eq i8* %6, null
br i1 %7, label %14, label %8
8: ; preds = %5
store i64 0, i64* @errno
%9 = tail call i64 @strtol(i8* nocapture %6, i32* null, i32 %4)
%10 = load i64, i64* @errno
%11 = icmp eq i64 %10, 0
%12 = select i1 %11, i64 %9, i64 -1
%13 = tail call i32 @free(i8* %6)
br label %14
14: ; preds = %5, %8
%15 = phi i64 [ %12, %8 ], [ -1, %5 ]
ret i64 %15
}