#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 */
char **brkstring(char *, char *, char *);
int /*<<< orphan*/ free(char *);
char *getcpy(char *);
int m_atoi(char *);
char *strchr(char *, char);
int count_messages(char *field) {
  int total = 0;
  int j, k;
  char *cp, **ap;
  field = getcpy(field);
  for (ap = brkstring(field, " ", "\n"); *ap; ap++) {
    if ((cp = strchr(*ap, '-')))
      *cp++ = '\0';
    if ((j = m_atoi(*ap)) > 0) {
      k = cp ? m_atoi(cp) : j;
      total += k - j + 1;
    }
  }
  free(field);
  return total;
}


x86 assembly (clang, -O0):
<count_messages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <getcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe85(%rip),%rsi
lea    0xe80(%rip),%rdx
call   1070 <brkstring@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
je     122f <count_messages+0xdf>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2d,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11ca <count_messages+0x7a>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1050 <m_atoi@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    1219 <count_messages+0xc9>
cmpq   $0x0,-0x20(%rbp)
je     11fe <count_messages+0xae>
mov    -0x20(%rbp),%rdi
call   1050 <m_atoi@plt>
mov    %eax,-0x2c(%rbp)
jmp    1204 <count_messages+0xb4>
mov    -0x10(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    -0x10(%rbp),%eax
add    $0x1,%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    121e <count_messages+0xce>
mov    -0x28(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x28(%rbp)
jmp    118b <count_messages+0x3b>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_messages>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
call   1060 <getcpy@plt>
mov    %rax,%r14
lea    0xe98(%rip),%rsi
lea    0xe93(%rip),%rdx
mov    %rax,%rdi
call   1070 <brkstring@plt>
mov    (%rax),%rbx
test   %rbx,%rbx
je     11eb <count_messages+0x9b>
mov    %rax,%r12
add    $0x8,%r12
xor    %r15d,%r15d
jmp    11b5 <count_messages+0x65>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1050 <m_atoi@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    11a8 <count_messages+0x58>
sub    %ebx,%r15d
add    %eax,%r15d
add    $0x1,%r15d
mov    (%r12),%rbx
add    $0x8,%r12
test   %rbx,%rbx
je     11ee <count_messages+0x9e>
mov    %rbx,%rdi
mov    $0x2d,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1190 <count_messages+0x40>
mov    %rax,%r13
movb   $0x0,(%rax)
mov    -0x8(%r12),%rdi
call   1050 <m_atoi@plt>
test   %eax,%eax
jle    11a8 <count_messages+0x58>
mov    %eax,%ebx
add    $0x1,%r13
mov    %r13,%rdi
call   1050 <m_atoi@plt>
jmp    119e <count_messages+0x4e>
xor    %r15d,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c" \00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @count_messages(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
store i8* %0, i8** %2
store i32 0, i32* %3
%8 = load i8*, i8** %2
%9 = call i8* @getcpy(i8* %8)
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = call i8** @brkstring(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8** %11, i8*** %7
br label %12
12: ; preds = %46, %1
%13 = load i8**, i8*** %7
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %49
16: ; preds = %12
%17 = load i8**, i8*** %7
%18 = load i8*, i8** %17
%19 = call i8* @strchr(i8* %18, i8 signext 45)
store i8* %19, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i8*, i8** %6
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %6
store i8 0, i8* %22
br label %24
24: ; preds = %21, %16
%25 = load i8**, i8*** %7
%26 = load i8*, i8** %25
%27 = call i32 @m_atoi(i8* %26)
store i32 %27, i32* %4
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %45
29: ; preds = %24
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8*, i8** %6
%34 = call i32 @m_atoi(i8* %33)
br label %37
35: ; preds = %29
%36 = load i32, i32* %4
br label %37
37: ; preds = %35, %32
%38 = phi i32 [ %34, %32 ], [ %36, %35 ]
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = load i32, i32* %4
%41 = sub nsw i32 %39, %40
%42 = add nsw i32 %41, 1
%43 = load i32, i32* %3
%44 = add nsw i32 %43, %42
store i32 %44, i32* %3
br label %45
45: ; preds = %37, %24
br label %46
46: ; preds = %45
%47 = load i8**, i8*** %7
%48 = getelementptr inbounds i8*, i8** %47, i32 1
store i8** %48, i8*** %7
br label %12
49: ; preds = %12
%50 = load i8*, i8** %2
%51 = call i32 @free(i8* %50)
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
@.str.1 = [2 x i8] c"\0A\00"
define i32 @count_messages(i8* %0) {
%2 = tail call i8* @getcpy(i8* %0)
%3 = tail call i8** @brkstring(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = load i8*, i8** %3
%5 = icmp eq i8* %4, null
br i1 %5, label %33, label %6
6: ; preds = %1, %28
%7 = phi i8* [ %31, %28 ], [ %4, %1 ]
%8 = phi i8** [ %30, %28 ], [ %3, %1 ]
%9 = phi i32 [ %29, %28 ], [ 0, %1 ]
%10 = tail call i8* @strchr(i8* %7, i8 signext 45)
%11 = icmp eq i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %6
store i8 0, i8* %10
%13 = load i8*, i8** %8
%14 = tail call i32 @m_atoi(i8* %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %19, label %28
16: ; preds = %6
%17 = tail call i32 @m_atoi(i8* %7)
%18 = icmp sgt i32 %17, 0
br i1 %18, label %22, label %28
19: ; preds = %12
%20 = getelementptr inbounds i8, i8* %10, i64 1
%21 = tail call i32 @m_atoi(i8* %20)
br label %22
22: ; preds = %16, %19
%23 = phi i32 [ %14, %19 ], [ %17, %16 ]
%24 = phi i32 [ %21, %19 ], [ %17, %16 ]
%25 = add i32 %9, 1
%26 = sub i32 %25, %23
%27 = add i32 %26, %24
br label %28
28: ; preds = %16, %12, %22
%29 = phi i32 [ %27, %22 ], [ %9, %12 ], [ %9, %16 ]
%30 = getelementptr inbounds i8*, i8** %8, i64 1
%31 = load i8*, i8** %30
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %6
33: ; preds = %28, %1
%34 = phi i32 [ 0, %1 ], [ %29, %28 ]
%35 = tail call i32 @free(i8* %2)
ret i32 %34
}