#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_replace_entry(char *name, char *pattern, char *line, 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, line) == -1) {
          goto cleanup;
        }
      } else {
        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_replace_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)
mov    %r8,-0x30(%rbp)
movq   $0x800,-0x938(%rbp)
mov    0x2e07(%rip),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x948(%rbp)
cmp    $0x0,%rax
jne    120b <cm_replace_entry+0x6b>
xor    %eax,%eax
mov    0x2dd7(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13de <cm_replace_entry+0x23e>
mov    -0x30(%rbp),%rdi
lea    0xdec(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x950(%rbp)
cmp    $0x0,%rax
jne    124b <cm_replace_entry+0xab>
mov    -0x948(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2d97(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    13de <cm_replace_entry+0x23e>
jmp    1250 <cm_replace_entry+0xb0>
lea    -0x830(%rbp),%rdi
mov    -0x938(%rbp),%rsi
mov    -0x948(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1308 <cm_replace_entry+0x168>
lea    -0x830(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x930(%rbp),%rdx
call   1080 <sscanf@plt>
mov    0x2d56(%rip),%rcx
cmp    (%rcx),%rax
je     1303 <cm_replace_entry+0x163>
mov    -0x10(%rbp),%rdi
lea    -0x930(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    12d8 <cm_replace_entry+0x138>
mov    -0x950(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <fprintf@plt>
cmp    $0xffffffff,%eax
jne    12d3 <cm_replace_entry+0x133>
jmp    13b8 <cm_replace_entry+0x218>
jmp    12fe <cm_replace_entry+0x15e>
mov    -0x950(%rbp),%rdi
lea    -0x830(%rbp),%rsi
call   1060 <fprintf@plt>
cmp    $0xffffffff,%eax
jne    12f9 <cm_replace_entry+0x159>
jmp    13b8 <cm_replace_entry+0x218>
jmp    12fe <cm_replace_entry+0x15e>
jmp    1303 <cm_replace_entry+0x163>
jmp    1250 <cm_replace_entry+0xb0>
mov    -0x28(%rbp),%rdi
lea    -0x960(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x93c(%rbp)
cmp    $0xffffffff,%eax
jne    132c <cm_replace_entry+0x18c>
jmp    13b8 <cm_replace_entry+0x218>
mov    -0x30(%rbp),%rdi
mov    -0x958(%rbp),%esi
mov    -0x95c(%rbp),%edx
call   1090 <chown@plt>
mov    %eax,-0x93c(%rbp)
cmp    $0x0,%eax
je     1355 <cm_replace_entry+0x1b5>
jmp    13b8 <cm_replace_entry+0x218>
mov    -0x30(%rbp),%rdi
mov    -0x960(%rbp),%esi
call   10a0 <chmod@plt>
mov    %eax,-0x93c(%rbp)
cmp    $0x0,%eax
je     1378 <cm_replace_entry+0x1d8>
jmp    13b8 <cm_replace_entry+0x218>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10c0 <rename@plt>
cmp    $0x0,%rax
je     1394 <cm_replace_entry+0x1f4>
jmp    13b8 <cm_replace_entry+0x218>
mov    -0x948(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x950(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    13de <cm_replace_entry+0x23e>
mov    -0x948(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x950(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2bff(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x960,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cm_replace_entry>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x938,%rsp
mov    %r8,%r13
mov    %rcx,%r12
mov    %rdx,0x28(%rsp)
mov    %rsi,%rbp
mov    %rdi,%r15
mov    0x2e0f(%rip),%r14
movl   $0x0,(%r14)
lea    0xe29(%rip),%rsi
mov    %rcx,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     131f <cm_replace_entry+0x17f>
mov    %rax,%rbx
lea    0xe10(%rip),%rsi
mov    %r13,%rdi
call   10b0 <fopen@plt>
mov    %rax,(%rsp)
test   %rax,%rax
je     1317 <cm_replace_entry+0x177>
mov    %r13,0x8(%rsp)
mov    %r12,0x10(%rsp)
lea    0x130(%rsp),%rdi
mov    $0x800,%esi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     12a9 <cm_replace_entry+0x109>
lea    0x130(%rsp),%r14
lea    0x30(%rsp),%r13
mov    0x2da9(%rip),%r12
jmp    127a <cm_replace_entry+0xda>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%rdi
mov    %r14,%rsi
call   1060 <fprintf@plt>
cmp    $0xffffffff,%eax
je     12f9 <cm_replace_entry+0x159>
mov    $0x800,%esi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     12a9 <cm_replace_entry+0x109>
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   1080 <sscanf@plt>
cmp    (%r12),%rax
je     1265 <cm_replace_entry+0xc5>
mov    %r15,%rdi
mov    %r13,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    1250 <cm_replace_entry+0xb0>
mov    (%rsp),%rdi
mov    0x28(%rsp),%rsi
jmp    1257 <cm_replace_entry+0xb7>
lea    0x18(%rsp),%rsi
mov    0x10(%rsp),%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
je     12f9 <cm_replace_entry+0x159>
mov    0x1c(%rsp),%edx
mov    0x20(%rsp),%esi
mov    0x8(%rsp),%rdi
call   1090 <chown@plt>
test   %eax,%eax
jne    12f9 <cm_replace_entry+0x159>
mov    0x18(%rsp),%esi
mov    0x8(%rsp),%rdi
call   10a0 <chmod@plt>
test   %eax,%eax
jne    12f9 <cm_replace_entry+0x159>
mov    0x8(%rsp),%rdi
mov    0x10(%rsp),%rsi
call   10c0 <rename@plt>
test   %rax,%rax
je     1336 <cm_replace_entry+0x196>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2cc5(%rip),%rcx
sub    (%rcx),%eax
jmp    1324 <cm_replace_entry+0x184>
mov    %rbx,%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    %rbx,%rdi
call   1030 <fclose@plt>
mov    (%rsp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1324 <cm_replace_entry+0x184>

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

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