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

/* Variables and functions */
int /*<<< orphan*/ memcpy(void *, unsigned int *, size_t);
unsigned int strtol(char const *, char **, int);
size_t convert_strtoint(const char *value, void *res, size_t len) {
  char *end;
  unsigned int i = strtol(value, &end, 10);
  if (*end != '\0')
    return 0;
  if (len > sizeof(i))
    len = sizeof(i);
  memcpy(res, &i, len);
  return sizeof(i);
}


x86 assembly (clang, -O0):
<convert_strtoint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1166 <convert_strtoint+0x46>
movq   $0x0,-0x8(%rbp)
jmp    1192 <convert_strtoint+0x72>
cmpq   $0x4,-0x20(%rbp)
jbe    1179 <convert_strtoint+0x59>
movq   $0x4,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    -0x2c(%rbp),%rsi
call   1040 <memcpy@plt>
movq   $0x4,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_strtoint>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %eax,0x4(%rsp)
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     1150 <convert_strtoint+0x30>
xor    %ebx,%ebx
jmp    116d <convert_strtoint+0x4d>
cmp    $0x4,%r15
mov    $0x4,%ebx
cmovae %rbx,%r15
lea    0x4(%rsp),%rsi
mov    %r14,%rdi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64 @convert_strtoint(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = call i32 @strtol(i8* %10, i8** %8, i32 10)
store i32 %11, i32* %9
%12 = load i8*, i8** %8
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i64 0, i64* %4
br label %25
17: ; preds = %3
%18 = load i64, i64* %7
%19 = icmp ugt i64 %18, 4
br i1 %19, label %20, label %21
20: ; preds = %17
store i64 4, i64* %7
br label %21
21: ; preds = %20, %17
%22 = load i8*, i8** %6
%23 = load i64, i64* %7
%24 = call i32 @memcpy(i8* %22, i32* %9, i64 %23)
store i64 4, i64* %4
br label %25
25: ; preds = %21, %16
%26 = load i64, i64* %4
ret i64 %26
}

LLVM IR (-O2):
define i64 @convert_strtoint(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = bitcast i8** %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i32 @strtol(i8* %0, i8** %4, i32 10)
store i32 %8, i32* %5
%9 = load i8*, i8** %4
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = icmp ult i64 %2, 4
%14 = select i1 %13, i64 %2, i64 4
%15 = call i32 @memcpy(i8* %1, i32* %5, i64 %14)
br label %16
16: ; preds = %3, %12
%17 = phi i64 [ 4, %12 ], [ 0, %3 ]
ret i64 %17
}