#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 sockaddr_nl {
  void *nl_pid;
  void *nl_family;
};
struct sockaddr {
  int dummy;
};
struct nlmsghdr {
  int nlmsg_type;
  int /*<<< orphan*/ nlmsg_len;
  void *nlmsg_pid;
  scalar_t__ nlmsg_seq;
  int /*<<< orphan*/ nlmsg_flags;
};
typedef int /*<<< orphan*/ saddr;
typedef int /*<<< orphan*/ daddr;

/* Variables and functions */
void *AF_NETLINK;
int /*<<< orphan*/ NLMSG_DATA(struct nlmsghdr *);
int /*<<< orphan*/ NLMSG_LENGTH(int);
int /*<<< orphan*/ NLMSG_SPACE(int);
int /*<<< orphan*/ NLM_F_REQUEST;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(struct nlmsghdr *);
void *getpid();
scalar_t__ malloc(int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_nl *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int sendto(int, struct nlmsghdr *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct sockaddr *, int);
int socket(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int strlen(char *);
int main(int argc, char *argv[]) {
  int fd;
  int res;
  int len;
  struct nlmsghdr *nlh = NULL;
  struct sockaddr_nl saddr, daddr;
  char *msg = "hello world";
  len = strlen(msg);
  fd = socket(AF_NETLINK, SOCK_DGRAM, 22);
  if (!fd) {
    printf("socket() error.\n");
    return -1;
  }
  memset(&saddr, 0, sizeof(saddr));
  saddr.nl_family = AF_NETLINK;
  saddr.nl_pid = getpid();
  memset(&daddr, 0, sizeof(daddr));
  daddr.nl_family = AF_NETLINK;
  daddr.nl_pid = 0;
  res = bind(fd, (const struct sockaddr *)&saddr, sizeof(saddr));
  if (res == -1) {
    printf("bind() error.\n");
    goto error;
  }
  nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(len + 1));
  if (!nlh) {
    printf("malloc() error.\n");
    goto error;
  }
  nlh->nlmsg_type = 0x10;
  nlh->nlmsg_flags = NLM_F_REQUEST;
  nlh->nlmsg_seq = 0;
  nlh->nlmsg_pid = getpid();
  strcpy(NLMSG_DATA(nlh), msg);
  nlh->nlmsg_len = NLMSG_LENGTH(len + 1);
  res = sendto(fd, nlh, NLMSG_SPACE(len + 1), 0, (struct sockaddr *)&daddr,
               sizeof(daddr));
  if (res == -1)
    printf("send() error.\n");
error:
  free(nlh);
  close(fd);
  return res;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xdfb(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%rdi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    $0x16,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1257 <main+0x77>
lea    0xdc6(%rip),%rdi
call   1080 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13cf <main+0x1ef>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <memset@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %rax,-0x38(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <memset@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x10,%edx
call   10f0 <bind@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    12d5 <main+0xf5>
lea    0xd52(%rip),%rdi
call   1080 <printf@plt>
jmp    13b8 <main+0x1d8>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   10c0 <NLMSG_SPACE@plt>
mov    %eax,%edi
call   10e0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1307 <main+0x127>
lea    0xd2f(%rip),%rdi
call   1080 <printf@plt>
jmp    13b8 <main+0x1d8>
mov    -0x28(%rbp),%rax
movl   $0x10,(%rax)
mov    0x2cd0(%rip),%rax
mov    (%rax),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x18(%rax)
mov    -0x28(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    $0x0,%al
call   1060 <getpid@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x28(%rbp),%rdi
call   10d0 <NLMSG_DATA@plt>
mov    %eax,%edi
mov    -0x50(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   1050 <NLMSG_LENGTH@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
call   10c0 <NLMSG_SPACE@plt>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    %eax,%edx
lea    -0x48(%rbp),%r8
xor    %ecx,%ecx
mov    $0x10,%r9d
call   10a0 <sendto@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    13b3 <main+0x1d3>
lea    0xc8f(%rip),%rdi
call   1080 <printf@plt>
jmp    13b8 <main+0x1d8>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
lea    0xe0d(%rip),%rdi
call   1080 <strlen@plt>
mov    %eax,%r15d
mov    0x2dd6(%rip),%rbp
mov    0x0(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    $0x16,%edx
call   1100 <socket@plt>
test   %eax,%eax
je     131e <main+0x13e>
mov    %eax,%r14d
lea    0x10(%rsp),%r12
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%rax
mov    %rax,0x18(%rsp)
xor    %eax,%eax
call   1070 <getpid@plt>
mov    %rax,0x10(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%rax
mov    %rax,0x8(%rsp)
movq   $0x0,(%rsp)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x10,%edx
call   10f0 <bind@plt>
mov    $0xffffffff,%r12d
cmp    $0xffffffff,%eax
je     1331 <main+0x151>
mov    %eax,%ebp
add    $0x1,%r15d
mov    %r15d,%edi
call   10c0 <NLMSG_SPACE@plt>
mov    %eax,%edi
call   10e0 <malloc@plt>
test   %rax,%rax
je     133a <main+0x15a>
mov    %rax,%rbx
movl   $0x10,(%rax)
mov    0x2d2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rbx)
movq   $0x0,0x10(%rbx)
xor    %eax,%eax
call   1070 <getpid@plt>
mov    %rax,0x8(%rbx)
mov    %rbx,%rdi
call   10d0 <NLMSG_DATA@plt>
lea    0xd1d(%rip),%rsi
mov    %eax,%edi
call   1040 <strcpy@plt>
mov    %r15d,%edi
call   1060 <NLMSG_LENGTH@plt>
mov    %eax,0x4(%rbx)
mov    %r15d,%edi
call   10c0 <NLMSG_SPACE@plt>
mov    %rsp,%r8
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
mov    $0x10,%r9d
call   10a0 <sendto@plt>
cmp    $0xffffffff,%eax
je     1348 <main+0x168>
mov    %eax,%ebp
jmp    1357 <main+0x177>
lea    0xce7(%rip),%rdi
call   1050 <puts@plt>
mov    $0xffffffff,%ebp
jmp    1367 <main+0x187>
lea    0xd02(%rip),%rdi
jmp    134f <main+0x16f>
lea    0xcdb(%rip),%rdi
xor    %ebx,%ebx
mov    %ebp,%r12d
jmp    134f <main+0x16f>
lea    0xcdd(%rip),%rdi
call   1050 <puts@plt>
mov    %r12d,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
call   10b0 <close@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32, i32, i8*, i64, i32 }
%struct.sockaddr_nl = type { i8*, i8* }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"hello world\00"
@AF_NETLINK = global i8* null
@SOCK_DGRAM = global i32 0
@.str.1 = [17 x i8] c"socket() error.\0A\00"
@.str.2 = [15 x i8] c"bind() error.\0A\00"
@.str.3 = [17 x i8] c"malloc() error.\0A\00"
@NLM_F_REQUEST = global i32 0
@.str.4 = [15 x i8] c"send() error.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.nlmsghdr*
%10 = alloca %struct.sockaddr_nl
%11 = alloca %struct.sockaddr_nl
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store %struct.nlmsghdr* null, %struct.nlmsghdr** %9
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %12
%13 = load i8*, i8** %12
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %8
%15 = load i8*, i8** @AF_NETLINK
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i8* %15, i32 %16, i32 22)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp ne i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %2
%21 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %87
22: ; preds = %2
%23 = call i32 @memset(%struct.sockaddr_nl* %10, i32 0, i32 16)
%24 = load i8*, i8** @AF_NETLINK
%25 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %10, i32 0, i32 1
store i8* %24, i8** %25
%26 = call i8* (...) @getpid()
%27 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %10, i32 0, i32 0
store i8* %26, i8** %27
%28 = call i32 @memset(%struct.sockaddr_nl* %11, i32 0, i32 16)
%29 = load i8*, i8** @AF_NETLINK
%30 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %11, i32 0, i32 1
store i8* %29, i8** %30
%31 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %11, i32 0, i32 0
store i8* null, i8** %31
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_nl* %10 to %struct.sockaddr*
%34 = call i32 @bind(i32 %32, %struct.sockaddr* %33, i32 16)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %22
%38 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %81
39: ; preds = %22
%40 = load i32, i32* %8
%41 = add nsw i32 %40, 1
%42 = call i32 @NLMSG_SPACE(i32 %41)
%43 = call i64 @malloc(i32 %42)
%44 = inttoptr i64 %43 to %struct.nlmsghdr*
store %struct.nlmsghdr* %44, %struct.nlmsghdr** %9
%45 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%46 = icmp ne %struct.nlmsghdr* %45, null
br i1 %46, label %49, label %47
47: ; preds = %39
%48 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %81
49: ; preds = %39
%50 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%51 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %50, i32 0, i32 0
store i32 16, i32* %51
%52 = load i32, i32* @NLM_F_REQUEST
%53 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%54 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %53, i32 0, i32 4
store i32 %52, i32* %54
%55 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%56 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %55, i32 0, i32 3
store i64 0, i64* %56
%57 = call i8* (...) @getpid()
%58 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%59 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %58, i32 0, i32 2
store i8* %57, i8** %59
%60 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%61 = call i32 @NLMSG_DATA(%struct.nlmsghdr* %60)
%62 = load i8*, i8** %12
%63 = call i32 @strcpy(i32 %61, i8* %62)
%64 = load i32, i32* %8
%65 = add nsw i32 %64, 1
%66 = call i32 @NLMSG_LENGTH(i32 %65)
%67 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%68 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %67, i32 0, i32 1
store i32 %66, i32* %68
%69 = load i32, i32* %6
%70 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%71 = load i32, i32* %8
%72 = add nsw i32 %71, 1
%73 = call i32 @NLMSG_SPACE(i32 %72)
%74 = bitcast %struct.sockaddr_nl* %11 to %struct.sockaddr*
%75 = call i32 @sendto(i32 %69, %struct.nlmsghdr* %70, i32 %73, i32 0, %struct.sockaddr* %74, i32 16)
store i32 %75, i32* %7
%76 = load i32, i32* %7
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %80
78: ; preds = %49
%79 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %80
80: ; preds = %78, %49
br label %81
81: ; preds = %80, %47, %37
%82 = load %struct.nlmsghdr*, %struct.nlmsghdr** %9
%83 = call i32 @free(%struct.nlmsghdr* %82)
%84 = load i32, i32* %6
%85 = call i32 @close(i32 %84)
%86 = load i32, i32* %7
store i32 %86, i32* %3
br label %87
87: ; preds = %81, %20
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr_nl = type { i8*, i8* }
%struct.nlmsghdr = type { i32, i32, i8*, i64, i32 }
%struct.sockaddr = type { i32 }
@.str = [12 x i8] c"hello world\00"
@AF_NETLINK = global i8* null
@SOCK_DGRAM = global i32 0
@NLM_F_REQUEST = global i32 0
@str = [16 x i8] c"socket() error.\00"
@str.5 = [16 x i8] c"malloc() error.\00"
@str.6 = [14 x i8] c"send() error.\00"
@str.7 = [14 x i8] c"bind() error.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_nl
%4 = alloca %struct.sockaddr_nl
%5 = bitcast %struct.sockaddr_nl* %3 to i8*
%6 = bitcast %struct.sockaddr_nl* %4 to i8*
%7 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** @AF_NETLINK
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i8* %8, i32 %9, i32 22)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %58
14: ; preds = %2
%15 = call i32 @memset(%struct.sockaddr_nl* %3, i32 0, i32 16)
%16 = load i8*, i8** @AF_NETLINK
%17 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 1
store i8* %16, i8** %17
%18 = call i8* (...) @getpid()
%19 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %3, i64 0, i32 0
store i8* %18, i8** %19
%20 = call i32 @memset(%struct.sockaddr_nl* %4, i32 0, i32 16)
%21 = load i8*, i8** @AF_NETLINK
%22 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i64 0, i32 1
store i8* %21, i8** %22
%23 = getelementptr inbounds %struct.sockaddr_nl, %struct.sockaddr_nl* %4, i64 0, i32 0
store i8* null, i8** %23
%24 = bitcast %struct.sockaddr_nl* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %10, %struct.sockaddr* %24, i32 16)
%26 = icmp eq i32 %25, -1
br i1 %26, label %48, label %27
27: ; preds = %14
%28 = add nsw i32 %7, 1
%29 = call i32 @NLMSG_SPACE(i32 %28)
%30 = call i64 @malloc(i32 %29)
%31 = inttoptr i64 %30 to %struct.nlmsghdr*
%32 = icmp eq i64 %30, 0
br i1 %32, label %48, label %33
33: ; preds = %27
%34 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i64 0, i32 0
store i32 16, i32* %34
%35 = load i32, i32* @NLM_F_REQUEST
%36 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i64 0, i32 4
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i64 0, i32 3
store i64 0, i64* %37
%38 = call i8* (...) @getpid()
%39 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i64 0, i32 2
store i8* %38, i8** %39
%40 = call i32 @NLMSG_DATA(%struct.nlmsghdr* %31)
%41 = call i32 @strcpy(i32 %40, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%42 = call i32 @NLMSG_LENGTH(i32 %28)
%43 = getelementptr inbounds %struct.nlmsghdr, %struct.nlmsghdr* %31, i64 0, i32 1
store i32 %42, i32* %43
%44 = call i32 @NLMSG_SPACE(i32 %28)
%45 = bitcast %struct.sockaddr_nl* %4 to %struct.sockaddr*
%46 = call i32 @sendto(i32 %10, %struct.nlmsghdr* %31, i32 %44, i32 0, %struct.sockaddr* %45, i32 16)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %53
48: ; preds = %33, %27, %14
%49 = phi i8* [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0), %14 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0), %27 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0), %33 ]
%50 = phi i32 [ -1, %14 ], [ %25, %27 ], [ -1, %33 ]
%51 = phi %struct.nlmsghdr* [ null, %14 ], [ null, %27 ], [ %31, %33 ]
%52 = call i32 @puts(i8* dereferenceable(1) %49)
br label %53
53: ; preds = %48, %33
%54 = phi i32 [ %46, %33 ], [ %50, %48 ]
%55 = phi %struct.nlmsghdr* [ %31, %33 ], [ %51, %48 ]
%56 = call i32 @free(%struct.nlmsghdr* %55)
%57 = call i32 @close(i32 %10)
br label %58
58: ; preds = %53, %12
%59 = phi i32 [ %54, %53 ], [ -1, %12 ]
ret i32 %59
}