#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 rtattr {
  int dummy;
};
struct nlmsghdr {
  int dummy;
};
struct ifinfomsg {
  int ifi_change;
  int /*<<< orphan*/ ifi_family;
};
typedef int /*<<< orphan*/ pi;
typedef int /*<<< orphan*/ ns;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ IFLA_IFNAME;
int /*<<< orphan*/ IFLA_NET_NS_FD;
int /*<<< orphan*/ VETH_INFO_PEER;
struct rtattr *_rtattr_begin(struct nlmsghdr *, size_t, int /*<<< orphan*/,
                             struct ifinfomsg *, int);
int /*<<< orphan*/ memset(struct ifinfomsg *, int /*<<< orphan*/, int);
int /*<<< orphan*/ rtattr_end(struct nlmsghdr *, struct rtattr *);
scalar_t__ rtattr_pack(struct nlmsghdr *, size_t, int /*<<< orphan*/, ...);
int strlen(char const *);
int veth_pack_peerb(struct nlmsghdr *nh, size_t req_sz, const char *peer,
                    int ns) {
  struct ifinfomsg pi;
  struct rtattr *peer_attr;
  memset(&pi, 0, sizeof(pi));
  pi.ifi_family = AF_UNSPEC;
  pi.ifi_change = 0xFFFFFFFF;
  peer_attr = _rtattr_begin(nh, req_sz, VETH_INFO_PEER, &pi, sizeof(pi));
  if (!peer_attr)
    return -1;
  if (rtattr_pack(nh, req_sz, IFLA_IFNAME, peer, strlen(peer)))
    return -1;
  if (rtattr_pack(nh, req_sz, IFLA_NET_NS_FD, &ns, sizeof(ns)))
    return -1;
  rtattr_end(nh, peer_attr);
  return 0;
}


x86 assembly (clang, -O0):
<veth_pack_peerb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
movl   $0xffffffff,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e4f(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x8,%r8d
call   1070 <_rtattr_begin@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c5 <veth_pack_peerb+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <veth_pack_peerb+0x11d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    %eax,%r8d
mov    $0x0,%al
call   1060 <rtattr_pack@plt>
cmp    $0x0,%rax
je     1221 <veth_pack_peerb+0xd1>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <veth_pack_peerb+0x11d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
mov    $0x0,%al
call   1060 <rtattr_pack@plt>
cmp    $0x0,%rax
je     1259 <veth_pack_peerb+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <veth_pack_peerb+0x11d>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1030 <rtattr_end@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<veth_pack_peerb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %rdi,%r14
mov    %ecx,0xc(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
movl   $0xffffffff,0x10(%rsp)
ff
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    %r13,%rsi
mov    %rbx,%rcx
mov    $0x8,%r8d
call   1070 <_rtattr_begin@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     121e <veth_pack_peerb+0xce>
mov    %rax,%r12
mov    0x2e12(%rip),%rax
mov    (%rax),%ebx
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %ebx,%edx
mov    %rbp,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1060 <rtattr_pack@plt>
test   %rax,%rax
jne    121e <veth_pack_peerb+0xce>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    $0x4,%r8d
mov    %r14,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1060 <rtattr_pack@plt>
test   %rax,%rax
jne    121e <veth_pack_peerb+0xce>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <rtattr_end@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.nlmsghdr = type { i32 }
%struct.ifinfomsg = type { i32, i32 }
%struct.rtattr = type { i32 }
@AF_UNSPEC = global i32 0
@VETH_INFO_PEER = global i32 0
@IFLA_IFNAME = global i32 0
@IFLA_NET_NS_FD = global i32 0
define i32 @veth_pack_peerb(%struct.nlmsghdr* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.nlmsghdr*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.ifinfomsg
%11 = alloca %struct.rtattr*
store %struct.nlmsghdr* %0, %struct.nlmsghdr** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%12 = call i32 @memset(%struct.ifinfomsg* %10, i32 0, i32 8)
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.ifinfomsg, %struct.ifinfomsg* %10, i32 0, i32 1
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.ifinfomsg, %struct.ifinfomsg* %10, i32 0, i32 0
store i32 -1, i32* %15
%16 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%17 = load i64, i64* %7
%18 = load i32, i32* @VETH_INFO_PEER
%19 = call %struct.rtattr* @_rtattr_begin(%struct.nlmsghdr* %16, i64 %17, i32 %18, %struct.ifinfomsg* %10, i32 8)
store %struct.rtattr* %19, %struct.rtattr** %11
%20 = load %struct.rtattr*, %struct.rtattr** %11
%21 = icmp ne %struct.rtattr* %20, null
br i1 %21, label %23, label %22
22: ; preds = %4
store i32 -1, i32* %5
br label %44
23: ; preds = %4
%24 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%25 = load i64, i64* %7
%26 = load i32, i32* @IFLA_IFNAME
%27 = load i8*, i8** %8
%28 = load i8*, i8** %8
%29 = call i32 @strlen(i8* %28)
%30 = call i64 (%struct.nlmsghdr*, i64, i32, ...) @rtattr_pack(%struct.nlmsghdr* %24, i64 %25, i32 %26, i8* %27, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %23
store i32 -1, i32* %5
br label %44
33: ; preds = %23
%34 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%35 = load i64, i64* %7
%36 = load i32, i32* @IFLA_NET_NS_FD
%37 = call i64 (%struct.nlmsghdr*, i64, i32, ...) @rtattr_pack(%struct.nlmsghdr* %34, i64 %35, i32 %36, i32* %9, i64 4)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
store i32 -1, i32* %5
br label %44
40: ; preds = %33
%41 = load %struct.nlmsghdr*, %struct.nlmsghdr** %6
%42 = load %struct.rtattr*, %struct.rtattr** %11
%43 = call i32 @rtattr_end(%struct.nlmsghdr* %41, %struct.rtattr* %42)
store i32 0, i32* %5
br label %44
44: ; preds = %40, %39, %32, %22
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
%struct.nlmsghdr = type { i32 }
%struct.ifinfomsg = type { i32, i32 }
%struct.rtattr = type { i32 }
@AF_UNSPEC = global i32 0
@VETH_INFO_PEER = global i32 0
@IFLA_IFNAME = global i32 0
@IFLA_NET_NS_FD = global i32 0
define i32 @veth_pack_peerb(%struct.nlmsghdr* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca %struct.ifinfomsg
store i32 %3, i32* %5
%7 = bitcast %struct.ifinfomsg* %6 to i8*
%8 = call i32 @memset(%struct.ifinfomsg* %6, i32 0, i32 8)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.ifinfomsg, %struct.ifinfomsg* %6, i64 0, i32 1
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.ifinfomsg, %struct.ifinfomsg* %6, i64 0, i32 0
store i32 -1, i32* %11
%12 = load i32, i32* @VETH_INFO_PEER
%13 = call %struct.rtattr* @_rtattr_begin(%struct.nlmsghdr* %0, i64 %1, i32 %12, %struct.ifinfomsg* %6, i32 8)
%14 = icmp eq %struct.rtattr* %13, null
br i1 %14, label %26, label %15
15: ; preds = %4
%16 = load i32, i32* @IFLA_IFNAME
%17 = call i32 @strlen(i8* %2)
%18 = call i64 (%struct.nlmsghdr*, i64, i32, ...) @rtattr_pack(%struct.nlmsghdr* %0, i64 %1, i32 %16, i8* %2, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %15
%21 = load i32, i32* @IFLA_NET_NS_FD
%22 = call i64 (%struct.nlmsghdr*, i64, i32, ...) @rtattr_pack(%struct.nlmsghdr* %0, i64 %1, i32 %21, i32* %5, i64 4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @rtattr_end(%struct.nlmsghdr* %0, %struct.rtattr* %13)
br label %26
26: ; preds = %20, %15, %4, %24
%27 = phi i32 [ 0, %24 ], [ -1, %4 ], [ -1, %15 ], [ -1, %20 ]
ret i32 %27
}