#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 *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t strlen(char const *);
int marker_string_buffer_set(const char *str, char *buffer,
                             size_t buffer_size) {
  size_t str_len = strlen(str);
  if (str_len >= buffer_size) {
    memset(buffer, 0, buffer_size);
    memcpy(buffer, str, buffer_size);
    buffer[buffer_size - 1] = '\0';
    return 1;
  }
  memcpy(buffer, str, str_len + 1);
  return 0;
}


x86 assembly (clang, -O0):
<marker_string_buffer_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     119b <marker_string_buffer_set+0x6b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1040 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x1,-0x4(%rbp)
jmp    11b7 <marker_string_buffer_set+0x87>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
add    $0x1,%rdx
call   1050 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<marker_string_buffer_set>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
cmp    %rbx,%rax
jae    115e <marker_string_buffer_set+0x2e>
add    $0x1,%rax
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <memcpy@plt>
xor    %eax,%eax
jmp    1184 <marker_string_buffer_set+0x54>
mov    %r14,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
movb   $0x0,-0x1(%rbx,%r14,1)
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
define i32 @marker_string_buffer_set(i8* %0, i8* %1, i64 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = icmp ult i64 %4, %2
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = tail call i32 @memset(i8* %1, i32 0, i64 %2)
%8 = tail call i32 @memcpy(i8* %1, i8* %0, i64 %2)
%9 = add i64 %2, -1
%10 = getelementptr inbounds i8, i8* %1, i64 %9
store i8 0, i8* %10
br label %14
11: ; preds = %3
%12 = add nuw i64 %4, 1
%13 = tail call i32 @memcpy(i8* %1, i8* %0, i64 %12)
br label %14
14: ; preds = %11, %6
%15 = phi i32 [ 1, %6 ], [ 0, %11 ]
ret i32 %15
}