#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 rtattr {
  unsigned short rta_len;
};
struct nlmsghdr {
  int /*<<< orphan*/ nlmsg_len;
};

/* Variables and functions */
int NLMSG_ALIGN(int /*<<< orphan*/);
int nlmsg_nest_end(struct nlmsghdr *nlmsg, struct rtattr *start) {
  if (nlmsg == NULL || start == NULL ||
      (char *)start >=
          (char *)((struct rtattr *)(((char *)(nlmsg)) +
                                     NLMSG_ALIGN((nlmsg)->nlmsg_len))))
    return -1;
  start->rta_len =
      (unsigned short)((char *)((
                           struct rtattr *)(((char *)(nlmsg)) +
                                            NLMSG_ALIGN((nlmsg)->nlmsg_len))) -
                       (char *)start);
  return 0;
}


x86 assembly (clang, -O0):
<nlmsg_nest_end>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     116a <nlmsg_nest_end+0x5a>
cmpq   $0x0,-0x18(%rbp)
je     116a <nlmsg_nest_end+0x5a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <NLMSG_ALIGN@plt>
mov    -0x28(%rbp),%rcx
mov    %eax,%edx
mov    -0x20(%rbp),%rax
movslq %edx,%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
jb     1176 <nlmsg_nest_end+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ad <nlmsg_nest_end+0x9d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <NLMSG_ALIGN@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    %ax,%cx
mov    -0x18(%rbp),%rax
mov    %cx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlmsg_nest_end>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     114c <nlmsg_nest_end+0x3c>
mov    %rsi,%r14
test   %rsi,%rsi
je     114c <nlmsg_nest_end+0x3c>
mov    %rdi,%rbx
mov    (%rdi),%edi
call   1030 <NLMSG_ALIGN@plt>
cltq
add    %rbx,%rax
cmp    %r14,%rax
jbe    114c <nlmsg_nest_end+0x3c>
mov    (%rbx),%edi
call   1030 <NLMSG_ALIGN@plt>
add    %eax,%ebx
sub    %r14d,%ebx
mov    %bx,(%r14)
xor    %ebp,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
%struct.rtattr = type { i16 }
define i32 @nlmsg_nest_end(%struct.nlmsghdr* %0, %struct.rtattr* %1) {
%3 = alloca i32
%4 = alloca %struct.nlmsghdr*
%5 = alloca %struct.rtattr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %4
store %struct.rtattr* %1, %struct.rtattr** %5
%6 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%7 = icmp eq %struct.nlmsghdr* %6, null
br i1 %7, label %25, label %8
8: ; preds = %2
%9 = load %struct.rtattr*, %struct.rtattr** %5
%10 = icmp eq %struct.rtattr* %9, null
br i1 %10, label %25, label %11
11: ; preds = %8
%12 = load %struct.rtattr*, %struct.rtattr** %5
%13 = bitcast %struct.rtattr* %12 to i8*
%14 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%15 = bitcast %struct.nlmsghdr* %14 to i8*
%16 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%17 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @NLMSG_ALIGN(i32 %18)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %15, i64 %20
%22 = bitcast i8* %21 to %struct.rtattr*
%23 = bitcast %struct.rtattr* %22 to i8*
%24 = icmp uge i8* %13, %23
br i1 %24, label %25, label %26
25: ; preds = %11, %8, %2
store i32 -1, i32* %3
br label %45
26: ; preds = %11
%27 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%28 = bitcast %struct.nlmsghdr* %27 to i8*
%29 = load %struct.nlmsghdr*, %struct.nlmsghdr** %4
%30 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @NLMSG_ALIGN(i32 %31)
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %28, i64 %33
%35 = bitcast i8* %34 to %struct.rtattr*
%36 = bitcast %struct.rtattr* %35 to i8*
%37 = load %struct.rtattr*, %struct.rtattr** %5
%38 = bitcast %struct.rtattr* %37 to i8*
%39 = ptrtoint i8* %36 to i64
%40 = ptrtoint i8* %38 to i64
%41 = sub i64 %39, %40
%42 = trunc i64 %41 to i16
%43 = load %struct.rtattr*, %struct.rtattr** %5
%44 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %43, i32 0, i32 0
store i16 %42, i16* %44
store i32 0, i32* %3
br label %45
45: ; preds = %26, %25
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.rtattr = type { i16 }
define i32 @nlmsg_nest_end(%struct.nlmsghdr* %0, %struct.rtattr* %1) {
%3 = icmp eq %struct.nlmsghdr* %0, null
%4 = icmp eq %struct.rtattr* %1, null
%5 = or i1 %3, %4
br i1 %5, label %25, label %6
6: ; preds = %2
%7 = bitcast %struct.rtattr* %1 to i8*
%8 = bitcast %struct.nlmsghdr* %0 to i8*
%9 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %0, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @NLMSG_ALIGN(i32 %10)
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = icmp ugt i8* %13, %7
br i1 %14, label %15, label %25
15: ; preds = %6
%16 = load i32, i32* %9
%17 = tail call i32 @NLMSG_ALIGN(i32 %16)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %8, i64 %18
%20 = ptrtoint i8* %19 to i64
%21 = ptrtoint %struct.rtattr* %1 to i64
%22 = sub i64 %20, %21
%23 = trunc i64 %22 to i16
%24 = getelementptr inbounds %struct.rtattr, %struct.rtattr* %1, i64 0, i32 0
store i16 %23, i16* %24
br label %25
25: ; preds = %2, %6, %15
%26 = phi i32 [ 0, %15 ], [ -1, %6 ], [ -1, %2 ]
ret i32 %26
}