#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
char tolower(char);
int radix_str2uint(unsigned *value, int radix, char *buffer) {
  int i, n;
  char *bufferlocase = strdup(buffer);
  for (i = 0; bufferlocase[i]; i++)
    bufferlocase[i] = tolower(bufferlocase[i]);
  if (radix == 10) {
    n = sscanf(bufferlocase, "%u", value);
  } else if (radix == 8) {
    n = sscanf(bufferlocase, "%o", value);
    if (!n)
      n = sscanf(bufferlocase, "0%o", value);
  } else if (radix == 16) {
    n = sscanf(bufferlocase, "%x", value);
    if (!n)
      n = sscanf(bufferlocase, "0x%x", value);
  } else {
    fprintf(stderr, "radix_str2uint(): radix must be 10, 8 or 16\n");
    exit(1);
  }
  free(bufferlocase);
  return (n > 0);
}


x86 assembly (clang, -O0):
<radix_str2uint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11c5 <radix_str2uint+0x65>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1050 <tolower@plt>
mov    %al,%dl
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1187 <radix_str2uint+0x27>
cmpl   $0xa,-0xc(%rbp)
jne    11eb <radix_str2uint+0x8b>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe22(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,-0x20(%rbp)
jmp    12a2 <radix_str2uint+0x142>
cmpl   $0x8,-0xc(%rbp)
jne    1232 <radix_str2uint+0xd2>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe00(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    122d <radix_str2uint+0xcd>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdde(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,-0x20(%rbp)
jmp    129d <radix_str2uint+0x13d>
cmpl   $0x10,-0xc(%rbp)
jne    1279 <radix_str2uint+0x119>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdbe(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    1274 <radix_str2uint+0x114>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xd9b(%rip),%rsi
call   1060 <sscanf@plt>
mov    %eax,-0x20(%rbp)
jmp    1298 <radix_str2uint+0x138>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xd83(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    129d <radix_str2uint+0x13d>
jmp    12a2 <radix_str2uint+0x142>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x20(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<radix_str2uint>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rdx,%rdi
call   1080 <strdup@plt>
mov    %rax,%r15
mov    (%rax),%al
test   %al,%al
je     11a6 <radix_str2uint+0x46>
lea    0x1(%r15),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl %al,%edi
call   1050 <tolower@plt>
mov    %al,-0x1(%rbx)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1190 <radix_str2uint+0x30>
cmp    $0x10,%ebp
je     11d6 <radix_str2uint+0x76>
cmp    $0xa,%ebp
je     11f7 <radix_str2uint+0x97>
cmp    $0x8,%ebp
jne    1225 <radix_str2uint+0xc5>
lea    0xe48(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1060 <sscanf@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    120b <radix_str2uint+0xab>
lea    0xe2f(%rip),%rsi
jmp    11fe <radix_str2uint+0x9e>
lea    0xe2c(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1060 <sscanf@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    120b <radix_str2uint+0xab>
lea    0xe12(%rip),%rsi
jmp    11fe <radix_str2uint+0x9e>
lea    0xe02(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1060 <sscanf@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
test   %ebp,%ebp
setg   %al
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%u\00"
@.str.1 = [3 x i8] c"%o\00"
@.str.2 = [4 x i8] c"0%o\00"
@.str.3 = [3 x i8] c"%x\00"
@.str.4 = [5 x i8] c"0x%x\00"
@stderr = global i32 0
@.str.5 = [45 x i8] c"radix_str2uint(): radix must be 10, 8 or 16\0A\00"
define i32 @radix_str2uint(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i8*, i8** %6
%11 = call i8* @strdup(i8* %10)
store i8* %11, i8** %9
store i32 0, i32* %7
br label %12
12: ; preds = %30, %3
%13 = load i8*, i8** %9
%14 = load i32, i32* %7
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = icmp ne i8 %17, 0
br i1 %18, label %19, label %33
19: ; preds = %12
%20 = load i8*, i8** %9
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = call signext i8 @tolower(i8 signext %24)
%26 = load i8*, i8** %9
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 %25, i8* %29
br label %30
30: ; preds = %19
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %12
33: ; preds = %12
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, 10
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i8*, i8** %9
%38 = load i32*, i32** %4
%39 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %38)
store i32 %39, i32* %8
br label %74
40: ; preds = %33
%41 = load i32, i32* %5
%42 = icmp eq i32 %41, 8
br i1 %42, label %43, label %54
43: ; preds = %40
%44 = load i8*, i8** %9
%45 = load i32*, i32** %4
%46 = call i32 @sscanf(i8* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %45)
store i32 %46, i32* %8
%47 = load i32, i32* %8
%48 = icmp ne i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %43
%50 = load i8*, i8** %9
%51 = load i32*, i32** %4
%52 = call i32 @sscanf(i8* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %51)
store i32 %52, i32* %8
br label %53
53: ; preds = %49, %43
br label %73
54: ; preds = %40
%55 = load i32, i32* %5
%56 = icmp eq i32 %55, 16
br i1 %56, label %57, label %68
57: ; preds = %54
%58 = load i8*, i8** %9
%59 = load i32*, i32** %4
%60 = call i32 @sscanf(i8* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %59)
store i32 %60, i32* %8
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %67, label %63
63: ; preds = %57
%64 = load i8*, i8** %9
%65 = load i32*, i32** %4
%66 = call i32 @sscanf(i8* %64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32* %65)
store i32 %66, i32* %8
br label %67
67: ; preds = %63, %57
br label %72
68: ; preds = %54
%69 = load i32, i32* @stderr
%70 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %67
br label %73
73: ; preds = %72, %53
br label %74
74: ; preds = %73, %36
%75 = load i8*, i8** %9
%76 = call i32 @free(i8* %75)
%77 = load i32, i32* %8
%78 = icmp sgt i32 %77, 0
%79 = zext i1 %78 to i32
ret i32 %79
}

LLVM IR (-O2):
@.str = [3 x i8] c"%u\00"
@.str.1 = [3 x i8] c"%o\00"
@.str.2 = [4 x i8] c"0%o\00"
@.str.3 = [3 x i8] c"%x\00"
@.str.4 = [5 x i8] c"0x%x\00"
@stderr = global i32 0
@.str.5 = [45 x i8] c"radix_str2uint(): radix must be 10, 8 or 16\0A\00"
define i32 @radix_str2uint(i32* %0, i32 %1, i8* nocapture %2) {
%4 = tail call i8* @strdup(i8* %2)
%5 = load i8, i8* %4
%6 = icmp eq i8 %5, 0
br i1 %6, label %16, label %7
7: ; preds = %3, %7
%8 = phi i64 [ %12, %7 ], [ 0, %3 ]
%9 = phi i8 [ %14, %7 ], [ %5, %3 ]
%10 = phi i8* [ %13, %7 ], [ %4, %3 ]
%11 = tail call signext i8 @tolower(i8 signext %9)
store i8 %11, i8* %10
%12 = add nuw i64 %8, 1
%13 = getelementptr inbounds i8, i8* %4, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
br i1 %15, label %16, label %7
16: ; preds = %7, %3
switch i32 %1, label %23 [
i32 10, label %27
i32 8, label %17
i32 16, label %20
]
17: ; preds = %16
%18 = tail call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %27, label %30
20: ; preds = %16
%21 = tail call i32 @sscanf(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %30
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %20, %17, %16
%28 = phi i8* [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %16 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), %17 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), %20 ]
%29 = tail call i32 @sscanf(i8* %4, i8* %28, i32* %0)
br label %30
30: ; preds = %27, %17, %20
%31 = phi i32 [ %18, %17 ], [ %21, %20 ], [ %29, %27 ]
%32 = tail call i32 @free(i8* %4)
%33 = icmp sgt i32 %31, 0
%34 = zext i1 %33 to i32
ret i32 %34
}