#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 */
struct variablelist {
  int dummy;
};
struct union_number {
  int dummy;
};

/* Variables and functions */
int EINVAL;
int incalc(char const **, struct variablelist *, struct union_number *);
int strlen(char const *);
int calc(const char *str, struct variablelist *arg, struct union_number *ret) {
  const char *end = str + strlen(str);
  int err = incalc(&str, arg, ret);
  return err ? err : end != str ? EINVAL : 0;
}


x86 assembly (clang, -O0):
<calc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    -0x8(%rbp),%rdi
call   1030 <incalc@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     117e <calc+0x5e>
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
jmp    11ad <calc+0x8d>
mov    -0x20(%rbp),%rax
cmp    -0x8(%rbp),%rax
je     119d <calc+0x7d>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
jmp    11a7 <calc+0x87>
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
jmp    11a7 <calc+0x87>
mov    -0x38(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
mov    %rdi,(%rsp)
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %rsp,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1030 <incalc@plt>
test   %eax,%eax
jne    1165 <calc+0x45>
movslq %r14d,%rax
add    %rax,%rbx
xor    %eax,%eax
cmp    (%rsp),%rbx
je     1165 <calc+0x45>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.variablelist = type { i32 }
%struct.union_number = type { i32 }
@EINVAL = global i32 0
define i32 @calc(i8* %0, %struct.variablelist* %1, %struct.union_number* %2) {
%4 = alloca i8*
%5 = alloca %struct.variablelist*
%6 = alloca %struct.union_number*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store %struct.variablelist* %1, %struct.variablelist** %5
store %struct.union_number* %2, %struct.union_number** %6
%9 = load i8*, i8** %4
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %9, i64 %12
store i8* %13, i8** %7
%14 = load %struct.variablelist*, %struct.variablelist** %5
%15 = load %struct.union_number*, %struct.union_number** %6
%16 = call i32 @incalc(i8** %4, %struct.variablelist* %14, %struct.union_number* %15)
store i32 %16, i32* %8
%17 = load i32, i32* %8
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = load i32, i32* %8
br label %30
21: ; preds = %3
%22 = load i8*, i8** %7
%23 = load i8*, i8** %4
%24 = icmp ne i8* %22, %23
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = load i32, i32* @EINVAL
br label %28
27: ; preds = %21
br label %28
28: ; preds = %27, %25
%29 = phi i32 [ %26, %25 ], [ 0, %27 ]
br label %30
30: ; preds = %28, %19
%31 = phi i32 [ %20, %19 ], [ %29, %28 ]
ret i32 %31
}

LLVM IR (-O2):
%struct.variablelist = type { i32 }
%struct.union_number = type { i32 }
@EINVAL = global i32 0
define i32 @calc(i8* %0, %struct.variablelist* %1, %struct.union_number* %2) {
%4 = alloca i8*
store i8* %0, i8** %4
%5 = tail call i32 @strlen(i8* %0)
%6 = call i32 @incalc(i8** %4, %struct.variablelist* %1, %struct.union_number* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %3
%9 = sext i32 %5 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8*, i8** %4
%12 = icmp eq i8* %10, %11
%13 = load i32, i32* @EINVAL
%14 = select i1 %12, i32 0, i32 %13
br label %15
15: ; preds = %3, %8
%16 = phi i32 [ %14, %8 ], [ %6, %3 ]
ret i32 %16
}