#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 EFAULT;
int EINVAL;
int EIO;
int sscanf(char const *, char *, int *);
int read_int(const char *s) {
  int i = -EIO;
  if (NULL == s) {
    return -EFAULT;
  }
  if ('0' == s[0]) {
    if (('x' == s[1]) || ('X' == s[1])) {
      if (sscanf(&s[2], "%x", &i) != 1) {
        return -EINVAL;
      }
    } else {
      if (sscanf(s, "%o", &i) != 1) {
        return -EINVAL;
      }
    }
  } else {
    if (sscanf(s, "%d", &i) != 1) {
      return -EINVAL;
    }
  }
  return i;
}


x86 assembly (clang, -O0):
<read_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
mov    0x2eb3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1149 <read_int+0x39>
xor    %eax,%eax
mov    0x2eb9(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1235 <read_int+0x125>
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
mov    $0x30,%eax
cmp    %ecx,%eax
jne    11fa <read_int+0xea>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%ecx
mov    $0x78,%eax
cmp    %ecx,%eax
je     1187 <read_int+0x77>
mov    -0x10(%rbp),%rax
movsbl 0x1(%rax),%ecx
mov    $0x58,%eax
cmp    %ecx,%eax
jne    11c0 <read_int+0xb0>
mov    -0x10(%rbp),%rdi
add    $0x2,%rdi
lea    0xe6a(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
je     11bb <read_int+0xab>
xor    %eax,%eax
mov    0x2e1f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1235 <read_int+0x125>
jmp    11f5 <read_int+0xe5>
mov    -0x10(%rbp),%rdi
lea    0xe38(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
je     11f0 <read_int+0xe0>
xor    %eax,%eax
mov    0x2dea(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1235 <read_int+0x125>
jmp    11f5 <read_int+0xe5>
jmp    122f <read_int+0x11f>
mov    -0x10(%rbp),%rdi
lea    0xe01(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
je     122a <read_int+0x11a>
xor    %eax,%eax
mov    0x2db0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1235 <read_int+0x125>
jmp    122f <read_int+0x11f>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_int>:
push   %rax
mov    0x2ec0(%rip),%rcx
xor    %eax,%eax
xor    %edx,%edx
sub    (%rcx),%edx
mov    %edx,0x4(%rsp)
test   %rdi,%rdi
je     1142 <read_int+0x32>
cmpb   $0x30,(%rdi)
jne    114b <read_int+0x3b>
mov    0x1(%rdi),%al
or     $0x20,%al
cmp    $0x78,%al
jne    1154 <read_int+0x44>
add    $0x2,%rdi
lea    0xec0(%rip),%rsi
jmp    115b <read_int+0x4b>
mov    0x2eaf(%rip),%rcx
jmp    1179 <read_int+0x69>
lea    0xeb4(%rip),%rsi
jmp    115b <read_int+0x4b>
lea    0xea8(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1030 <sscanf@plt>
cmp    $0x1,%eax
jne    1170 <read_int+0x60>
mov    0x4(%rsp),%eax
pop    %rcx
ret
mov    0x2e59(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rcx
ret

LLVM IR (-O0):
@EIO = global i32 0
@EFAULT = global i32 0
@.str = [3 x i8] c"%x\00"
@EINVAL = global i32 0
@.str.1 = [3 x i8] c"%o\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @read_int(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i32, i32* @EIO
%6 = sub nsw i32 0, %5
store i32 %6, i32* %4
%7 = load i8*, i8** %3
%8 = icmp eq i8* null, %7
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @EFAULT
%11 = sub nsw i32 0, %10
store i32 %11, i32* %2
br label %58
12: ; preds = %1
%13 = load i8*, i8** %3
%14 = getelementptr inbounds i8, i8* %13, i64 0
%15 = load i8, i8* %14
%16 = sext i8 %15 to i32
%17 = icmp eq i32 48, %16
br i1 %17, label %18, label %48
18: ; preds = %12
%19 = load i8*, i8** %3
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 120, %22
br i1 %23, label %30, label %24
24: ; preds = %18
%25 = load i8*, i8** %3
%26 = getelementptr inbounds i8, i8* %25, i64 1
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 88, %28
br i1 %29, label %30, label %39
30: ; preds = %24, %18
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i64 2
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i32, i32* @EINVAL
%37 = sub nsw i32 0, %36
store i32 %37, i32* %2
br label %58
38: ; preds = %30
br label %47
39: ; preds = %24
%40 = load i8*, i8** %3
%41 = call i32 @sscanf(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%42 = icmp ne i32 %41, 1
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* @EINVAL
%45 = sub nsw i32 0, %44
store i32 %45, i32* %2
br label %58
46: ; preds = %39
br label %47
47: ; preds = %46, %38
br label %56
48: ; preds = %12
%49 = load i8*, i8** %3
%50 = call i32 @sscanf(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%51 = icmp ne i32 %50, 1
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i32, i32* @EINVAL
%54 = sub nsw i32 0, %53
store i32 %54, i32* %2
br label %58
55: ; preds = %48
br label %56
56: ; preds = %55, %47
%57 = load i32, i32* %4
store i32 %57, i32* %2
br label %58
58: ; preds = %56, %52, %43, %35, %9
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
@EIO = global i32 0
@EFAULT = global i32 0
@.str = [3 x i8] c"%x\00"
@EINVAL = global i32 0
@.str.1 = [3 x i8] c"%o\00"
@.str.2 = [3 x i8] c"%d\00"
define i32 @read_int(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @EIO
%5 = sub nsw i32 0, %4
store i32 %5, i32* %2
%6 = icmp eq i8* %0, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @EFAULT
%9 = sub nsw i32 0, %8
br label %37
10: ; preds = %1
%11 = load i8, i8* %0
%12 = icmp eq i8 %11, 48
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = getelementptr inbounds i8, i8* %0, i64 1
%15 = load i8, i8* %14
switch i8 %15, label %23 [
i8 120, label %16
i8 88, label %16
]
16: ; preds = %13, %13
%17 = getelementptr inbounds i8, i8* %0, i64 2
%18 = call i32 @sscanf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%19 = icmp eq i32 %18, 1
br i1 %19, label %35, label %20
20: ; preds = %16
%21 = load i32, i32* @EINVAL
%22 = sub nsw i32 0, %21
br label %37
23: ; preds = %13
%24 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%25 = icmp eq i32 %24, 1
br i1 %25, label %35, label %26
26: ; preds = %23
%27 = load i32, i32* @EINVAL
%28 = sub nsw i32 0, %27
br label %37
29: ; preds = %10
%30 = call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %2)
%31 = icmp eq i32 %30, 1
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = load i32, i32* @EINVAL
%34 = sub nsw i32 0, %33
br label %37
35: ; preds = %29, %16, %23
%36 = load i32, i32* %2
br label %37
37: ; preds = %35, %32, %26, %20, %7
%38 = phi i32 [ %9, %7 ], [ %22, %20 ], [ %36, %35 ], [ %28, %26 ], [ %34, %32 ]
ret i32 %38
}