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

/* Variables and functions */
int /*<<< orphan*/ nlmsg_free(struct nlmsg *);
struct nlmsg *nlmsg_new(int, int);
scalar_t__ nlmsg_put(struct nlmsg *, struct genlmsghdr *, int);
struct nlmsg *genlmsg_new(int type, int cmd, int flags) {
  struct genlmsghdr g = {.cmd = cmd, .version = 1};
  struct nlmsg *res;
  res = nlmsg_new(type, flags);
  if (!res)
    return NULL;
  if (nlmsg_put(res, &g, sizeof(g))) {
    nlmsg_free(res);
    return NULL;
  }
  return res;
}


x86 assembly (clang, -O0):
<genlmsg_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <nlmsg_new@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1175 <genlmsg_new+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11af <genlmsg_new+0x7f>
mov    -0x28(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1030 <nlmsg_put@plt>
cmp    $0x0,%rax
je     11a7 <genlmsg_new+0x77>
mov    -0x28(%rbp),%rdi
call   1040 <nlmsg_free@plt>
movq   $0x0,-0x8(%rbp)
jmp    11af <genlmsg_new+0x7f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<genlmsg_new>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,0x8(%rsp)
movl   $0x1,0xc(%rsp)
mov    %edx,%esi
call   1050 <nlmsg_new@plt>
test   %rax,%rax
je     116f <genlmsg_new+0x3f>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
mov    $0x8,%edx
call   1030 <nlmsg_put@plt>
test   %rax,%rax
je     1171 <genlmsg_new+0x41>
mov    %rbx,%rdi
call   1040 <nlmsg_free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.nlmsg = type { i32 }
%struct.genlmsghdr = type { i32, i32 }
define %struct.nlmsg* @genlmsg_new(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.genlmsghdr
%5 = bitcast %struct.genlmsghdr* %4 to i8*
%6 = getelementptr inbounds %struct.genlmsghdr, %struct.genlmsghdr* %4, i64 0, i32 0
store i32 %1, i32* %6
%7 = getelementptr inbounds %struct.genlmsghdr, %struct.genlmsghdr* %4, i64 0, i32 1
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 = %3
%11 = call i64 @nlmsg_put(%struct.nlmsg* %8, %struct.genlmsghdr* %4, i32 8)
%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, %3, %13
%16 = phi %struct.nlmsg* [ null, %13 ], [ null, %3 ], [ %8, %10 ]
ret %struct.nlmsg* %16
}