#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int write(int, char *, int);
void CWE367_TOC_TOU__stat_01_bad() {
  {
    char filename[100] = "";
    struct stat statBuffer;
    int fileDesc = -1;
    if (fgets(filename, 100, stdin) == NULL) {
      printf("fgets() failed\n");
      filename[0] = '\0';
    }
    if (strlen(filename) > 0) {
      filename[strlen(filename) - 1] = '\0';
    }
    if (stat(filename, &statBuffer) == -1) {
      exit(1);
    }
    fileDesc = open(filename, O_RDWR);
    if (fileDesc == -1) {
      exit(1);
    }
    if (write(fileDesc, "Bad Sink...", strlen("Bad Sink...")) == -1) {
      exit(1);
    }
    if (fileDesc != -1) {
      close(fileDesc);
    }
  }
}


x86 assembly (clang, -O0):
<CWE367_TOC_TOU__stat_01_bad>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
movl   $0xffffffff,-0x7c(%rbp)
lea    -0x70(%rbp),%rdi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    11e3 <CWE367_TOC_TOU__stat_01_bad+0x53>
lea    0xe26(%rip),%rdi
call   1050 <printf@plt>
movb   $0x0,-0x70(%rbp)
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    1208 <CWE367_TOC_TOU__stat_01_bad+0x78>
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rdi
lea    -0x78(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
jne    1228 <CWE367_TOC_TOU__stat_01_bad+0x98>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x70(%rbp),%rdi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0xffffffff,-0x7c(%rbp)
jne    1251 <CWE367_TOC_TOU__stat_01_bad+0xc1>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x7c(%rbp),%eax
mov    %eax,-0x80(%rbp)
lea    0xdb2(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x80(%rbp),%edi
mov    %eax,%edx
lea    0xda1(%rip),%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1287 <CWE367_TOC_TOU__stat_01_bad+0xf7>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0xffffffff,-0x7c(%rbp)
je     1299 <CWE367_TOC_TOU__stat_01_bad+0x109>
mov    -0x7c(%rbp),%edi
call   1070 <close@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CWE367_TOC_TOU__stat_01_bad>:
push   %r14
push   %rbx
sub    $0x78,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    11de <CWE367_TOC_TOU__stat_01_bad+0x5e>
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
movb   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11fe <CWE367_TOC_TOU__stat_01_bad+0x7e>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
je     125b <CWE367_TOC_TOU__stat_01_bad+0xdb>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     125b <CWE367_TOC_TOU__stat_01_bad+0xdb>
mov    %eax,%ebx
lea    0xdcd(%rip),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     125b <CWE367_TOC_TOU__stat_01_bad+0xdb>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stdin = global i32 0
@.str = [16 x i8] c"fgets() failed\0A\00"
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"Bad Sink...\00"
define void @CWE367_TOC_TOU__stat_01_bad() {
%1 = alloca [100 x i8]
%2 = alloca %struct.stat
%3 = alloca i32
%4 = bitcast [100 x i8]* %1 to i8*
store i32 -1, i32* %3
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%6 = load i32, i32* @stdin
%7 = call i32* @fgets(i8* %5, i32 100, i32 %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
store i8 0, i8* %11
br label %12
12: ; preds = %9, %0
%13 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%14 = call i32 @strlen(i8* %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %16, %12
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%24 = call i32 @stat(i8* %23, %struct.stat* %2)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = load i32, i32* %3
%38 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @write(i32 %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
%44 = load i32, i32* %3
%45 = icmp ne i32 %44, -1
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32, i32* %3
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %46, %43
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stdin = global i32 0
@O_RDWR = global i32 0
@.str.1 = [12 x i8] c"Bad Sink...\00"
@str = [15 x i8] c"fgets() failed\00"
define void @CWE367_TOC_TOU__stat_01_bad() {
%1 = alloca [100 x i8]
%2 = alloca %struct.stat
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.stat* %2 to i8*
%5 = load i32, i32* @stdin
%6 = call i32* @fgets(i8* %3, i32 100, i32 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
store i8 0, i8* %3
br label %10
10: ; preds = %8, %0
%11 = call i32 @strlen(i8* %3)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %10
%14 = call i32 @strlen(i8* %3)
%15 = add nsw i32 %14, -1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %13, %10
%19 = call i32 @stat(i8* %3, %struct.stat* %2)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %18
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* %3, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %23
%30 = call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @write(i32 %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = call i32 @close(i32 %25)
ret void
}