#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 NetFrame {
  int dummy;
};
struct Ferret {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ FRAMERR(struct NetFrame *, char *);
void extract_item(struct Ferret *ferret, struct NetFrame *frame,
                  const unsigned char *px, unsigned length, unsigned *r_offset,
                  const unsigned char **r_object, unsigned *r_object_len) {
  unsigned len;
  ferret = (ferret);
  if (*r_offset + 1 > length) {
    FRAMERR(frame, "truncated\n");
    *r_offset = length + 1;
    return;
  }
  len = px[*r_offset];
  (*r_offset)++;
  if (*r_offset + len > length)
    len = length - *r_offset;
  *r_object = px + *r_offset;
  *r_object_len = len;
  *r_offset += len;
}


x86 assembly (clang, -O0):
<extract_item>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
add    $0x1,%eax
cmp    -0x1c(%rbp),%eax
jbe    116e <extract_item+0x5e>
mov    -0x10(%rbp),%rdi
lea    0xea8(%rip),%rsi
call   1030 <FRAMERR@plt>
mov    -0x1c(%rbp),%ecx
add    $0x1,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
jmp    11d0 <extract_item+0xc0>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%ecx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
add    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jbe    11a8 <extract_item+0x98>
mov    -0x1c(%rbp),%eax
mov    -0x28(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
add    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%ecx
mov    0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x34(%rbp),%ecx
mov    -0x28(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_item>:
push   %rbp
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %ecx,%ebp
mov    (%r8),%ecx
lea    0x1(%rcx),%eax
cmp    %ebp,%eax
jbe    1139 <extract_item+0x29>
lea    0xed7(%rip),%rax
mov    %rsi,%rdi
mov    %rax,%rsi
call   1030 <FRAMERR@plt>
add    $0x1,%ebp
jmp    115e <extract_item+0x4e>
movzbl (%rdx,%rcx,1),%ecx
lea    (%rax,%rcx,1),%esi
mov    %ebp,%edi
sub    %eax,%edi
cmp    %ebp,%esi
cmovbe %ecx,%edi
mov    0x20(%rsp),%rcx
mov    %eax,(%rbx)
mov    %eax,%eax
add    %rdx,%rax
mov    %rax,(%r9)
mov    %edi,(%rcx)
add    (%rbx),%edi
mov    %edi,%ebp
mov    %ebp,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.Ferret = type { i32 }
%struct.NetFrame = type { i32 }
@.str = [11 x i8] c"truncated\0A\00"
define void @extract_item(%struct.Ferret* %0, %struct.NetFrame* %1, i8* %2, i32 %3, i32* %4, i8** %5, i32* %6) {
%8 = alloca %struct.Ferret*
%9 = alloca %struct.NetFrame*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i8**
%14 = alloca i32*
%15 = alloca i32
store %struct.Ferret* %0, %struct.Ferret** %8
store %struct.NetFrame* %1, %struct.NetFrame** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i32* %4, i32** %12
store i8** %5, i8*** %13
store i32* %6, i32** %14
%16 = load %struct.Ferret*, %struct.Ferret** %8
store %struct.Ferret* %16, %struct.Ferret** %8
%17 = load i32*, i32** %12
%18 = load i32, i32* %17
%19 = add i32 %18, 1
%20 = load i32, i32* %11
%21 = icmp ugt i32 %19, %20
br i1 %21, label %22, label %28
22: ; preds = %7
%23 = load %struct.NetFrame*, %struct.NetFrame** %9
%24 = call i32 @FRAMERR(%struct.NetFrame* %23, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %11
%26 = add i32 %25, 1
%27 = load i32*, i32** %12
store i32 %26, i32* %27
br label %63
28: ; preds = %7
%29 = load i8*, i8** %10
%30 = load i32*, i32** %12
%31 = load i32, i32* %30
%32 = zext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
%34 = load i8, i8* %33
%35 = zext i8 %34 to i32
store i32 %35, i32* %15
%36 = load i32*, i32** %12
%37 = load i32, i32* %36
%38 = add i32 %37, 1
store i32 %38, i32* %36
%39 = load i32*, i32** %12
%40 = load i32, i32* %39
%41 = load i32, i32* %15
%42 = add i32 %40, %41
%43 = load i32, i32* %11
%44 = icmp ugt i32 %42, %43
br i1 %44, label %45, label %50
45: ; preds = %28
%46 = load i32, i32* %11
%47 = load i32*, i32** %12
%48 = load i32, i32* %47
%49 = sub i32 %46, %48
store i32 %49, i32* %15
br label %50
50: ; preds = %45, %28
%51 = load i8*, i8** %10
%52 = load i32*, i32** %12
%53 = load i32, i32* %52
%54 = zext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %51, i64 %54
%56 = load i8**, i8*** %13
store i8* %55, i8** %56
%57 = load i32, i32* %15
%58 = load i32*, i32** %14
store i32 %57, i32* %58
%59 = load i32, i32* %15
%60 = load i32*, i32** %12
%61 = load i32, i32* %60
%62 = add i32 %61, %59
store i32 %62, i32* %60
br label %63
63: ; preds = %50, %22
ret void
}

LLVM IR (-O2):
%struct.Ferret = type { i32 }
%struct.NetFrame = type { i32 }
@.str = [11 x i8] c"truncated\0A\00"
define void @extract_item(%struct.Ferret* nocapture readnone %0, %struct.NetFrame* %1, i8* %2, i32 %3, i32* nocapture %4, i8** nocapture writeonly %5, i32* nocapture writeonly %6) {
%8 = load i32, i32* %4
%9 = add i32 %8, 1
%10 = icmp ugt i32 %9, %3
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = tail call i32 @FRAMERR(%struct.NetFrame* %1, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%13 = add nuw i32 %3, 1
br label %27
14: ; preds = %7
%15 = zext i32 %8 to i64
%16 = getelementptr inbounds i8, i8* %2, i64 %15
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
store i32 %9, i32* %4
%19 = add i32 %9, %18
%20 = icmp ugt i32 %19, %3
%21 = sub i32 %3, %9
%22 = select i1 %20, i32 %21, i32 %18
%23 = zext i32 %9 to i64
%24 = getelementptr inbounds i8, i8* %2, i64 %23
store i8* %24, i8** %5
store i32 %22, i32* %6
%25 = load i32, i32* %4
%26 = add i32 %25, %22
br label %27
27: ; preds = %14, %11
%28 = phi i32 [ %26, %14 ], [ %13, %11 ]
store i32 %28, i32* %4
ret void
}