#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 nlmsghdr {
  int dummy;
};
struct nlattr {
  int dummy;
};

/* Variables and functions */
int NLA_F_NESTED;
struct nlattr *nh_tail(struct nlmsghdr *);
scalar_t__ nlattr_add(struct nlmsghdr *, size_t, int, int /*<<< orphan*/ *,
                      int /*<<< orphan*/);
struct nlattr *nlattr_begin_nested(struct nlmsghdr *nh, size_t maxsz,
                                   int type) {
  struct nlattr *tail;
  tail = nh_tail(nh);
  if (nlattr_add(nh, maxsz, type | NLA_F_NESTED, NULL, 0))
    return NULL;
  return tail;
}


x86 assembly (clang, -O0):
<nlattr_begin_nested>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <nh_tail@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    0x2e8e(%rip),%rax
or     (%rax),%edx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   1030 <nlattr_add@plt>
cmp    $0x0,%rax
je     1177 <nlattr_begin_nested+0x57>
movq   $0x0,-0x8(%rbp)
jmp    117f <nlattr_begin_nested+0x5f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlattr_begin_nested>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <nh_tail@plt>
mov    %rax,%r12
mov    0x2ea0(%rip),%rax
or     (%rax),%ebx
xor    %r13d,%r13d
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1030 <nlattr_add@plt>
test   %rax,%rax
cmovne %r13,%r12
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.nlattr = type { i32 }
%struct.nlmsghdr = type { i32 }
@NLA_F_NESTED = global i32 0
define %struct.nlattr* @nlattr_begin_nested(%struct.nlmsghdr* %0, i64 %1, i32 %2) {
%4 = alloca %struct.nlattr*
%5 = alloca %struct.nlmsghdr*
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.nlattr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%10 = call %struct.nlattr* @nh_tail(%struct.nlmsghdr* %9)
store %struct.nlattr* %10, %struct.nlattr** %8
%11 = load %struct.nlmsghdr*, %struct.nlmsghdr** %5
%12 = load i64, i64* %6
%13 = load i32, i32* %7
%14 = load i32, i32* @NLA_F_NESTED
%15 = or i32 %13, %14
%16 = call i64 @nlattr_add(%struct.nlmsghdr* %11, i64 %12, i32 %15, i32* null, i32 0)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store %struct.nlattr* null, %struct.nlattr** %4
br label %21
19: ; preds = %3
%20 = load %struct.nlattr*, %struct.nlattr** %8
store %struct.nlattr* %20, %struct.nlattr** %4
br label %21
21: ; preds = %19, %18
%22 = load %struct.nlattr*, %struct.nlattr** %4
ret %struct.nlattr* %22
}

LLVM IR (-O2):
%struct.nlattr = type { i32 }
%struct.nlmsghdr = type { i32 }
@NLA_F_NESTED = global i32 0
define %struct.nlattr* @nlattr_begin_nested(%struct.nlmsghdr* %0, i64 %1, i32 %2) {
%4 = tail call %struct.nlattr* @nh_tail(%struct.nlmsghdr* %0)
%5 = load i32, i32* @NLA_F_NESTED
%6 = or i32 %5, %2
%7 = tail call i64 @nlattr_add(%struct.nlmsghdr* %0, i64 %1, i32 %6, i32* null, i32 0)
%8 = icmp eq i64 %7, 0
%9 = select i1 %8, %struct.nlattr* %4, %struct.nlattr* null
ret %struct.nlattr* %9
}