#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 cec_msg {
  int len;
  int /*<<< orphan*/ *msg;
};
typedef int /*<<< orphan*/ __u8;

/* Variables and functions */
int /*<<< orphan*/ CEC_MSG_SET_OSD_STRING;
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, char const *, unsigned int);
unsigned int strlen(char const *);
void cec_msg_set_osd_string(struct cec_msg *msg, __u8 disp_ctl,
                            const char *osd) {
  unsigned int len = strlen(osd);
  if (len > 13) {
    len = 13;
  }
  msg->len = 3 + len;
  msg->msg[1] = CEC_MSG_SET_OSD_STRING;
  msg->msg[2] = disp_ctl;
  memcpy(msg->msg + 3, osd, len);
}


x86 assembly (clang, -O0):
<cec_msg_set_osd_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xd,-0x1c(%rbp)
jbe    1150 <cec_msg_set_osd_string+0x30>
movl   $0xd,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
add    $0x3,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    0x2e85(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
add    $0xc,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cec_msg_set_osd_string>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1030 <strlen@plt>
cmp    $0xd,%eax
mov    $0xd,%edx
cmovb  %eax,%edx
lea    0x3(%rdx),%eax
mov    %eax,0x0(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    0x8(%rbp),%rdi
mov    %eax,0x4(%rdi)
mov    %r14d,0x8(%rdi)
add    $0xc,%rdi
mov    %rbx,%rsi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <memcpy@plt>

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

LLVM IR (-O2):
%struct.cec_msg = type { i32, i32* }
@CEC_MSG_SET_OSD_STRING = global i32 0
define void @cec_msg_set_osd_string(%struct.cec_msg* nocapture %0, i32 %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %2)
%5 = icmp ult i32 %4, 13
%6 = select i1 %5, i32 %4, i32 13
%7 = add nuw nsw i32 %6, 3
%8 = getelementptr inbounds %struct.cec_msg, %struct.cec_msg* %0, i64 0, i32 0
store i32 %7, i32* %8
%9 = load i32, i32* @CEC_MSG_SET_OSD_STRING
%10 = getelementptr inbounds %struct.cec_msg, %struct.cec_msg* %0, i64 0, i32 1
%11 = load i32*, i32** %10
%12 = getelementptr inbounds i32, i32* %11, i64 1
store i32 %9, i32* %12
%13 = getelementptr inbounds i32, i32* %11, i64 2
store i32 %1, i32* %13
%14 = getelementptr inbounds i32, i32* %11, i64 3
%15 = tail call i32 @memcpy(i32* %14, i8* %2, i32 %6)
ret void
}