#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 nlmsg_type;
  int nlmsg_flags;
};
struct nlmsg {
  int dummy;
};
typedef int /*<<< orphan*/ hdr;

/* Variables and functions */
int NLM_F_REQUEST;
struct nlmsg *nlmsg_alloc(int);
int /*<<< orphan*/ nlmsg_free(struct nlmsg *);
scalar_t__ nlmsg_put(struct nlmsg *, struct nlmsghdr *, int);
struct nlmsg *nlmsg_new(int type, int flags) {
  struct nlmsghdr hdr = {.nlmsg_type = type, .nlmsg_flags = flags};
  struct nlmsg *msg;
  hdr.nlmsg_flags |= NLM_F_REQUEST;
  msg = nlmsg_alloc(16384);
  if (!msg)
    return NULL;
  if (nlmsg_put(msg, &hdr, sizeof(hdr))) {
    nlmsg_free(msg);
    return NULL;
  }
  return msg;
}


x86 assembly (clang, -O0):
<nlmsg_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    $0x4000,%edi
call   1040 <nlmsg_alloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117f <nlmsg_new+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    11b9 <nlmsg_new+0x89>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x8,%edx
call   1030 <nlmsg_put@plt>
cmp    $0x0,%rax
je     11b1 <nlmsg_new+0x81>
mov    -0x20(%rbp),%rdi
call   1050 <nlmsg_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b9 <nlmsg_new+0x89>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nlmsg_new>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,0x8(%rsp)
mov    0x2ea8(%rip),%rax
or     (%rax),%esi
mov    %esi,0xc(%rsp)
mov    $0x4000,%edi
call   1040 <nlmsg_alloc@plt>
test   %rax,%rax
je     1177 <nlmsg_new+0x47>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0x8,%edx
call   1030 <nlmsg_put@plt>
test   %rax,%rax
je     1179 <nlmsg_new+0x49>
mov    %rbx,%rdi
call   1050 <nlmsg_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.nlmsg = type { i32 }
%struct.nlmsghdr = type { i32, i32 }
@NLM_F_REQUEST = global i32 0
define %struct.nlmsg* @nlmsg_new(i32 %0, i32 %1) {
%3 = alloca %struct.nlmsg*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.nlmsghdr
%7 = alloca %struct.nlmsg*
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %6, i32 0, i32 0
%9 = load i32, i32* %4
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %6, i32 0, i32 1
%11 = load i32, i32* %5
store i32 %11, i32* %10
%12 = load i32, i32* @NLM_F_REQUEST
%13 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %6, i32 0, i32 1
%14 = load i32, i32* %13
%15 = or i32 %14, %12
store i32 %15, i32* %13
%16 = call %struct.nlmsg* @nlmsg_alloc(i32 16384)
store %struct.nlmsg* %16, %struct.nlmsg** %7
%17 = load %struct.nlmsg*, %struct.nlmsg** %7
%18 = icmp ne %struct.nlmsg* %17, null
br i1 %18, label %20, label %19
19: ; preds = %2
store %struct.nlmsg* null, %struct.nlmsg** %3
br label %29
20: ; preds = %2
%21 = load %struct.nlmsg*, %struct.nlmsg** %7
%22 = call i64 @nlmsg_put(%struct.nlmsg* %21, %struct.nlmsghdr* %6, i32 8)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load %struct.nlmsg*, %struct.nlmsg** %7
%26 = call i32 @nlmsg_free(%struct.nlmsg* %25)
store %struct.nlmsg* null, %struct.nlmsg** %3
br label %29
27: ; preds = %20
%28 = load %struct.nlmsg*, %struct.nlmsg** %7
store %struct.nlmsg* %28, %struct.nlmsg** %3
br label %29
29: ; preds = %27, %24, %19
%30 = load %struct.nlmsg*, %struct.nlmsg** %3
ret %struct.nlmsg* %30
}

LLVM IR (-O2):
%struct.nlmsg = type { i32 }
%struct.nlmsghdr = type { i32, i32 }
@NLM_F_REQUEST = global i32 0
define %struct.nlmsg* @nlmsg_new(i32 %0, i32 %1) {
%3 = alloca %struct.nlmsghdr
%4 = bitcast %struct.nlmsghdr* %3 to i8*
%5 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %3, i64 0, i32 0
store i32 %0, i32* %5
%6 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %3, i64 0, i32 1
%7 = load i32, i32* @NLM_F_REQUEST
%8 = or i32 %7, %1
store i32 %8, i32* %6
%9 = tail call %struct.nlmsg* @nlmsg_alloc(i32 16384)
%10 = icmp eq %struct.nlmsg* %9, null
br i1 %10, label %16, label %11
11: ; preds = %2
%12 = call i64 @nlmsg_put(%struct.nlmsg* %9, %struct.nlmsghdr* %3, i32 8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @nlmsg_free(%struct.nlmsg* %9)
br label %16
16: ; preds = %11, %2, %14
%17 = phi %struct.nlmsg* [ null, %14 ], [ null, %2 ], [ %9, %11 ]
ret %struct.nlmsg* %17
}