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

/* Variables and functions */
scalar_t__ errno;
long strtol(char const *, char **, int);
uint8_t opdi_str_to_uint8(const char *str, uint8_t *result) {
  char *p;
  long res;
  errno = 0;
  res = strtol(str, &p, 10);
  if (errno != 0 || *p != 0 || p == str)
    return 5;
  *result = (uint8_t)res;
  return 0;
}


x86 assembly (clang, -O0):
<opdi_str_to_uint8>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2eb9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,-0x28(%rbp)
mov    0x2e95(%rip),%rax
cmpq   $0x0,(%rax)
jne    1173 <opdi_str_to_uint8+0x63>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1173 <opdi_str_to_uint8+0x63>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    117f <opdi_str_to_uint8+0x6f>
movl   $0x5,-0x4(%rbp)
jmp    1192 <opdi_str_to_uint8+0x82>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opdi_str_to_uint8>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2eba(%rip),%r15
movq   $0x0,(%r15)
lea    0x8(%rsp),%rsi
mov    $0xa,%edx
call   1030 <strtol@plt>
mov    %rax,%rcx
mov    $0x5,%eax
cmpq   $0x0,(%r15)
jne    115e <opdi_str_to_uint8+0x4e>
mov    0x8(%rsp),%rdx
cmp    %rbx,%rdx
je     115e <opdi_str_to_uint8+0x4e>
cmpb   $0x0,(%rdx)
jne    115e <opdi_str_to_uint8+0x4e>
mov    %ecx,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

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