#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 */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int __unserialize_string(char *out, const uint8_t *data, int s) {
  if (s < 1)
    return (-1);
  size_t n = (strlen((const char *)data) + 1 < (size_t)s
                  ? strlen((const char *)data) + 1
                  : (size_t)s);
  ;
  n = (n < 50 ? n : 50);
  ;
  strncpy(out, (const char *)data, n);
  out[n - 1] = '\0';
  return ((int)n);
}


x86 assembly (clang, -O0):
<__unserialize_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jge    1149 <__unserialize_string+0x29>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <__unserialize_string+0xc1>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jae    117b <__unserialize_string+0x5b>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
jmp    1183 <__unserialize_string+0x63>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x32,-0x28(%rbp)
jae    11a3 <__unserialize_string+0x83>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11b1 <__unserialize_string+0x91>
mov    $0x32,%eax
mov    %rax,-0x38(%rbp)
jmp    11b1 <__unserialize_string+0x91>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__unserialize_string>:
push   %rbp
push   %r14
push   %rbx
test   %edx,%edx
jle    1172 <__unserialize_string+0x52>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    %ebp,%eax
jae    114c <__unserialize_string+0x2c>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
add    $0x1,%ebp
movslq %ebp,%rax
cmp    $0x32,%rax
mov    $0x32,%ebp
cmovb  %rax,%rbp
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbp,%r14,1)
jmp    1177 <__unserialize_string+0x57>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @__unserialize_string(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i64
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%9 = load i32, i32* %7
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %12
11: ; preds = %3
store i32 -1, i32* %4
br label %50
12: ; preds = %3
%13 = load i32*, i32** %6
%14 = bitcast i32* %13 to i8*
%15 = call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = icmp ult i64 %17, %19
br i1 %20, label %21, label %27
21: ; preds = %12
%22 = load i32*, i32** %6
%23 = bitcast i32* %22 to i8*
%24 = call i32 @strlen(i8* %23)
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
br label %30
27: ; preds = %12
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
br label %30
30: ; preds = %27, %21
%31 = phi i64 [ %26, %21 ], [ %29, %27 ]
store i64 %31, i64* %8
%32 = load i64, i64* %8
%33 = icmp ult i64 %32, 50
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = load i64, i64* %8
br label %37
36: ; preds = %30
br label %37
37: ; preds = %36, %34
%38 = phi i64 [ %35, %34 ], [ 50, %36 ]
store i64 %38, i64* %8
%39 = load i8*, i8** %5
%40 = load i32*, i32** %6
%41 = bitcast i32* %40 to i8*
%42 = load i64, i64* %8
%43 = call i32 @strncpy(i8* %39, i8* %41, i64 %42)
%44 = load i8*, i8** %5
%45 = load i64, i64* %8
%46 = sub i64 %45, 1
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i64, i64* %8
%49 = trunc i64 %48 to i32
store i32 %49, i32* %4
br label %50
50: ; preds = %37, %11
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
define i32 @__unserialize_string(i8* %0, i32* %1, i32 %2) {
%4 = icmp slt i32 %2, 1
br i1 %4, label %22, label %5
5: ; preds = %3
%6 = bitcast i32* %1 to i8*
%7 = tail call i32 @strlen(i8* %6)
%8 = add nsw i32 %7, 1
%9 = icmp ult i32 %8, %2
br i1 %9, label %10, label %13
10: ; preds = %5
%11 = tail call i32 @strlen(i8* %6)
%12 = add nsw i32 %11, 1
br label %13
13: ; preds = %5, %10
%14 = phi i32 [ %12, %10 ], [ %2, %5 ]
%15 = sext i32 %14 to i64
%16 = icmp ult i64 %15, 50
%17 = select i1 %16, i64 %15, i64 50
%18 = tail call i32 @strncpy(i8* %0, i8* %6, i64 %17)
%19 = add nsw i64 %17, -1
%20 = getelementptr inbounds i8, i8* %0, i64 %19
store i8 0, i8* %20
%21 = trunc i64 %17 to i32
br label %22
22: ; preds = %3, %13
%23 = phi i32 [ %21, %13 ], [ -1, %3 ]
ret i32 %23
}