#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 /*<<< 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 /*<<< orphan*/ stdin;
int strlen(char *);
int write(int, char *, int);
void good1() {
  {
    char filename[100] = "";
    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';
    }
    fileDesc = open(filename, O_RDWR);
    if (fileDesc == -1) {
      exit(1);
    }
    if (write(fileDesc, "Good Sink...", strlen("Good Sink...")) == -1) {
      exit(1);
    }
    if (fileDesc != -1) {
      close(fileDesc);
    }
  }
}


x86 assembly (clang, -O0):
<good1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
lea    -0x70(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
movl   $0xffffffff,-0x74(%rbp)
lea    -0x70(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
jne    11d3 <good1+0x53>
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
movb   $0x0,-0x70(%rbp)
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    11f8 <good1+0x78>
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x70(%rbp,%rax,1)
lea    -0x70(%rbp),%rdi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0xffffffff,-0x74(%rbp)
jne    1221 <good1+0xa1>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x74(%rbp),%eax
mov    %eax,-0x78(%rbp)
lea    0xde2(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x78(%rbp),%edi
mov    %eax,%edx
lea    0xdd1(%rip),%rsi
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1257 <good1+0xd7>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpl   $0xffffffff,-0x74(%rbp)
je     1269 <good1+0xe9>
mov    -0x74(%rbp),%edi
call   1070 <close@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<good1>:
push   %r14
push   %rbx
sub    $0x68,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    11ca <good1+0x5a>
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
movb   $0x0,(%rsp)
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    11e8 <good1+0x78>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     122f <good1+0xbf>
mov    %eax,%ebx
lea    0xdf9(%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     122f <good1+0xbf>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r14
ret
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@stdin = global i32 0
@O_RDWR = global i32 0
@.str.1 = [13 x i8] c"Good Sink...\00"
@str = [15 x i8] c"fgets() failed\00"
define void @good1() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @stdin
%4 = call i32* @fgets(i8* %2, i32 100, i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
store i8 0, i8* %2
br label %8
8: ; preds = %6, %0
%9 = call i32 @strlen(i8* %2)
%10 = icmp sgt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = call i32 @strlen(i8* %2)
%13 = add nsw i32 %12, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %14
store i8 0, i8* %15
br label %16
16: ; preds = %11, %8
%17 = load i32, i32* @O_RDWR
%18 = call i32 @open(i8* %2, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16
%23 = call i32 @strlen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @write(i32 %18, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%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 = call i32 @close(i32 %18)
ret void
}