#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 gob_encode_unsigned_int(char *, size_t, size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int gob_encode_string(char *buf, size_t buf_size, const char *s) {
  size_t len = strlen(s);
  int encoded_len_size = gob_encode_unsigned_int(buf, buf_size, len);
  buf += encoded_len_size;
  buf_size -= encoded_len_size;
  if (buf_size > 0) {
    strncpy(buf, s, buf_size);
  }
  return len + encoded_len_size;
}


x86 assembly (clang, -O0):
<gob_encode_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <gob_encode_unsigned_int@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
movslq -0x24(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jbe    11a1 <gob_encode_string+0x71>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gob_encode_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rdx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r14
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <gob_encode_unsigned_int@plt>
mov    %eax,%ebp
cltq
sub    %rax,%rbx
je     1174 <gob_encode_string+0x44>
add    %rax,%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
add    %r14d,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @gob_encode_string(i8* %0, i64 %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%5 = tail call i32 @gob_encode_unsigned_int(i8* %0, i64 %1, i64 %4)
%6 = sext i32 %5 to i64
%7 = sub i64 %1, %6
%8 = icmp eq i64 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %3
%10 = getelementptr inbounds i8, i8* %0, i64 %6
%11 = tail call i32 @strncpy(i8* %10, i8* %2, i64 %7)
br label %12
12: ; preds = %9, %3
%13 = trunc i64 %4 to i32
%14 = add i32 %5, %13
ret i32 %14
}