#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 */
char *sha1_hash(char const *);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int changepass_sha(const char *pass, char *buffer, size_t len) {
  const char *cipher;
  if (!pass || !buffer || len <= 0)
    return -1;
  if (len < strlen("{SHA}") + 20)
    return -1;
  strncpy(buffer, "{SHA}", len);
  cipher = sha1_hash(pass);
  strncpy(buffer + strlen("{SHA}"), cipher, len);
  return 0;
}


x86 assembly (clang, -O0):
<changepass_sha>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1165 <changepass_sha+0x35>
cmpq   $0x0,-0x18(%rbp)
je     1165 <changepass_sha+0x35>
cmpq   $0x0,-0x20(%rbp)
ja     1171 <changepass_sha+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <changepass_sha+0xc5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    0xe80(%rip),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    $0x14,%rcx
cmp    %rcx,%rax
jae    11a5 <changepass_sha+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <changepass_sha+0xc5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe4c(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rdi
call   1050 <sha1_hash@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe2b(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<changepass_sha>:
push   %r15
push   %r14
push   %rbx
mov    $0xffffffff,%eax
test   %rdi,%rdi
je     1171 <changepass_sha+0x51>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1171 <changepass_sha+0x51>
mov    %rdx,%r14
cmp    $0x19,%rdx
jb     1171 <changepass_sha+0x51>
mov    %rdi,%r15
lea    0xeb6(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1040 <sha1_hash@plt>
add    $0x5,%rbx
mov    %rbx,%rdi
mov    %rax,%rsi
mov    %r14,%rdx
call   1030 <strncpy@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [6 x i8] c"{SHA}\00"
define i32 @changepass_sha(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i8*, i8** %6
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i64, i64* %7
%16 = icmp ule i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14, %11, %3
store i32 -1, i32* %4
br label %36
18: ; preds = %14
%19 = load i64, i64* %7
%20 = call i64 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = add i64 %20, 20
%22 = icmp ult i64 %19, %21
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %4
br label %36
24: ; preds = %18
%25 = load i8*, i8** %6
%26 = load i64, i64* %7
%27 = call i32 @strncpy(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %26)
%28 = load i8*, i8** %5
%29 = call i8* @sha1_hash(i8* %28)
store i8* %29, i8** %8
%30 = load i8*, i8** %6
%31 = call i64 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%32 = getelementptr inbounds i8, i8* %30, i64 %31
%33 = load i8*, i8** %8
%34 = load i64, i64* %7
%35 = call i32 @strncpy(i8* %32, i8* %33, i64 %34)
store i32 0, i32* %4
br label %36
36: ; preds = %24, %23, %17
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@.str = [6 x i8] c"{SHA}\00"
define i32 @changepass_sha(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %0, null
%5 = icmp eq i8* %1, null
%6 = or i1 %4, %5
%7 = icmp ult i64 %2, 25
%8 = or i1 %6, %7
br i1 %8, label %14, label %9
9: ; preds = %3
%10 = tail call i32 @strncpy(i8* %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %2)
%11 = tail call i8* @sha1_hash(i8* %0)
%12 = getelementptr inbounds i8, i8* %1, i64 5
%13 = tail call i32 @strncpy(i8* %12, i8* %11, i64 %2)
br label %14
14: ; preds = %3, %9
%15 = phi i32 [ 0, %9 ], [ -1, %3 ]
ret i32 %15
}