#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*/ memcpy(char *, char const *, int);
scalar_t__ strlen(char const *);
void iupStrCopyN(char *dst_str, int dst_max_size, const char *src_str) {
  if (src_str) {
    int size = (int)strlen(src_str) + 1;
    if (size > dst_max_size)
      size = dst_max_size;
    memcpy(dst_str, src_str, size - 1);
    dst_str[size - 1] = 0;
  }
}


x86 assembly (clang, -O0):
<iupStrCopyN>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1183 <iupStrCopyN+0x63>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    115f <iupStrCopyN+0x3f>
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iupStrCopyN>:
test   %rdx,%rdx
je     115d <iupStrCopyN+0x3d>
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    %rdx,%rdi
call   1030 <strlen@plt>
lea    0x1(%rax),%ecx
cmp    %ebp,%eax
cmovge %ebp,%ecx
movslq %ecx,%rbp
lea    -0x1(%rbp),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <memcpy@plt>
movb   $0x0,-0x1(%r14,%rbp,1)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @iupStrCopyN(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%8 = load i8*, i8** %6
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %31
10: ; preds = %3
%11 = load i8*, i8** %6
%12 = call i64 @strlen(i8* %11)
%13 = trunc i64 %12 to i32
%14 = add nsw i32 %13, 1
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = load i32, i32* %5
%17 = icmp sgt i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = load i32, i32* %5
store i32 %19, i32* %7
br label %20
20: ; preds = %18, %10
%21 = load i8*, i8** %4
%22 = load i8*, i8** %6
%23 = load i32, i32* %7
%24 = sub nsw i32 %23, 1
%25 = call i32 @memcpy(i8* %21, i8* %22, i32 %24)
%26 = load i8*, i8** %4
%27 = load i32, i32* %7
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %20, %3
ret void
}

LLVM IR (-O2):
define void @iupStrCopyN(i8* %0, i32 %1, i8* %2) {
%4 = icmp eq i8* %2, null
br i1 %4, label %15, label %5
5: ; preds = %3
%6 = tail call i64 @strlen(i8* %2)
%7 = trunc i64 %6 to i32
%8 = add nsw i32 %7, 1
%9 = icmp slt i32 %7, %1
%10 = select i1 %9, i32 %8, i32 %1
%11 = add nsw i32 %10, -1
%12 = tail call i32 @memcpy(i8* %0, i8* %2, i32 %11)
%13 = sext i32 %11 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %5, %3
ret void
}