#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 */
scalar_t__ atoi(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
scalar_t__ strcmp(char *, char *);
char *strtok(char *, char *);
int expValid(char *exp) {
  char *tok;
  int tally = 0;
  tok = strtok(exp, " \n");
  while (tok != NULL) {
    if (strcmp(tok, "*") == 0 || strcmp(tok, "/") == 0 ||
        strcmp(tok, "+") == 0 || strcmp(tok, "-") == 0) {
      tally--;
    } else if (atoi(tok) != 0) {
      tally++;
    } else {
      printf("error: contains unknown symbols");
      return 0;
    }
    if (tally < 1) {
      printf("error: not a valid RPN expression\n");
      return 0;
    }
    tok = strtok(NULL, " \n");
  }
  if (tally == 1) {
    return 1;
  }
  puts("error: not a valid RPN expression\n");
  return 0;
}


x86 assembly (clang, -O0):
<expValid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1271 <expValid+0x121>
mov    -0x18(%rbp),%rdi
lea    0xe76(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11ea <expValid+0x9a>
mov    -0x18(%rbp),%rdi
lea    0xe5e(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11ea <expValid+0x9a>
mov    -0x18(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     11ea <expValid+0x9a>
mov    -0x18(%rbp),%rdi
lea    0xe2e(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    11f8 <expValid+0xa8>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1236 <expValid+0xe6>
mov    -0x18(%rbp),%rdi
call   1070 <atoi@plt>
cmp    $0x0,%rax
je     1219 <expValid+0xc9>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1231 <expValid+0xe1>
lea    0xdeb(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    129a <expValid+0x14a>
jmp    1236 <expValid+0xe6>
cmpl   $0x1,-0x1c(%rbp)
jge    1258 <expValid+0x108>
lea    0xde4(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    129a <expValid+0x14a>
xor    %eax,%eax
mov    %eax,%edi
lea    0xd9d(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x18(%rbp)
jmp    1177 <expValid+0x27>
cmpl   $0x1,-0x1c(%rbp)
jne    1287 <expValid+0x137>
movl   $0x1,-0x4(%rbp)
jmp    129a <expValid+0x14a>
lea    0xd9d(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expValid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe9e(%rip),%rsi
call   1060 <strtok@plt>
test   %rax,%rax
je     1222 <expValid+0xd2>
mov    %rax,%rbp
xor    %r13d,%r13d
lea    0xe86(%rip),%rbx
lea    0xe7c(%rip),%r14
lea    0xe7a(%rip),%r15
lea    0xe75(%rip),%r12
jmp    11c0 <expValid+0x70>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x1,%r13d
jle    122b <expValid+0xdb>
add    $0xffffffff,%r13d
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1217 <expValid+0xc7>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11a0 <expValid+0x50>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11a0 <expValid+0x50>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11a0 <expValid+0x50>
mov    %rbp,%rdi
lea    0xe0f(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11a0 <expValid+0x50>
mov    %rbp,%rdi
call   1070 <atoi@plt>
test   %rax,%rax
je     1248 <expValid+0xf8>
add    $0x1,%r13d
jmp    11ae <expValid+0x5e>
mov    $0x1,%eax
cmp    $0x1,%r13d
je     1239 <expValid+0xe9>
lea    0xe02(%rip),%rdi
jmp    1232 <expValid+0xe2>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdbc(%rip),%rdi
call   1040 <printf@plt>
jmp    1237 <expValid+0xe7>

LLVM IR (-O0):
@.str = [3 x i8] c" \0A\00"
@.str.1 = [2 x i8] c"*\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [2 x i8] c"+\00"
@.str.4 = [2 x i8] c"-\00"
@.str.5 = [32 x i8] c"error: contains unknown symbols\00"
@.str.6 = [35 x i8] c"error: not a valid RPN expression\0A\00"
define i32 @expValid(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %5
%6 = load i8*, i8** %3
%7 = call i8* @strtok(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %4
br label %8
8: ; preds = %45, %1
%9 = load i8*, i8** %4
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %47
11: ; preds = %8
%12 = load i8*, i8** %4
%13 = call i64 @strcmp(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %27, label %15
15: ; preds = %11
%16 = load i8*, i8** %4
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %15
%20 = load i8*, i8** %4
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i8*, i8** %4
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23, %19, %15, %11
%28 = load i32, i32* %5
%29 = add nsw i32 %28, -1
store i32 %29, i32* %5
br label %40
30: ; preds = %23
%31 = load i8*, i8** %4
%32 = call i64 @atoi(i8* %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
br label %39
37: ; preds = %30
%38 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %2
br label %53
39: ; preds = %34
br label %40
40: ; preds = %39, %27
%41 = load i32, i32* %5
%42 = icmp slt i32 %41, 1
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %2
br label %53
45: ; preds = %40
%46 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %46, i8** %4
br label %8
47: ; preds = %8
%48 = load i32, i32* %5
%49 = icmp eq i32 %48, 1
br i1 %49, label %50, label %51
50: ; preds = %47
store i32 1, i32* %2
br label %53
51: ; preds = %47
%52 = call i32 @puts(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %2
br label %53
53: ; preds = %51, %50, %43, %37
%54 = load i32, i32* %2
ret i32 %54
}

LLVM IR (-O2):
@.str = [3 x i8] c" \0A\00"
@.str.1 = [2 x i8] c"*\00"
@.str.2 = [2 x i8] c"/\00"
@.str.3 = [2 x i8] c"+\00"
@.str.4 = [2 x i8] c"-\00"
@.str.5 = [32 x i8] c"error: contains unknown symbols\00"
@.str.6 = [35 x i8] c"error: not a valid RPN expression\0A\00"
@str = [34 x i8] c"error: not a valid RPN expression\00"
define i32 @expValid(i8* %0) {
%2 = tail call i8* @strtok(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i8* %2, null
br i1 %3, label %36, label %4
4: ; preds = %1, %30
%5 = phi i32 [ %31, %30 ], [ 0, %1 ]
%6 = phi i8* [ %32, %30 ], [ %2, %1 ]
%7 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %25, label %9
9: ; preds = %4
%10 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %25, label %12
12: ; preds = %9
%13 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %25, label %15
15: ; preds = %12
%16 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = tail call i64 @atoi(i8* %6)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = add nuw nsw i32 %5, 1
br label %30
23: ; preds = %18
%24 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
br label %38
25: ; preds = %4, %9, %12, %15
%26 = add nsw i32 %5, -1
%27 = icmp slt i32 %5, 2
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
br label %38
30: ; preds = %21, %25
%31 = phi i32 [ %22, %21 ], [ %26, %25 ]
%32 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %4
34: ; preds = %30
%35 = icmp eq i32 %31, 1
br i1 %35, label %38, label %36
36: ; preds = %1, %34
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %34, %36, %28, %23
%39 = phi i32 [ 0, %28 ], [ 0, %23 ], [ 0, %36 ], [ 1, %34 ]
ret i32 %39
}