#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*/ memmove(char *, char *, int);
int /*<<< orphan*/ startuperror(char *, char *, ...);
int strlen(char *);
char *copyvalue(char *dest, size_t maxlen, char *value, char *keyword) {
  if (strlen(value) > maxlen) {
    if (keyword)
      startuperror("Too long value for \"%s\" (%s)\n", keyword, value);
    else
      startuperror("Too long value (%s)\n", value);
    return NULL;
  }
  memmove(dest, value, strlen(value) + 1);
  return dest;
}


x86 assembly (clang, -O0):
<copyvalue>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
cltq
cmp    -0x18(%rbp),%rax
jbe    11a2 <copyvalue+0x72>
cmpq   $0x0,-0x28(%rbp)
je     1183 <copyvalue+0x53>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1030 <startuperror@plt>
jmp    1195 <copyvalue+0x65>
mov    -0x20(%rbp),%rsi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <startuperror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11d5 <copyvalue+0xa5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
add    $0x1,%edx
call   1050 <memmove@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<copyvalue>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdx,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rbx,%rax
jbe    1171 <copyvalue+0x41>
test   %r15,%r15
je     1189 <copyvalue+0x59>
lea    0xea1(%rip),%rdi
xor    %r14d,%r14d
mov    %r15,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1030 <startuperror@plt>
jmp    119d <copyvalue+0x6d>
mov    %r12,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edx
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <memmove@plt>
jmp    119d <copyvalue+0x6d>
lea    0xe8e(%rip),%rdi
xor    %r14d,%r14d
mov    %r12,%rsi
xor    %eax,%eax
call   1030 <startuperror@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Too long value for \22%s\22 (%s)\0A\00"
@.str.1 = [21 x i8] c"Too long value (%s)\0A\00"
define i8* @copyvalue(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%10 = load i8*, i8** %8
%11 = call i32 @strlen(i8* %10)
%12 = sext i32 %11 to i64
%13 = load i64, i64* %7
%14 = icmp ugt i64 %12, %13
br i1 %14, label %15, label %26
15: ; preds = %4
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i8*, i8** %9
%20 = load i8*, i8** %8
%21 = call i32 (i8*, i8*, ...) @startuperror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %19, i8* %20)
br label %25
22: ; preds = %15
%23 = load i8*, i8** %8
%24 = call i32 (i8*, i8*, ...) @startuperror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %18
store i8* null, i8** %5
br label %34
26: ; preds = %4
%27 = load i8*, i8** %6
%28 = load i8*, i8** %8
%29 = load i8*, i8** %8
%30 = call i32 @strlen(i8* %29)
%31 = add nsw i32 %30, 1
%32 = call i32 @memmove(i8* %27, i8* %28, i32 %31)
%33 = load i8*, i8** %6
store i8* %33, i8** %5
br label %34
34: ; preds = %26, %25
%35 = load i8*, i8** %5
ret i8* %35
}

LLVM IR (-O2):
@.str = [30 x i8] c"Too long value for \22%s\22 (%s)\0A\00"
@.str.1 = [21 x i8] c"Too long value (%s)\0A\00"
define i8* @copyvalue(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = tail call i32 @strlen(i8* %2)
%6 = sext i32 %5 to i64
%7 = icmp ugt i64 %6, %1
br i1 %7, label %8, label %14
8: ; preds = %4
%9 = icmp eq i8* %3, null
br i1 %9, label %12, label %10
10: ; preds = %8
%11 = tail call i32 (i8*, i8*, ...) @startuperror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %3, i8* %2)
br label %18
12: ; preds = %8
%13 = tail call i32 (i8*, i8*, ...) @startuperror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %18
14: ; preds = %4
%15 = tail call i32 @strlen(i8* %2)
%16 = add nsw i32 %15, 1
%17 = tail call i32 @memmove(i8* %0, i8* %2, i32 %16)
br label %18
18: ; preds = %10, %12, %14
%19 = phi i8* [ %0, %14 ], [ null, %12 ], [ null, %10 ]
ret i8* %19
}