#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 rtgenmsg {
  int rtgen_family;
};
struct nlmsg {
  int dummy;
};
typedef int /*<<< orphan*/ g;

/* Variables and functions */
int /*<<< orphan*/ nlmsg_free(struct nlmsg *);
struct nlmsg *nlmsg_new(int, int);
scalar_t__ nlmsg_put_padded(struct nlmsg *, struct rtgenmsg *, int, int);
struct nlmsg *rtnlmsg_new(int type, int family, int flags, int size) {
  struct rtgenmsg g = {.rtgen_family = family};
  struct nlmsg *res;
  res = nlmsg_new(type, flags);
  if (!res)
    return NULL;
  if (nlmsg_put_padded(res, &g, sizeof(g), size)) {
    nlmsg_free(res);
    return NULL;
  }
  return res;
}


x86 assembly (clang, -O0):
<rtnlmsg_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <nlmsg_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1171 <rtnlmsg_new+0x41>
movq   $0x0,-0x8(%rbp)
jmp    11ae <rtnlmsg_new+0x7e>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%ecx
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <nlmsg_put_padded@plt>
cmp    $0x0,%rax
je     11a6 <rtnlmsg_new+0x76>
mov    -0x28(%rbp),%rdi
call   1040 <nlmsg_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ae <rtnlmsg_new+0x7e>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtnlmsg_new>:
push   %rbp
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %esi,(%rsp)
mov    %edx,%esi
call   1050 <nlmsg_new@plt>
test   %rax,%rax
je     1166 <rtnlmsg_new+0x36>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
mov    $0x4,%edx
mov    %ebp,%ecx
call   1030 <nlmsg_put_padded@plt>
test   %rax,%rax
je     1168 <rtnlmsg_new+0x38>
mov    %rbx,%rdi
call   1040 <nlmsg_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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