#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 */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
void print_non_null_terminated_str(const char *s, int len) {
  for (int i = 0; *s && i < len; i++)
    printf("%c", *s++);
  puts("");
}


x86 assembly (clang, -O0):
<print_non_null_terminated_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x11(%rbp)
je     1157 <print_non_null_terminated_str+0x37>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
setl   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1167 <print_non_null_terminated_str+0x47>
jmp    1193 <print_non_null_terminated_str+0x73>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%esi
lea    0xe80(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1136 <print_non_null_terminated_str+0x16>
lea    0xe68(%rip),%rdi
call   1030 <puts@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_non_null_terminated_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    (%rdi),%al
test   %al,%al
je     116f <print_non_null_terminated_str+0x4f>
mov    %esi,%r15d
test   %esi,%esi
jle    116f <print_non_null_terminated_str+0x4f>
mov    %rdi,%rbx
mov    $0x1,%ebp
lea    0xebd(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl %al,%esi
mov    %r14,%rdi
call   1040 <printf@plt>
movzbl (%rbx,%rbp,1),%eax
test   %al,%al
je     116f <print_non_null_terminated_str+0x4f>
lea    0x1(%rbp),%rcx
cmp    %r15d,%ebp
mov    %rcx,%rbp
jl     1150 <print_non_null_terminated_str+0x30>
mov    $0xa,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"%c\00"
@.str.1 = [1 x i8] zeroinitializer
define void @print_non_null_terminated_str(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
br label %6
6: ; preds = %23, %2
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %6
%12 = load i32, i32* %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br label %15
15: ; preds = %11, %6
%16 = phi i1 [ false, %6 ], [ %14, %11 ]
br i1 %16, label %17, label %26
17: ; preds = %15
%18 = load i8*, i8** %3
%19 = getelementptr inbounds i8, i8* %18, i32 1
store i8* %19, i8** %3
%20 = load i8, i8* %18
%21 = sext i8 %20 to i32
%22 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %17
%24 = load i32, i32* %5
%25 = add nsw i32 %24, 1
store i32 %25, i32* %5
br label %6
26: ; preds = %15
%27 = call i32 @puts(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%c\00"
define void @print_non_null_terminated_str(i8* nocapture %0, i32 %1) {
%3 = load i8, i8* %0
%4 = icmp ne i8 %3, 0
%5 = icmp sgt i32 %1, 0
%6 = and i1 %4, %5
br i1 %6, label %9, label %7
7: ; preds = %9, %2
%8 = tail call i32 @putchar(i32 10)
ret void
9: ; preds = %2, %9
%10 = phi i8 [ %17, %9 ], [ %3, %2 ]
%11 = phi i32 [ %16, %9 ], [ 0, %2 ]
%12 = phi i8* [ %14, %9 ], [ %0, %2 ]
%13 = sext i8 %10 to i32
%14 = getelementptr inbounds i8, i8* %12, i64 1
%15 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %13)
%16 = add nuw nsw i32 %11, 1
%17 = load i8, i8* %14
%18 = icmp ne i8 %17, 0
%19 = icmp slt i32 %16, %1
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %9, label %7
}