#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 dstring {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ DSAppendChar(struct dstring *, char);
void DSAppendStringN(struct dstring *pds, char *str, int len) {
  while (*str) {
    if (len-- <= 0)
      break;
    DSAppendChar(pds, *str);
    ++str;
  }
}


x86 assembly (clang, -O0):
<DSAppendStringN>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     116a <DSAppendStringN+0x5a>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jg     1149 <DSAppendStringN+0x39>
jmp    116a <DSAppendStringN+0x5a>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
call   1030 <DSAppendChar@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1123 <DSAppendStringN+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<DSAppendStringN>:
push   %r15
push   %r14
push   %rbx
mov    (%rsi),%al
test   %al,%al
je     1153 <DSAppendStringN+0x43>
mov    %edx,%r15d
test   %edx,%edx
jle    1153 <DSAppendStringN+0x43>
mov    %rsi,%rbx
mov    %rdi,%r14
add    $0x1,%rbx
nopl   0x0(%rax)
movsbl %al,%esi
mov    %r14,%rdi
call   1030 <DSAppendChar@plt>
movzbl (%rbx),%eax
test   %al,%al
je     1153 <DSAppendStringN+0x43>
lea    -0x1(%r15),%ecx
add    $0x1,%rbx
cmp    $0x1,%r15d
mov    %ecx,%r15d
ja     1130 <DSAppendStringN+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dstring = type { i32 }
define void @DSAppendStringN(%struct.dstring* %0, i8* %1, i32 %2) {
%4 = alloca %struct.dstring*
%5 = alloca i8*
%6 = alloca i32
store %struct.dstring* %0, %struct.dstring** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %7
7: ; preds = %16, %3
%8 = load i8*, i8** %5
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %7
%12 = load i32, i32* %6
%13 = add nsw i32 %12, -1
store i32 %13, i32* %6
%14 = icmp sle i32 %12, 0
br i1 %14, label %15, label %16
15: ; preds = %11
br label %23
16: ; preds = %11
%17 = load %struct.dstring*, %struct.dstring** %4
%18 = load i8*, i8** %5
%19 = load i8, i8* %18
%20 = call i32 @DSAppendChar(%struct.dstring* %17, i8 signext %19)
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %5
br label %7
23: ; preds = %15, %7
ret void
}

LLVM IR (-O2):
%struct.dstring = type { i32 }
define void @DSAppendStringN(%struct.dstring* %0, i8* nocapture %1, i32 %2) {
%4 = load i8, i8* %1
%5 = icmp eq i8 %4, 0
%6 = icmp slt i32 %2, 1
%7 = or i1 %5, %6
br i1 %7, label %19, label %8
8: ; preds = %3, %8
%9 = phi i8 [ %15, %8 ], [ %4, %3 ]
%10 = phi i32 [ %12, %8 ], [ %2, %3 ]
%11 = phi i8* [ %14, %8 ], [ %1, %3 ]
%12 = add nsw i32 %10, -1
%13 = tail call i32 @DSAppendChar(%struct.dstring* %0, i8 signext %9)
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
%17 = icmp ult i32 %10, 2
%18 = select i1 %16, i1 true, i1 %17
br i1 %18, label %19, label %8
19: ; preds = %8, %3
ret void
}