#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 */
scalar_t__ alloca(int);
char *crypt(char *, char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stdin;
size_t strlen(char *);
void CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad() {
  {
    char *password = (char *)alloca(100 * sizeof(char));
    char *epasswd = (char *)alloca(100 * sizeof(char));
    size_t passwordLen = 0;
    password[0] = '\0';
    if (fgets(password, 100, stdin) == NULL) {
      printf("fgets() failed\n");
      password[0] = '\0';
    }
    passwordLen = strlen(password);
    if (passwordLen > 0) {
      password[passwordLen - 1] = '\0';
    }
    epasswd = crypt(password, "ab");
    if (epasswd == NULL) {
      printf("crypt error\n");
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x64,%edi
call   1070 <alloca@plt>
mov    %rax,-0x8(%rbp)
mov    $0x64,%edi
call   1070 <alloca@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11c7 <CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad+0x67>
lea    0xe45(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11ef <CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad+0x8f>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1030 <crypt@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1224 <CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad+0xc4>
lea    0xdfe(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad>:
push   %rbx
mov    $0x64,%edi
call   1070 <alloca@plt>
mov    %rax,%rbx
mov    $0x64,%edi
call   1070 <alloca@plt>
movb   $0x0,(%rbx)
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <fgets@plt>
test   %rax,%rax
jne    11a5 <CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad+0x45>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
movb   $0x0,(%rbx)
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
je     11b7 <CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad+0x57>
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe42(%rip),%rsi
mov    %rbx,%rdi
call   1040 <crypt@plt>
test   %rax,%rax
je     11cd <CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad+0x6d>
pop    %rbx
ret
lea    0xe2f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [16 x i8] c"fgets() failed\0A\00"
@.str.1 = [3 x i8] c"ab\00"
@.str.2 = [13 x i8] c"crypt error\0A\00"
define void @CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i64
%4 = call i64 @alloca(i32 100)
%5 = inttoptr i64 %4 to i8*
store i8* %5, i8** %1
%6 = call i64 @alloca(i32 100)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %2
store i64 0, i64* %3
%8 = load i8*, i8** %1
%9 = getelementptr inbounds i8, i8* %8, i64 0
store i8 0, i8* %9
%10 = load i8*, i8** %1
%11 = load i32, i32* @stdin
%12 = call i32* @fgets(i8* %10, i32 100, i32 %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = load i8*, i8** %1
%17 = getelementptr inbounds i8, i8* %16, i64 0
store i8 0, i8* %17
br label %18
18: ; preds = %14, %0
%19 = load i8*, i8** %1
%20 = call i64 @strlen(i8* %19)
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i8*, i8** %1
%25 = load i64, i64* %3
%26 = sub i64 %25, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %23, %18
%29 = load i8*, i8** %1
%30 = call i8* @crypt(i8* %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %30, i8** %2
%31 = load i8*, i8** %2
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.1 = [3 x i8] c"ab\00"
@str = [12 x i8] c"crypt error\00"
@str.3 = [15 x i8] c"fgets() failed\00"
define void @CWE226_Sensitive_Information_Uncleared_Before_Release__w32_char_alloca_01_bad() {
%1 = tail call i64 @alloca(i32 100)
%2 = inttoptr i64 %1 to i8*
%3 = tail call i64 @alloca(i32 100)
store i8 0, i8* %2
%4 = load i32, i32* @stdin
%5 = tail call i32* @fgets(i8* %2, i32 100, i32 %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.3, i64 0, i64 0))
store i8 0, i8* %2
br label %9
9: ; preds = %7, %0
%10 = tail call i64 @strlen(i8* %2)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = add i64 %10, -1
%14 = getelementptr inbounds i8, i8* %2, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %12, %9
%16 = tail call i8* @crypt(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
ret void
}