#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*/ input3;
typedef int /*<<< orphan*/ input2;
typedef int /*<<< orphan*/ input;

/* Variables and functions */
int /*<<< orphan*/ check_unsigned_int(unsigned int, unsigned int);
int /*<<< orphan*/ check_unsigned_ints(unsigned int, int, char *);
unsigned int str_nlen(char const *, int);
unsigned int strlen(char const *);
void test_str_nlen() {
  const char *input = "foo";
  const char *input2 = "fo\0bar";
  const char *input3 = "";
  unsigned int result, expected_result;
  result = str_nlen(input, sizeof(input));
  expected_result = strlen(input);
  check_unsigned_ints(result, expected_result, "compare with libc");
  check_unsigned_int(result, 3);
  result = str_nlen(input2, sizeof(input2));
  expected_result = strlen(input2);
  check_unsigned_int(result, expected_result);
  result = str_nlen(input3, sizeof(input3));
  expected_result = strlen(input3);
  check_unsigned_int(result, expected_result);
  result = str_nlen(input, 2);
  check_unsigned_ints(result, 2, "limit to 2 not 3");
}


x86 assembly (clang, -O0):
<test_str_nlen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xeb1(%rip),%rax
mov    %rax,-0x8(%rbp)
lea    0xecd(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    0xec1(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x8,%esi
call   1060 <str_nlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
lea    0xe71(%rip),%rdx
call   1040 <check_unsigned_ints@plt>
mov    -0x1c(%rbp),%edi
mov    $0x3,%esi
call   1050 <check_unsigned_int@plt>
mov    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1060 <str_nlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1050 <check_unsigned_int@plt>
mov    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1060 <str_nlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%esi
call   1050 <check_unsigned_int@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2,%esi
call   1060 <str_nlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
lea    0xe01(%rip),%rdx
call   1040 <check_unsigned_ints@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_str_nlen>:
push   %rbp
push   %r14
push   %rbx
lea    0xeb5(%rip),%r14
mov    %r14,%rdi
mov    $0x8,%esi
call   1060 <str_nlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    0xe9b(%rip),%rdx
mov    %ebp,%edi
mov    %eax,%esi
call   1040 <check_unsigned_ints@plt>
mov    %ebp,%edi
mov    $0x3,%esi
call   1050 <check_unsigned_int@plt>
lea    0xea2(%rip),%rbp
mov    %rbp,%rdi
mov    $0x8,%esi
call   1060 <str_nlen@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1050 <check_unsigned_int@plt>
lea    0xe7a(%rip),%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
call   1060 <str_nlen@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <check_unsigned_int@plt>
mov    %r14,%rdi
mov    $0x2,%esi
call   1060 <str_nlen@plt>
lea    0xe36(%rip),%rdx
mov    %eax,%edi
mov    $0x2,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <check_unsigned_ints@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"foo\00"
@.str.1 = [7 x i8] c"fo\00bar\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [18 x i8] c"compare with libc\00"
@.str.4 = [17 x i8] c"limit to 2 not 3\00"
define void @test_str_nlen() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %1
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %2
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i8** %3
%6 = load i8*, i8** %1
%7 = call i32 @str_nlen(i8* %6, i32 8)
store i32 %7, i32* %4
%8 = load i8*, i8** %1
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %5
%10 = load i32, i32* %4
%11 = load i32, i32* %5
%12 = call i32 @check_unsigned_ints(i32 %10, i32 %11, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%13 = load i32, i32* %4
%14 = call i32 @check_unsigned_int(i32 %13, i32 3)
%15 = load i8*, i8** %2
%16 = call i32 @str_nlen(i8* %15, i32 8)
store i32 %16, i32* %4
%17 = load i8*, i8** %2
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %5
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = call i32 @check_unsigned_int(i32 %19, i32 %20)
%22 = load i8*, i8** %3
%23 = call i32 @str_nlen(i8* %22, i32 8)
store i32 %23, i32* %4
%24 = load i8*, i8** %3
%25 = call i32 @strlen(i8* %24)
store i32 %25, i32* %5
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = call i32 @check_unsigned_int(i32 %26, i32 %27)
%29 = load i8*, i8** %1
%30 = call i32 @str_nlen(i8* %29, i32 2)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = call i32 @check_unsigned_ints(i32 %31, i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"foo\00"
@.str.1 = [7 x i8] c"fo\00bar\00"
@.str.2 = [1 x i8] zeroinitializer
@.str.3 = [18 x i8] c"compare with libc\00"
@.str.4 = [17 x i8] c"limit to 2 not 3\00"
define void @test_str_nlen() {
%1 = tail call i32 @str_nlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 8)
%2 = tail call i32 @strlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @check_unsigned_ints(i32 %1, i32 %2, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%4 = tail call i32 @check_unsigned_int(i32 %1, i32 3)
%5 = tail call i32 @str_nlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 8)
%6 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @check_unsigned_int(i32 %5, i32 %6)
%8 = tail call i32 @str_nlen(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), i32 8)
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @check_unsigned_int(i32 %8, i32 %9)
%11 = tail call i32 @str_nlen(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 2)
%12 = tail call i32 @check_unsigned_ints(i32 %11, i32 2, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
ret void
}