#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_in6 {
  int /*<<< orphan*/ sin6_addr;
};
struct ip6_rthdr0 {
  int ip6r0_segleft;
};
struct addrinfo {
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_family;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET6;
int /*<<< orphan*/ AI_NUMERICHOST;
int /*<<< orphan*/ IPV6_RTHDR_TYPE_0;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ checknum(int, int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo const *,
                       struct addrinfo **);
int inet6_rth_add(void *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *inet6_rth_init(char *, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ set_funcname(char *, int);
void test_rth_add() {
  int i, ret;
  char buf[10240];
  struct addrinfo *res;
  struct addrinfo hints;
  set_funcname("test_rth_add", sizeof("test_rth_add\0"));
  if (NULL == inet6_rth_init(buf, 10240, IPV6_RTHDR_TYPE_0, 127))
    abort();
  memset((void *)&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_INET6;
  hints.ai_flags = AI_NUMERICHOST;
  if (0 != getaddrinfo("::1", NULL, (const struct addrinfo *)&hints, &res))
    abort();
  for (i = 0; i < 127; i++)
    inet6_rth_add((void *)buf,
                  &((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr);
  checknum(127, ((struct ip6_rthdr0 *)buf)->ip6r0_segleft, 0,
           "add 127 segments\0");
  ret = inet6_rth_add((void *)buf,
                      &((struct sockaddr_in6 *)(res->ai_addr))->sin6_addr);
  checknum(-1, ret, 0, "add 128th segment to 127 segment header\0");
  freeaddrinfo(res);
}


x86 assembly (clang, -O0):
<test_rth_add>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2830,%rsp
lea    0xe6e(%rip),%rdi
mov    $0xe,%esi
call   1070 <set_funcname@plt>
lea    -0x2810(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%edx
mov    $0x2800,%esi
mov    $0x7f,%ecx
call   1060 <inet6_rth_init@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11d0 <test_rth_add+0x50>
mov    $0x0,%al
call   1030 <abort@plt>
lea    -0x2828(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x281c(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2820(%rbp)
lea    0xe05(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x2828(%rbp),%rdx
lea    -0x2818(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1234 <test_rth_add+0xb4>
mov    $0x0,%al
call   1030 <abort@plt>
movl   $0x0,-0x4(%rbp)
cmpl   $0x7f,-0x4(%rbp)
jge    1269 <test_rth_add+0xe9>
lea    -0x2810(%rbp),%rdi
mov    -0x2818(%rbp),%rax
mov    (%rax),%rsi
call   1080 <inet6_rth_add@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4(%rbp)
jmp    123b <test_rth_add+0xbb>
mov    -0x2810(%rbp),%esi
mov    $0x7f,%edi
xor    %edx,%edx
lea    0xd94(%rip),%rcx
call   1050 <checknum@plt>
lea    -0x2810(%rbp),%rdi
mov    -0x2818(%rbp),%rax
mov    (%rax),%rsi
call   1080 <inet6_rth_add@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    $0xffffffff,%edi
xor    %edx,%edx
lea    0xd77(%rip),%rcx
call   1050 <checknum@plt>
mov    -0x2818(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
add    $0x2830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_rth_add>:
push   %rbp
push   %rbx
sub    $0x2828,%rsp
lea    0xe70(%rip),%rdi
mov    $0xe,%esi
call   1070 <set_funcname@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    0x20(%rsp),%rdi
mov    $0x2800,%esi
mov    $0x7f,%ecx
call   1060 <inet6_rth_init@plt>
test   %rax,%rax
je     1287 <test_rth_add+0x107>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0xe18(%rip),%rdi
lea    0x8(%rsp),%rcx
xor    %esi,%esi
mov    %rbx,%rdx
call   1090 <getaddrinfo@plt>
test   %rax,%rax
jne    1287 <test_rth_add+0x107>
mov    $0x7f,%ebp
lea    0x20(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%rax
mov    (%rax),%rsi
mov    %rbx,%rdi
call   1080 <inet6_rth_add@plt>
add    $0xffffffff,%ebp
jne    1220 <test_rth_add+0xa0>
mov    0x20(%rsp),%esi
lea    0xdd1(%rip),%rcx
mov    $0x7f,%edi
xor    %edx,%edx
call   1050 <checknum@plt>
mov    0x8(%rsp),%rax
mov    (%rax),%rsi
lea    0x20(%rsp),%rdi
call   1080 <inet6_rth_add@plt>
lea    0xdbe(%rip),%rcx
mov    $0xffffffff,%edi
mov    %eax,%esi
xor    %edx,%edx
call   1050 <checknum@plt>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
add    $0x2828,%rsp
pop    %rbx
pop    %rbp
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32 }
%struct.sockaddr_in6 = type { i32 }
%struct.ip6_rthdr0 = type { i32 }
@.str = [13 x i8] c"test_rth_add\00"
@IPV6_RTHDR_TYPE_0 = global i32 0
@AF_INET6 = global i32 0
@AI_NUMERICHOST = global i32 0
@.str.1 = [4 x i8] c"::1\00"
@.str.2 = [18 x i8] c"add 127 segments\00\00"
@.str.3 = [41 x i8] c"add 128th segment to 127 segment header\00\00"
define void @test_rth_add() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [10240 x i8]
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo
%6 = call i32 @set_funcname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 14)
%7 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* @IPV6_RTHDR_TYPE_0
%9 = call i32* @inet6_rth_init(i8* %7, i32 10240, i32 %8, i32 127)
%10 = icmp eq i32* null, %9
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (...) @abort()
unreachable
13: ; preds = %0
%14 = bitcast %struct.addrinfo* %5 to i8*
%15 = call i32 @memset(i8* %14, i32 0, i32 16)
%16 = load i32, i32* @AF_INET6
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @AI_NUMERICHOST
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 1
store i32 %18, i32* %19
%20 = call i64 @getaddrinfo(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* null, %struct.addrinfo* %5, %struct.addrinfo** %4)
%21 = icmp ne i64 0, %20
br i1 %21, label %22, label %24
22: ; preds = %13
%23 = call i32 (...) @abort()
unreachable
24: ; preds = %13
store i32 0, i32* %1
br label %25
25: ; preds = %36, %24
%26 = load i32, i32* %1
%27 = icmp slt i32 %26, 127
br i1 %27, label %28, label %39
28: ; preds = %25
%29 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 0
%30 = load %struct.addrinfo*, %struct.addrinfo** %4
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 0
%32 = load i64, i64* %31
%33 = inttoptr i64 %32 to %struct.sockaddr_in6*
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %33, i32 0, i32 0
%35 = call i32 @inet6_rth_add(i8* %29, i32* %34)
br label %36
36: ; preds = %28
%37 = load i32, i32* %1
%38 = add nsw i32 %37, 1
store i32 %38, i32* %1
br label %25
39: ; preds = %25
%40 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 0
%41 = bitcast i8* %40 to %struct.ip6_rthdr0*
%42 = getelementptr inbounds %struct.ip6_rthdr0, %struct.ip6_rthdr0* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @checknum(i32 127, i32 %43, i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%45 = getelementptr inbounds [10240 x i8], [10240 x i8]* %3, i64 0, i64 0
%46 = load %struct.addrinfo*, %struct.addrinfo** %4
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to %struct.sockaddr_in6*
%50 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %49, i32 0, i32 0
%51 = call i32 @inet6_rth_add(i8* %45, i32* %50)
store i32 %51, i32* %2
%52 = load i32, i32* %2
%53 = call i32 @checknum(i32 -1, i32 %52, i32 0, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%54 = load %struct.addrinfo*, %struct.addrinfo** %4
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32 }
%struct.sockaddr_in6 = type { i32 }
@.str = [13 x i8] c"test_rth_add\00"
@IPV6_RTHDR_TYPE_0 = global i32 0
@AF_INET6 = global i32 0
@AI_NUMERICHOST = global i32 0
@.str.1 = [4 x i8] c"::1\00"
@.str.2 = [18 x i8] c"add 127 segments\00\00"
@.str.3 = [41 x i8] c"add 128th segment to 127 segment header\00\00"
define void @test_rth_add() {
%1 = alloca [10240 x i8]
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo
%4 = getelementptr inbounds [10240 x i8], [10240 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.addrinfo** %2 to i8*
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = tail call i32 @set_funcname(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 14)
%8 = load i32, i32* @IPV6_RTHDR_TYPE_0
%9 = call i32* @inet6_rth_init(i8* %4, i32 10240, i32 %8, i32 127)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (...) @abort()
unreachable
13: ; preds = %0
%14 = call i32 @memset(i8* %6, i32 0, i32 16)
%15 = load i32, i32* @AF_INET6
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @AI_NUMERICHOST
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = call i64 @getaddrinfo(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32* null, %struct.addrinfo* %3, %struct.addrinfo** %2)
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %13
%22 = call i32 (...) @abort()
unreachable
23: ; preds = %13, %23
%24 = phi i32 [ %31, %23 ], [ 0, %13 ]
%25 = load %struct.addrinfo*, %struct.addrinfo** %2
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to %struct.sockaddr_in6*
%29 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %28, i64 0, i32 0
%30 = call i32 @inet6_rth_add(i8* %4, i32* %29)
%31 = add nuw nsw i32 %24, 1
%32 = icmp eq i32 %31, 127
br i1 %32, label %33, label %23
33: ; preds = %23
%34 = bitcast [10240 x i8]* %1 to i32*
%35 = load i32, i32* %34
%36 = call i32 @checknum(i32 127, i32 %35, i32 0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%37 = load %struct.addrinfo*, %struct.addrinfo** %2
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i64 0, i32 0
%39 = load i64, i64* %38
%40 = inttoptr i64 %39 to %struct.sockaddr_in6*
%41 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %40, i64 0, i32 0
%42 = call i32 @inet6_rth_add(i8* %4, i32* %41)
%43 = call i32 @checknum(i32 -1, i32 %42, i32 0, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
%44 = load %struct.addrinfo*, %struct.addrinfo** %2
%45 = call i32 @freeaddrinfo(%struct.addrinfo* %44)
ret void
}