#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 buffer {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ buffer_current(struct buffer *);
int /*<<< orphan*/ buffer_forward(struct buffer *, size_t);
char buffer_getc(struct buffer *);
size_t buffer_len(struct buffer *);
size_t buffer_pos(struct buffer *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, size_t);
int sscanf(int /*<<< orphan*/, char *, size_t *, int *);
int parse_netstring(struct buffer *stream, char **buffer, size_t *length) {
  char *buf;
  size_t len;
  int rv, nscanf;
  rv = sscanf(buffer_current(stream), "%9lu%n", &len, &nscanf);
  if (rv != 1 || nscanf <= 0)
    return (0x0003);
  else
    buffer_forward(stream, nscanf);
  if (buffer_getc(stream) != ':')
    return (0x0001);
  buf = (char *)malloc(len + 1);
  if (!buf)
    return (0x0002);
  if (buffer_len(stream) - buffer_pos(stream) < len + 1) {
    free(buf);
    return (0x0003);
  } else {
    memcpy(buf, buffer_current(stream), len);
    buffer_forward(stream, len);
  }
  if (buffer_getc(stream) != ',') {
    free(buf);
    return (0x0001);
  }
  if (buffer)
    *buffer = buf;
  if (length)
    *length = len;
  return (0);
}


x86 assembly (clang, -O0):
<parse_netstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <buffer_current@plt>
mov    %eax,%edi
lea    0xe4a(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <sscanf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
jne    11da <parse_netstring+0x4a>
cmpl   $0x0,-0x38(%rbp)
jg     11e6 <parse_netstring+0x56>
movl   $0x3,-0x4(%rbp)
jmp    130c <parse_netstring+0x17c>
mov    -0x10(%rbp),%rdi
movslq -0x38(%rbp),%rsi
call   10b0 <buffer_forward@plt>
mov    -0x10(%rbp),%rdi
call   1090 <buffer_getc@plt>
movsbl %al,%eax
cmp    $0x3a,%eax
je     1214 <parse_netstring+0x84>
movl   $0x1,-0x4(%rbp)
jmp    130c <parse_netstring+0x17c>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123c <parse_netstring+0xac>
movl   $0x2,-0x4(%rbp)
jmp    130c <parse_netstring+0x17c>
mov    -0x10(%rbp),%rdi
call   1080 <buffer_len@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <buffer_pos@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    -0x30(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jae    1282 <parse_netstring+0xf2>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x3,-0x4(%rbp)
jmp    130c <parse_netstring+0x17c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <buffer_current@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10b0 <buffer_forward@plt>
mov    -0x10(%rbp),%rdi
call   1090 <buffer_getc@plt>
movsbl %al,%eax
cmp    $0x2c,%eax
je     12d9 <parse_netstring+0x149>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
movl   $0x1,-0x4(%rbp)
jmp    130c <parse_netstring+0x17c>
cmpq   $0x0,-0x18(%rbp)
je     12ef <parse_netstring+0x15f>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x20(%rbp)
je     1305 <parse_netstring+0x175>
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_netstring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1030 <buffer_current@plt>
lea    0xe4d(%rip),%rsi
lea    0x8(%rsp),%rdx
lea    0x14(%rsp),%rcx
mov    %eax,%edi
call   1070 <sscanf@plt>
mov    $0x3,%ebp
cmp    $0x1,%eax
jne    1297 <parse_netstring+0x107>
mov    0x14(%rsp),%esi
test   %esi,%esi
jle    1297 <parse_netstring+0x107>
mov    %rbx,%rdi
call   10b0 <buffer_forward@plt>
mov    %rbx,%rdi
call   1090 <buffer_getc@plt>
cmp    $0x3a,%al
jne    1292 <parse_netstring+0x102>
mov    0x8(%rsp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     123a <parse_netstring+0xaa>
mov    %rax,%r12
mov    %rbx,%rdi
call   1080 <buffer_len@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   10a0 <buffer_pos@plt>
sub    %rax,%r13
mov    0x8(%rsp),%rax
add    $0x1,%rax
cmp    %rax,%r13
jae    1241 <parse_netstring+0xb1>
mov    %r12,%rdi
call   1040 <free@plt>
jmp    1297 <parse_netstring+0x107>
mov    $0x2,%ebp
jmp    1297 <parse_netstring+0x107>
mov    %rbx,%rdi
call   1030 <buffer_current@plt>
mov    0x8(%rsp),%rdx
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   10b0 <buffer_forward@plt>
mov    %rbx,%rdi
call   1090 <buffer_getc@plt>
cmp    $0x2c,%al
jne    128a <parse_netstring+0xfa>
test   %r15,%r15
je     1279 <parse_netstring+0xe9>
mov    %r12,(%r15)
xor    %ebp,%ebp
test   %r14,%r14
je     1297 <parse_netstring+0x107>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
jmp    1297 <parse_netstring+0x107>
mov    %r12,%rdi
call   1040 <free@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.buffer = type { i32 }
@.str = [7 x i8] c"%9lu%n\00"
define i32 @parse_netstring(%struct.buffer* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca %struct.buffer*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store %struct.buffer* %0, %struct.buffer** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%12 = load %struct.buffer*, %struct.buffer** %5
%13 = call i32 @buffer_current(%struct.buffer* %12)
%14 = call i32 @sscanf(i32 %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64* %9, i32* %11)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp ne i32 %15, 1
br i1 %16, label %20, label %17
17: ; preds = %3
%18 = load i32, i32* %11
%19 = icmp sle i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17, %3
store i32 3, i32* %4
br label %82
21: ; preds = %17
%22 = load %struct.buffer*, %struct.buffer** %5
%23 = load i32, i32* %11
%24 = sext i32 %23 to i64
%25 = call i32 @buffer_forward(%struct.buffer* %22, i64 %24)
br label %26
26: ; preds = %21
%27 = load %struct.buffer*, %struct.buffer** %5
%28 = call signext i8 @buffer_getc(%struct.buffer* %27)
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 58
br i1 %30, label %31, label %32
31: ; preds = %26
store i32 1, i32* %4
br label %82
32: ; preds = %26
%33 = load i64, i64* %9
%34 = add i64 %33, 1
%35 = call i64 @malloc(i64 %34)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %8
%37 = load i8*, i8** %8
%38 = icmp ne i8* %37, null
br i1 %38, label %40, label %39
39: ; preds = %32
store i32 2, i32* %4
br label %82
40: ; preds = %32
%41 = load %struct.buffer*, %struct.buffer** %5
%42 = call i64 @buffer_len(%struct.buffer* %41)
%43 = load %struct.buffer*, %struct.buffer** %5
%44 = call i64 @buffer_pos(%struct.buffer* %43)
%45 = sub i64 %42, %44
%46 = load i64, i64* %9
%47 = add i64 %46, 1
%48 = icmp ult i64 %45, %47
br i1 %48, label %49, label %52
49: ; preds = %40
%50 = load i8*, i8** %8
%51 = call i32 @free(i8* %50)
store i32 3, i32* %4
br label %82
52: ; preds = %40
%53 = load i8*, i8** %8
%54 = load %struct.buffer*, %struct.buffer** %5
%55 = call i32 @buffer_current(%struct.buffer* %54)
%56 = load i64, i64* %9
%57 = call i32 @memcpy(i8* %53, i32 %55, i64 %56)
%58 = load %struct.buffer*, %struct.buffer** %5
%59 = load i64, i64* %9
%60 = call i32 @buffer_forward(%struct.buffer* %58, i64 %59)
br label %61
61: ; preds = %52
%62 = load %struct.buffer*, %struct.buffer** %5
%63 = call signext i8 @buffer_getc(%struct.buffer* %62)
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %64, 44
br i1 %65, label %66, label %69
66: ; preds = %61
%67 = load i8*, i8** %8
%68 = call i32 @free(i8* %67)
store i32 1, i32* %4
br label %82
69: ; preds = %61
%70 = load i8**, i8*** %6
%71 = icmp ne i8** %70, null
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = load i8*, i8** %8
%74 = load i8**, i8*** %6
store i8* %73, i8** %74
br label %75
75: ; preds = %72, %69
%76 = load i64*, i64** %7
%77 = icmp ne i64* %76, null
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i64, i64* %9
%80 = load i64*, i64** %7
store i64 %79, i64* %80
br label %81
81: ; preds = %78, %75
store i32 0, i32* %4
br label %82
82: ; preds = %81, %66, %49, %39, %31, %20
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
%struct.buffer = type { i32 }
@.str = [7 x i8] c"%9lu%n\00"
define i32 @parse_netstring(%struct.buffer* %0, i8** writeonly %1, i64* writeonly %2) {
%4 = alloca i64
%5 = alloca i32
%6 = bitcast i64* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = tail call i32 @buffer_current(%struct.buffer* %0)
%9 = call i32 @sscanf(i32 %8, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i64* %4, i32* %5)
%10 = icmp ne i32 %9, 1
%11 = load i32, i32* %5
%12 = icmp slt i32 %11, 1
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %51, label %14
14: ; preds = %3
%15 = zext i32 %11 to i64
%16 = call i32 @buffer_forward(%struct.buffer* %0, i64 %15)
%17 = call signext i8 @buffer_getc(%struct.buffer* %0)
%18 = icmp eq i8 %17, 58
br i1 %18, label %19, label %51
19: ; preds = %14
%20 = load i64, i64* %4
%21 = add i64 %20, 1
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
br i1 %24, label %51, label %25
25: ; preds = %19
%26 = call i64 @buffer_len(%struct.buffer* %0)
%27 = call i64 @buffer_pos(%struct.buffer* %0)
%28 = sub i64 %26, %27
%29 = load i64, i64* %4
%30 = add i64 %29, 1
%31 = icmp ult i64 %28, %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @free(i8* %23)
br label %51
34: ; preds = %25
%35 = call i32 @buffer_current(%struct.buffer* %0)
%36 = load i64, i64* %4
%37 = call i32 @memcpy(i8* %23, i32 %35, i64 %36)
%38 = load i64, i64* %4
%39 = call i32 @buffer_forward(%struct.buffer* %0, i64 %38)
%40 = call signext i8 @buffer_getc(%struct.buffer* %0)
%41 = icmp eq i8 %40, 44
br i1 %41, label %44, label %42
42: ; preds = %34
%43 = call i32 @free(i8* %23)
br label %51
44: ; preds = %34
%45 = icmp eq i8** %1, null
br i1 %45, label %47, label %46
46: ; preds = %44
store i8* %23, i8** %1
br label %47
47: ; preds = %46, %44
%48 = icmp eq i64* %2, null
br i1 %48, label %51, label %49
49: ; preds = %47
%50 = load i64, i64* %4
store i64 %50, i64* %2
br label %51
51: ; preds = %47, %49, %19, %14, %3, %42, %32
%52 = phi i32 [ 3, %32 ], [ 1, %42 ], [ 3, %3 ], [ 1, %14 ], [ 2, %19 ], [ 0, %49 ], [ 0, %47 ]
ret i32 %52
}