#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 */
typedef int /*<<< orphan*/ uint8_t;
struct ip6_opt {
  int ip6o_len;
  int /*<<< orphan*/ ip6o_type;
};

/* Variables and functions */
int /*<<< orphan*/ IP6OPT_PAD1;
int /*<<< orphan*/ IP6OPT_PADN;
int /*<<< orphan*/ memset(struct ip6_opt *, char, int);
void add_padding(uint8_t *extbuf, int offset, int npad) {
  if (npad == 1)
    extbuf[offset] = IP6OPT_PAD1;
  else if (npad > 0) {
    struct ip6_opt *pad_opt = (struct ip6_opt *)(extbuf + offset);
    pad_opt->ip6o_type = IP6OPT_PADN;
    pad_opt->ip6o_len = npad - sizeof(struct ip6_opt);
    memset(pad_opt + 1, '\0', pad_opt->ip6o_len);
  }
}


x86 assembly (clang, -O0):
<add_padding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    1145 <add_padding+0x35>
mov    0x2eb5(%rip),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
jmp    119c <add_padding+0x8c>
cmpl   $0x0,-0x10(%rbp)
jle    1197 <add_padding+0x87>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
movslq -0x10(%rbp),%rax
sub    $0x8,%rax
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
add    $0x8,%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <memset@plt>
jmp    119c <add_padding+0x8c>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_padding>:
cmp    $0x1,%edx
jne    1125 <add_padding+0x15>
mov    0x2ecc(%rip),%rax
mov    (%rax),%eax
movslq %esi,%rcx
mov    %eax,(%rdi,%rcx,4)
ret
jl     114c <add_padding+0x3c>
movslq %esi,%rax
mov    0x2ea7(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x4(%rdi,%rax,4)
add    $0xfffffff8,%edx
mov    %edx,(%rdi,%rax,4)
lea    (%rdi,%rax,4),%rdi
add    $0x8,%rdi
xor    %esi,%esi
jmp    1030 <memset@plt>
ret

LLVM IR (-O0):
%struct.ip6_opt = type { i32, i32 }
@IP6OPT_PAD1 = global i32 0
@IP6OPT_PADN = global i32 0
define void @add_padding(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.ip6_opt*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = load i32, i32* @IP6OPT_PAD1
%12 = load i32*, i32** %4
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %12, i64 %14
store i32 %11, i32* %15
br label %41
16: ; preds = %3
%17 = load i32, i32* %6
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %40
19: ; preds = %16
%20 = load i32*, i32** %4
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
%24 = bitcast i32* %23 to %struct.ip6_opt*
store %struct.ip6_opt* %24, %struct.ip6_opt** %7
%25 = load i32, i32* @IP6OPT_PADN
%26 = load %struct.ip6_opt*, %struct.ip6_opt** %7
%27 = getelementptr inbounds %struct.ip6_opt, %struct.ip6_opt* %26, i32 0, i32 1
store i32 %25, i32* %27
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = sub i64 %29, 8
%31 = trunc i64 %30 to i32
%32 = load %struct.ip6_opt*, %struct.ip6_opt** %7
%33 = getelementptr inbounds %struct.ip6_opt, %struct.ip6_opt* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load %struct.ip6_opt*, %struct.ip6_opt** %7
%35 = getelementptr inbounds %struct.ip6_opt, %struct.ip6_opt* %34, i64 1
%36 = load %struct.ip6_opt*, %struct.ip6_opt** %7
%37 = getelementptr inbounds %struct.ip6_opt, %struct.ip6_opt* %36, i32 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @memset(%struct.ip6_opt* %35, i8 signext 0, i32 %38)
br label %40
40: ; preds = %19, %16
br label %41
41: ; preds = %40, %10
ret void
}

LLVM IR (-O2):
%struct.ip6_opt = type { i32, i32 }
@IP6OPT_PAD1 = global i32 0
@IP6OPT_PADN = global i32 0
define void @add_padding(i32* %0, i32 %1, i32 %2) {
%4 = icmp eq i32 %2, 1
br i1 %4, label %5, label %9
5: ; preds = %3
%6 = load i32, i32* @IP6OPT_PAD1
%7 = sext i32 %1 to i64
%8 = getelementptr inbounds i32, i32* %0, i64 %7
store i32 %6, i32* %8
br label %20
9: ; preds = %3
%10 = icmp sgt i32 %2, 0
br i1 %10, label %11, label %20
11: ; preds = %9
%12 = sext i32 %1 to i64
%13 = getelementptr inbounds i32, i32* %0, i64 %12
%14 = load i32, i32* @IP6OPT_PADN
%15 = getelementptr inbounds i32, i32* %13, i64 1
store i32 %14, i32* %15
%16 = add nsw i32 %2, -8
store i32 %16, i32* %13
%17 = getelementptr inbounds i32, i32* %13, i64 2
%18 = bitcast i32* %17 to %struct.ip6_opt*
%19 = tail call i32 @memset(%struct.ip6_opt* %18, i8 signext 0, i32 %16)
br label %20
20: ; preds = %9, %11, %5
ret void
}