#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*/ assert(int);
int expr(char const *, size_t *);
int /*<<< orphan*/ sscanf(char const *, char *, int *, int *);
int factor(const char *expr_str, size_t *cursor) {
  int val;
  if (expr_str[*cursor] == '(') {
    (*cursor)++;
    val = expr(expr_str, cursor);
    assert(expr_str[*cursor] == ')');
    (*cursor)++;
  } else {
    int read;
    sscanf(&expr_str[*cursor], "%d%n", &val, &read);
    *cursor += read;
  }
  return val;
}


x86 assembly (clang, -O0):
<factor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x28,%eax
jne    11a8 <factor+0x78>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <expr@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x29,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
add    $0x1,%rcx
mov    %rcx,(%rax)
jmp    11d5 <factor+0xa5>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
add    (%rax),%rdi
lea    0xe46(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1050 <sscanf@plt>
movslq -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<factor>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    (%rsi),%rax
cmpb   $0x28,(%rdi,%rax,1)
jne    1175 <factor+0x45>
add    $0x1,%rax
mov    %rax,(%r14)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <expr@plt>
mov    %eax,%ebp
mov    (%r14),%rax
xor    %edi,%edi
cmpb   $0x29,(%rbx,%rax,1)
sete   %dil
call   1040 <assert@plt>
addq   $0x1,(%r14)
mov    %ebp,%eax
jmp    119d <factor+0x6d>
add    %rax,%rbx
lea    0xe81(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
call   1050 <sscanf@plt>
movslq 0x8(%rsp),%rax
add    %rax,(%r14)
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%d%n\00"
define i32 @factor(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i64* %1, i64** %4
%7 = load i8*, i8** %3
%8 = load i64*, i64** %4
%9 = load i64, i64* %8
%10 = getelementptr inbounds i8, i8* %7, i64 %9
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 40
br i1 %13, label %14, label %33
14: ; preds = %2
%15 = load i64*, i64** %4
%16 = load i64, i64* %15
%17 = add i64 %16, 1
store i64 %17, i64* %15
%18 = load i8*, i8** %3
%19 = load i64*, i64** %4
%20 = call i32 @expr(i8* %18, i64* %19)
store i32 %20, i32* %5
%21 = load i8*, i8** %3
%22 = load i64*, i64** %4
%23 = load i64, i64* %22
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 41
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i64*, i64** %4
%31 = load i64, i64* %30
%32 = add i64 %31, 1
store i64 %32, i64* %30
br label %44
33: ; preds = %2
%34 = load i8*, i8** %3
%35 = load i64*, i64** %4
%36 = load i64, i64* %35
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = call i32 @sscanf(i8* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %5, i32* %6)
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = load i64*, i64** %4
%42 = load i64, i64* %41
%43 = add i64 %42, %40
store i64 %43, i64* %41
br label %44
44: ; preds = %33, %14
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@.str = [5 x i8] c"%d%n\00"
define i32 @factor(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = load i64, i64* %1
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 40
br i1 %9, label %10, label %21
10: ; preds = %2
%11 = add i64 %6, 1
store i64 %11, i64* %1
%12 = tail call i32 @expr(i8* %0, i64* %1)
%13 = load i64, i64* %1
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 41
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = load i64, i64* %1
%20 = add i64 %19, 1
store i64 %20, i64* %1
br label %29
21: ; preds = %2
%22 = bitcast i32* %4 to i8*
%23 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32* %3, i32* %4)
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = load i64, i64* %1
%27 = add i64 %26, %25
store i64 %27, i64* %1
%28 = load i32, i32* %3
br label %29
29: ; preds = %21, %10
%30 = phi i32 [ %28, %21 ], [ %12, %10 ]
ret i32 %30
}