#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 {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
  int st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int O_RDWR;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ lstat(char const *, struct stat *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char const *, int);
int /*<<< orphan*/ safe_fclose(int /*<<< orphan*/ **);
int /*<<< orphan*/ unlink(char const *);
void mutt_unlink(const char *s) {
  int fd;
  int flags;
  FILE *f;
  struct stat sb, sb2;
  char buf[2048];
  flags = O_RDWR;
  if (lstat(s, &sb) == 0 && S_ISREG(sb.st_mode)) {
    if ((fd = open(s, flags)) < 0)
      return;
    if ((fstat(fd, &sb2) != 0) || !S_ISREG(sb2.st_mode) ||
        (sb.st_dev != sb2.st_dev) || (sb.st_ino != sb2.st_ino)) {
      close(fd);
      return;
    }
    if ((f = fdopen(fd, "r+"))) {
      unlink(s);
      memset(buf, 0, sizeof(buf));
      while (sb.st_size > 0) {
        fwrite(buf, 1,
               ((sizeof(buf)) < (sb.st_size) ? (sizeof(buf)) : (sb.st_size)),
               f);
        sb.st_size -=
            ((sizeof(buf)) < (sb.st_size) ? (sizeof(buf)) : (sb.st_size));
      }
      safe_fclose(&f);
    }
  }
}


x86 assembly (clang, -O0):
<mutt_unlink>:
push   %rbp
mov    %rsp,%rbp
sub    $0x870,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jne    1341 <mutt_unlink+0x1a1>
mov    -0x1c(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     1341 <mutt_unlink+0x1a1>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1201 <mutt_unlink+0x61>
jmp    1341 <mutt_unlink+0x1a1>
mov    -0xc(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   10c0 <fstat@plt>
cmp    $0x0,%rax
jne    1245 <mutt_unlink+0xa5>
mov    -0x34(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%rax
je     1245 <mutt_unlink+0xa5>
mov    -0x30(%rbp),%rax
cmp    -0x48(%rbp),%rax
jne    1245 <mutt_unlink+0xa5>
mov    -0x28(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     1252 <mutt_unlink+0xb2>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
jmp    1341 <mutt_unlink+0x1a1>
mov    -0xc(%rbp),%edi
lea    0xda4(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     133c <mutt_unlink+0x19c>
mov    -0x8(%rbp),%rdi
call   1030 <unlink@plt>
lea    -0x850(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
cmpl   $0x0,-0x20(%rbp)
jle    1333 <mutt_unlink+0x193>
lea    -0x850(%rbp),%rax
mov    %rax,-0x858(%rbp)
movslq -0x20(%rbp),%rcx
mov    $0x800,%eax
cmp    %rcx,%rax
jae    12c6 <mutt_unlink+0x126>
mov    $0x800,%eax
mov    %rax,-0x860(%rbp)
jmp    12d1 <mutt_unlink+0x131>
movslq -0x20(%rbp),%rax
mov    %rax,-0x860(%rbp)
mov    -0x858(%rbp),%rdi
mov    -0x860(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
movslq -0x20(%rbp),%rcx
mov    $0x800,%eax
cmp    %rcx,%rax
jae    1312 <mutt_unlink+0x172>
mov    $0x800,%eax
mov    %rax,-0x868(%rbp)
jmp    131d <mutt_unlink+0x17d>
movslq -0x20(%rbp),%rax
mov    %rax,-0x868(%rbp)
mov    -0x868(%rbp),%rcx
movslq -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x20(%rbp)
jmp    128b <mutt_unlink+0xeb>
lea    -0x18(%rbp),%rdi
call   10a0 <safe_fclose@plt>
jmp    1341 <mutt_unlink+0x1a1>
add    $0x870,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mutt_unlink>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x840,%rsp
mov    %rdi,%rbx
mov    0x2e2f(%rip),%rax
mov    (%rax),%ebp
lea    0x10(%rsp),%rsi
call   1060 <lstat@plt>
test   %rax,%rax
je     11da <mutt_unlink+0x3a>
add    $0x840,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x24(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     11ca <mutt_unlink+0x2a>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1090 <open@plt>
test   %eax,%eax
js     11ca <mutt_unlink+0x2a>
mov    %eax,%ebp
lea    0x28(%rsp),%rsi
mov    %eax,%edi
call   10c0 <fstat@plt>
test   %rax,%rax
je     121f <mutt_unlink+0x7f>
mov    %ebp,%edi
add    $0x840,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <close@plt>
mov    0x3c(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %rax,%rax
je     1209 <mutt_unlink+0x69>
mov    0x10(%rsp),%rax
cmp    0x28(%rsp),%rax
jne    1209 <mutt_unlink+0x69>
mov    0x18(%rsp),%rax
cmp    0x30(%rsp),%rax
jne    1209 <mutt_unlink+0x69>
lea    0xdb4(%rip),%rsi
mov    %ebp,%edi
call   1080 <fdopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
je     11ca <mutt_unlink+0x2a>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <unlink@plt>
lea    0x40(%rsp),%rdi
xor    %ebx,%ebx
mov    $0x800,%r14d
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
mov    0x20(%rsp),%eax
test   %eax,%eax
jle    12ed <mutt_unlink+0x14d>
mov    %eax,%ebp
sub    $0x800,%ebp
cmovb  %eax,%r14d
cmovb  %ebx,%ebp
lea    0x40(%rsp),%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15,%rcx
call   10b0 <fwrite@plt>
test   %ebp,%ebp
jle    12ed <mutt_unlink+0x14d>
xor    %r14d,%r14d
mov    $0x800,%r15d
lea    0x40(%rsp),%r12
mov    %ebp,%ebx
mov    0x8(%rsp),%rcx
sub    $0x800,%ebx
cmovae %r15d,%ebp
cmovb  %r14d,%ebx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
call   10b0 <fwrite@plt>
mov    %ebx,%ebp
test   %ebx,%ebx
jg     12c5 <mutt_unlink+0x125>
lea    0x8(%rsp),%rdi
call   10a0 <safe_fclose@plt>
jmp    11ca <mutt_unlink+0x2a>

LLVM IR (-O0):
%struct.stat = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@.str = [3 x i8] c"r+\00"
define void @mutt_unlink(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.stat
%7 = alloca %struct.stat
%8 = alloca [2048 x i8]
store i8* %0, i8** %2
%9 = load i32, i32* @O_RDWR
store i32 %9, i32* %4
%10 = load i8*, i8** %2
%11 = call i64 @lstat(i8* %10, %struct.stat* %6)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %96
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 3
%15 = load i32, i32* %14
%16 = call i64 @S_ISREG(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %96
18: ; preds = %13
%19 = load i8*, i8** %2
%20 = load i32, i32* %4
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %96
24: ; preds = %18
%25 = load i32, i32* %3
%26 = call i64 @fstat(i32 %25, %struct.stat* %7)
%27 = icmp ne i64 %26, 0
br i1 %27, label %45, label %28
28: ; preds = %24
%29 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 3
%30 = load i32, i32* %29
%31 = call i64 @S_ISREG(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %45
33: ; preds = %28
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i64, i64* %34
%36 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%37 = load i64, i64* %36
%38 = icmp ne i64 %35, %37
br i1 %38, label %45, label %39
39: ; preds = %33
%40 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%41 = load i64, i64* %40
%42 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%43 = load i64, i64* %42
%44 = icmp ne i64 %41, %43
br i1 %44, label %45, label %48
45: ; preds = %39, %33, %28, %24
%46 = load i32, i32* %3
%47 = call i32 @close(i32 %46)
br label %96
48: ; preds = %39
%49 = load i32, i32* %3
%50 = call i32* @fdopen(i32 %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %50, i32** %5
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %95
52: ; preds = %48
%53 = load i8*, i8** %2
%54 = call i32 @unlink(i8* %53)
%55 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%56 = call i32 @memset(i8* %55, i32 0, i32 2048)
br label %57
57: ; preds = %86, %52
%58 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%59 = load i32, i32* %58
%60 = icmp sgt i32 %59, 0
br i1 %60, label %61, label %93
61: ; preds = %57
%62 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%63 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%64 = load i32, i32* %63
%65 = sext i32 %64 to i64
%66 = icmp ult i64 2048, %65
br i1 %66, label %67, label %68
67: ; preds = %61
br label %72
68: ; preds = %61
%69 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%70 = load i32, i32* %69
%71 = sext i32 %70 to i64
br label %72
72: ; preds = %68, %67
%73 = phi i64 [ 2048, %67 ], [ %71, %68 ]
%74 = trunc i64 %73 to i32
%75 = load i32*, i32** %5
%76 = call i32 @fwrite(i8* %62, i32 1, i32 %74, i32* %75)
%77 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%78 = load i32, i32* %77
%79 = sext i32 %78 to i64
%80 = icmp ult i64 2048, %79
br i1 %80, label %81, label %82
81: ; preds = %72
br label %86
82: ; preds = %72
%83 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%84 = load i32, i32* %83
%85 = sext i32 %84 to i64
br label %86
86: ; preds = %82, %81
%87 = phi i64 [ 2048, %81 ], [ %85, %82 ]
%88 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%89 = load i32, i32* %88
%90 = sext i32 %89 to i64
%91 = sub i64 %90, %87
%92 = trunc i64 %91 to i32
store i32 %92, i32* %88
br label %57
93: ; preds = %57
%94 = call i32 @safe_fclose(i32** %5)
br label %95
95: ; preds = %93, %48
br label %96
96: ; preds = %23, %45, %95, %13, %1
ret void
}

LLVM IR (-O2):
%struct.stat = type { i64, i64, i32, i32 }
@O_RDWR = global i32 0
@.str = [3 x i8] c"r+\00"
define void @mutt_unlink(i8* nocapture %0) {
%2 = alloca i32*
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca [2048 x i8]
%6 = bitcast i32** %2 to i8*
%7 = bitcast %struct.stat* %3 to i8*
%8 = bitcast %struct.stat* %4 to i8*
%9 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* @O_RDWR
%11 = call i64 @lstat(i8* %0, %struct.stat* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %68
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 3
%15 = load i32, i32* %14
%16 = tail call i64 @S_ISREG(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %68, label %18
18: ; preds = %13
%19 = tail call i32 @open(i8* %0, i32 %10)
%20 = icmp slt i32 %19, 0
br i1 %20, label %68, label %21
21: ; preds = %18
%22 = call i64 @fstat(i32 %19, %struct.stat* %4)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %41
24: ; preds = %21
%25 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 3
%26 = load i32, i32* %25
%27 = tail call i64 @S_ISREG(i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %41, label %29
29: ; preds = %24
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i64, i64* %30
%32 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%33 = load i64, i64* %32
%34 = icmp eq i64 %31, %33
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%37 = load i64, i64* %36
%38 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%39 = load i64, i64* %38
%40 = icmp eq i64 %37, %39
br i1 %40, label %43, label %41
41: ; preds = %35, %29, %24, %21
%42 = tail call i32 @close(i32 %19)
br label %68
43: ; preds = %35
%44 = tail call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %44, i32** %2
%45 = icmp eq i32* %44, null
br i1 %45, label %68, label %46
46: ; preds = %43
%47 = tail call i32 @unlink(i8* %0)
%48 = call i32 @memset(i8* %9, i32 0, i32 2048)
%49 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%50 = load i32, i32* %49
%51 = icmp sgt i32 %50, 0
br i1 %51, label %52, label %66
52: ; preds = %46
%53 = icmp ult i32 %50, 2048
%54 = select i1 %53, i32 %50, i32 2048
%55 = call i32 @fwrite(i8* %9, i32 1, i32 %54, i32* %44)
%56 = call i32 @llvm.usub.sat.i32(i32 %50, i32 2048)
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %66
58: ; preds = %52, %58
%59 = phi i32 [ %64, %58 ], [ %56, %52 ]
%60 = load i32*, i32** %2
%61 = icmp ult i32 %59, 2048
%62 = select i1 %61, i32 %59, i32 2048
%63 = call i32 @fwrite(i8* %9, i32 1, i32 %62, i32* %60)
%64 = call i32 @llvm.usub.sat.i32(i32 %59, i32 2048)
%65 = icmp sgt i32 %64, 0
br i1 %65, label %58, label %66
66: ; preds = %58, %52, %46
%67 = call i32 @safe_fclose(i32** %2)
br label %68
68: ; preds = %1, %13, %66, %43, %18, %41
ret void
}