#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 /*<<< orphan*/ st_mode;
  int /*<<< orphan*/ st_gid;
  int /*<<< orphan*/ st_uid;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EOF;
int chmod(char *, int /*<<< orphan*/);
int chown(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *);
scalar_t__ rename(char *, char *);
scalar_t__ sscanf(char *, char *, char *);
int stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int cm_delete_entry(char *name, char *pattern, char *file, char *tmpfile) {
  char buf[2048], match[256];
  size_t bs = sizeof(buf);
  int i;
  FILE *o, *n;
  struct stat s;
  errno = 0;
  if ((o = fopen(file, "r")) == NULL) {
    return -errno;
  }
  if ((n = fopen(tmpfile, "w")) == NULL) {
    fclose(o);
    return -errno;
  }
  while (fgets(buf, bs, o) != NULL) {
    if (sscanf(buf, pattern, match) != EOF) {
      if (strcmp(name, match) != 0) {
        if (fprintf(n, buf) == -1) {
          goto cleanup;
        }
      }
    }
  }
  if ((i = stat(file, &s)) == -1) {
    goto cleanup;
  }
  if ((i = chown(tmpfile, s.st_uid, s.st_gid)) != 0) {
    goto cleanup;
  }
  if ((i = chmod(tmpfile, s.st_mode)) != 0) {
    goto cleanup;
  }
  if (rename(tmpfile, file) != 0) {
    goto cleanup;
  }
  fclose(o);
  fclose(n);
  return 0;
cleanup:
  fclose(o);
  fclose(n);
  return -errno;
}


x86 assembly (clang, -O0):
<cm_delete_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x960,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movq   $0x800,-0x938(%rbp)
mov    0x2e0b(%rip),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    0xe22(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x948(%rbp)
cmp    $0x0,%rax
jne    1207 <cm_delete_entry+0x67>
xor    %eax,%eax
mov    0x2ddb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13b7 <cm_delete_entry+0x217>
mov    -0x28(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x950(%rbp)
cmp    $0x0,%rax
jne    1247 <cm_delete_entry+0xa7>
mov    -0x948(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2d9b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13b7 <cm_delete_entry+0x217>
jmp    124c <cm_delete_entry+0xac>
lea    -0x830(%rbp),%rdi
mov    -0x938(%rbp),%rsi
mov    -0x948(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     12e1 <cm_delete_entry+0x141>
lea    -0x830(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x930(%rbp),%rdx
call   1080 <sscanf@plt>
mov    0x2d5a(%rip),%rcx
cmp    (%rcx),%rax
je     12dc <cm_delete_entry+0x13c>
mov    -0x10(%rbp),%rdi
lea    -0x930(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     12d7 <cm_delete_entry+0x137>
mov    -0x950(%rbp),%rdi
lea    -0x830(%rbp),%rsi
call   1060 <fprintf@plt>
cmp    $0xffffffff,%eax
jne    12d2 <cm_delete_entry+0x132>
jmp    1391 <cm_delete_entry+0x1f1>
jmp    12d7 <cm_delete_entry+0x137>
jmp    12dc <cm_delete_entry+0x13c>
jmp    124c <cm_delete_entry+0xac>
mov    -0x20(%rbp),%rdi
lea    -0x960(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x93c(%rbp)
cmp    $0xffffffff,%eax
jne    1305 <cm_delete_entry+0x165>
jmp    1391 <cm_delete_entry+0x1f1>
mov    -0x28(%rbp),%rdi
mov    -0x958(%rbp),%esi
mov    -0x95c(%rbp),%edx
call   1090 <chown@plt>
mov    %eax,-0x93c(%rbp)
cmp    $0x0,%eax
je     132e <cm_delete_entry+0x18e>
jmp    1391 <cm_delete_entry+0x1f1>
mov    -0x28(%rbp),%rdi
mov    -0x960(%rbp),%esi
call   10a0 <chmod@plt>
mov    %eax,-0x93c(%rbp)
cmp    $0x0,%eax
je     1351 <cm_delete_entry+0x1b1>
jmp    1391 <cm_delete_entry+0x1f1>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10c0 <rename@plt>
cmp    $0x0,%rax
je     136d <cm_delete_entry+0x1cd>
jmp    1391 <cm_delete_entry+0x1f1>
mov    -0x948(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x950(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    13b7 <cm_delete_entry+0x217>
mov    -0x948(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x950(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2c26(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x960,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cm_delete_entry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x938,%rsp
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,0x28(%rsp)
mov    0x2e12(%rip),%r14
movl   $0x0,(%r14)
lea    0xe2c(%rip),%rsi
mov    %rdx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1302 <cm_delete_entry+0x162>
mov    %rax,%r13
lea    0xe13(%rip),%rsi
mov    %r15,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12fa <cm_delete_entry+0x15a>
mov    %rax,%r12
mov    %r15,0x8(%rsp)
mov    %rbp,0x10(%rsp)
lea    0x130(%rsp),%rdi
mov    $0x800,%esi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
mov    %r12,%rbp
je     128d <cm_delete_entry+0xed>
lea    0x130(%rsp),%r15
lea    0x30(%rsp),%r14
mov    0x2daa(%rip),%r12
jmp    1255 <cm_delete_entry+0xb5>
mov    $0x800,%esi
mov    %r15,%rdi
mov    %r13,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     128d <cm_delete_entry+0xed>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <sscanf@plt>
cmp    (%r12),%rax
je     1240 <cm_delete_entry+0xa0>
mov    0x28(%rsp),%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1240 <cm_delete_entry+0xa0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1060 <fprintf@plt>
cmp    $0xffffffff,%eax
jne    1240 <cm_delete_entry+0xa0>
jmp    12dd <cm_delete_entry+0x13d>
lea    0x18(%rsp),%rsi
mov    0x10(%rsp),%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     12dd <cm_delete_entry+0x13d>
mov    0x1c(%rsp),%edx
mov    0x20(%rsp),%esi
mov    0x8(%rsp),%rdi
call   1090 <chown@plt>
test   %eax,%eax
jne    12dd <cm_delete_entry+0x13d>
mov    0x18(%rsp),%esi
mov    0x8(%rsp),%rdi
call   10a0 <chmod@plt>
test   %eax,%eax
jne    12dd <cm_delete_entry+0x13d>
mov    0x8(%rsp),%rdi
mov    0x10(%rsp),%rsi
call   10c0 <rename@plt>
test   %rax,%rax
je     1319 <cm_delete_entry+0x179>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2ce2(%rip),%rcx
sub    (%rcx),%eax
jmp    1307 <cm_delete_entry+0x167>
mov    %r13,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
sub    (%r14),%eax
add    $0x938,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    %rbp,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1307 <cm_delete_entry+0x167>

LLVM IR (-O0):
%struct.stat = type { i32, i32, i32 }
@errno = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i64 0
define i32 @cm_delete_entry(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [2048 x i8]
%11 = alloca [256 x i8]
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca %struct.stat
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i64 2048, i64* %12
store i32 0, i32* @errno
%17 = load i8*, i8** %8
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %14
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %4
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
store i32 %22, i32* %5
br label %99
23: ; preds = %4
%24 = load i8*, i8** %9
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %15
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = load i32*, i32** %14
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* @errno
%31 = sub nsw i32 0, %30
store i32 %31, i32* %5
br label %99
32: ; preds = %23
br label %33
33: ; preds = %59, %32
%34 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%35 = load i64, i64* %12
%36 = load i32*, i32** %14
%37 = call i32* @fgets(i8* %34, i64 %35, i32* %36)
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %60
39: ; preds = %33
%40 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%41 = load i8*, i8** %7
%42 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%43 = call i64 @sscanf(i8* %40, i8* %41, i8* %42)
%44 = load i64, i64* @EOF
%45 = icmp ne i64 %43, %44
br i1 %45, label %46, label %59
46: ; preds = %39
%47 = load i8*, i8** %6
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%49 = call i64 @strcmp(i8* %47, i8* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %46
%52 = load i32*, i32** %15
%53 = getelementptr inbounds [2048 x i8], [2048 x i8]* %10, i64 0, i64 0
%54 = call i32 @fprintf(i32* %52, i8* %53)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %57
56: ; preds = %51
br label %92
57: ; preds = %51
br label %58
58: ; preds = %57, %46
br label %59
59: ; preds = %58, %39
br label %33
60: ; preds = %33
%61 = load i8*, i8** %8
%62 = call i32 @stat(i8* %61, %struct.stat* %16)
store i32 %62, i32* %13
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %65
64: ; preds = %60
br label %92
65: ; preds = %60
%66 = load i8*, i8** %9
%67 = getelementptr inbounds %struct.stat, %struct.stat* %16, i32 0, i32 2
%68 = load i32, i32* %67
%69 = getelementptr inbounds %struct.stat, %struct.stat* %16, i32 0, i32 1
%70 = load i32, i32* %69
%71 = call i32 @chown(i8* %66, i32 %68, i32 %70)
store i32 %71, i32* %13
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %74
73: ; preds = %65
br label %92
74: ; preds = %65
%75 = load i8*, i8** %9
%76 = getelementptr inbounds %struct.stat, %struct.stat* %16, i32 0, i32 0
%77 = load i32, i32* %76
%78 = call i32 @chmod(i8* %75, i32 %77)
store i32 %78, i32* %13
%79 = icmp ne i32 %78, 0
br i1 %79, label %80, label %81
80: ; preds = %74
br label %92
81: ; preds = %74
%82 = load i8*, i8** %9
%83 = load i8*, i8** %8
%84 = call i64 @rename(i8* %82, i8* %83)
%85 = icmp ne i64 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %81
br label %92
87: ; preds = %81
%88 = load i32*, i32** %14
%89 = call i32 @fclose(i32* %88)
%90 = load i32*, i32** %15
%91 = call i32 @fclose(i32* %90)
store i32 0, i32* %5
br label %99
92: ; preds = %86, %80, %73, %64, %56
%93 = load i32*, i32** %14
%94 = call i32 @fclose(i32* %93)
%95 = load i32*, i32** %15
%96 = call i32 @fclose(i32* %95)
%97 = load i32, i32* @errno
%98 = sub nsw i32 0, %97
store i32 %98, i32* %5
br label %99
99: ; preds = %92, %87, %27, %20
%100 = load i32, i32* %5
ret i32 %100
}

LLVM IR (-O2):
%struct.stat = type { i32, i32, i32 }
@errno = global i32 0
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
@EOF = global i64 0
define i32 @cm_delete_entry(i8* %0, i8* %1, i8* nocapture %2, i8* nocapture %3) {
%5 = alloca [2048 x i8]
%6 = alloca [256 x i8]
%7 = alloca %struct.stat
%8 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = bitcast %struct.stat* %7 to i8*
store i32 0, i32* @errno
%11 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
br label %65
16: ; preds = %4
%17 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = call i32* @fgets(i8* %8, i64 2048, i32* %11)
%21 = icmp eq i32* %20, null
br i1 %21, label %39, label %26
22: ; preds = %16
%23 = tail call i32 @fclose(i32* %11)
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
br label %65
26: ; preds = %19, %36
%27 = call i64 @sscanf(i8* %8, i8* %1, i8* %9)
%28 = load i64, i64* @EOF
%29 = icmp eq i64 %27, %28
br i1 %29, label %36, label %30
30: ; preds = %26
%31 = call i64 @strcmp(i8* %0, i8* %9)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %30
%34 = call i32 @fprintf(i32* %17, i8* %8)
%35 = icmp eq i32 %34, -1
br i1 %35, label %60, label %36
36: ; preds = %30, %33, %26
%37 = call i32* @fgets(i8* %8, i64 2048, i32* %11)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %26
39: ; preds = %36, %19
%40 = call i32 @stat(i8* %2, %struct.stat* %7)
%41 = icmp eq i32 %40, -1
br i1 %41, label %60, label %42
42: ; preds = %39
%43 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 2
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 1
%46 = load i32, i32* %45
%47 = call i32 @chown(i8* %3, i32 %44, i32 %46)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %60
49: ; preds = %42
%50 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
%51 = load i32, i32* %50
%52 = call i32 @chmod(i8* %3, i32 %51)
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %49
%55 = call i64 @rename(i8* %3, i8* %2)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54
%58 = call i32 @fclose(i32* %11)
%59 = call i32 @fclose(i32* %17)
br label %65
60: ; preds = %33, %54, %49, %42, %39
%61 = call i32 @fclose(i32* %11)
%62 = call i32 @fclose(i32* %17)
%63 = load i32, i32* @errno
%64 = sub nsw i32 0, %63
br label %65
65: ; preds = %60, %57, %22, %13
%66 = phi i32 [ %15, %13 ], [ %25, %22 ], [ %64, %60 ], [ 0, %57 ]
ret i32 %66
}