#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 confctl {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int asprintf(char **, char *, char const *);
int /*<<< orphan*/ confctl_write(struct confctl *, int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, char *, ...);
int fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int fflush(int /*<<< orphan*/ *);
int fsync(int);
int mkstemp(char *);
int /*<<< orphan*/ remove_tmpfile(char *);
int rename(char *, char const *);
void confctl_save_atomic(struct confctl *cc, const char *path) {
  FILE *fp;
  int error, fd, written;
  char *tmppath = NULL;
  written = asprintf(&tmppath, "%s.XXXXXXXXX", path);
  if (written < 0)
    err(1, "asprintf");
  fd = mkstemp(tmppath);
  if (fd < 0)
    err(1, "cannot create temporary file %s; use -I to rewrite file in place",
        tmppath);
  fp = fdopen(fd, "w");
  if (fp == NULL) {
    remove_tmpfile(tmppath);
    err(1, "fdopen");
  }
  confctl_write(cc, fp);
  error = fflush(fp);
  if (error != 0) {
    remove_tmpfile(tmppath);
    err(1, "fflush");
  }
  error = fsync(fd);
  if (error != 0) {
    remove_tmpfile(tmppath);
    err(1, "fsync");
  }
  error = fclose(fp);
  if (error != 0) {
    remove_tmpfile(tmppath);
    err(1, "fclose");
  }
  error = rename(tmppath, path);
  if (error != 0) {
    remove_tmpfile(tmppath);
    err(1, "cannot replace %s; use -I to rewrite file in place", path);
  }
}


x86 assembly (clang, -O0):
<confctl_save_atomic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x30(%rbp),%rdi
lea    0xe39(%rip),%rsi
call   1080 <asprintf@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ec <confctl_save_atomic+0x4c>
mov    $0x1,%edi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0x30(%rbp),%rdi
call   1060 <mkstemp@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1219 <confctl_save_atomic+0x79>
mov    -0x30(%rbp),%rdx
mov    $0x1,%edi
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0x20(%rbp),%edi
lea    0xe34(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1253 <confctl_save_atomic+0xb3>
mov    -0x30(%rbp),%rdi
call   10c0 <remove_tmpfile@plt>
mov    $0x1,%edi
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   10a0 <confctl_write@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fflush@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1292 <confctl_save_atomic+0xf2>
mov    -0x30(%rbp),%rdi
call   10c0 <remove_tmpfile@plt>
mov    $0x1,%edi
lea    0xdd5(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0x20(%rbp),%edi
call   1090 <fsync@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12c3 <confctl_save_atomic+0x123>
mov    -0x30(%rbp),%rdi
call   10c0 <remove_tmpfile@plt>
mov    $0x1,%edi
lea    0xdab(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12f5 <confctl_save_atomic+0x155>
mov    -0x30(%rbp),%rdi
call   10c0 <remove_tmpfile@plt>
mov    $0x1,%edi
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <rename@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     132f <confctl_save_atomic+0x18f>
mov    -0x30(%rbp),%rdi
call   10c0 <remove_tmpfile@plt>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xd4c(%rip),%rsi
mov    $0x0,%al
call   1040 <err@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<confctl_save_atomic>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
movq   $0x0,(%rsp)
lea    0xe44(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
call   1080 <asprintf@plt>
test   %eax,%eax
jns    11de <confctl_save_atomic+0x3e>
lea    0xe3b(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <err@plt>
mov    (%rsp),%rdi
call   1060 <mkstemp@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1204 <confctl_save_atomic+0x64>
mov    (%rsp),%rdx
lea    0xe1e(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <err@plt>
lea    0xe4c(%rip),%rsi
mov    %ebp,%edi
call   1070 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1236 <confctl_save_atomic+0x96>
mov    (%rsp),%rdi
call   10c0 <remove_tmpfile@plt>
lea    0xe2f(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <err@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   10a0 <confctl_write@plt>
mov    %rbx,%rdi
call   1050 <fflush@plt>
test   %eax,%eax
je     1269 <confctl_save_atomic+0xc9>
mov    (%rsp),%rdi
call   10c0 <remove_tmpfile@plt>
lea    0xe03(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <err@plt>
mov    %ebp,%edi
call   1090 <fsync@plt>
test   %eax,%eax
je     1290 <confctl_save_atomic+0xf0>
mov    (%rsp),%rdi
call   10c0 <remove_tmpfile@plt>
lea    0xde3(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <err@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %eax,%eax
je     12b8 <confctl_save_atomic+0x118>
mov    (%rsp),%rdi
call   10c0 <remove_tmpfile@plt>
lea    0xdc1(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1040 <err@plt>
mov    (%rsp),%rdi
mov    %r14,%rsi
call   10b0 <rename@plt>
test   %eax,%eax
je     12e7 <confctl_save_atomic+0x147>
mov    (%rsp),%rdi
call   10c0 <remove_tmpfile@plt>
lea    0xd9c(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <err@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.confctl = type { i32 }
@.str = [13 x i8] c"%s.XXXXXXXXX\00"
@.str.1 = [9 x i8] c"asprintf\00"
@.str.2 = [65 x i8] c"cannot create temporary file %s; use -I to rewrite file in place\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [7 x i8] c"fdopen\00"
@.str.5 = [7 x i8] c"fflush\00"
@.str.6 = [6 x i8] c"fsync\00"
@.str.7 = [7 x i8] c"fclose\00"
@.str.8 = [51 x i8] c"cannot replace %s; use -I to rewrite file in place\00"
define void @confctl_save_atomic(%struct.confctl* %0, i8* %1) {
%3 = alloca %struct.confctl*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store %struct.confctl* %0, %struct.confctl** %3
store i8* %1, i8** %4
store i8* null, i8** %9
%10 = load i8*, i8** %4
%11 = call i32 @asprintf(i8** %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = load i8*, i8** %9
%18 = call i32 @mkstemp(i8* %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i8*, i8** %9
%23 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %16
%25 = load i32, i32* %7
%26 = call i32* @fdopen(i32 %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %26, i32** %5
%27 = load i32*, i32** %5
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i8*, i8** %9
%31 = call i32 @remove_tmpfile(i8* %30)
%32 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %29, %24
%34 = load %struct.confctl*, %struct.confctl** %3
%35 = load i32*, i32** %5
%36 = call i32 @confctl_write(%struct.confctl* %34, i32* %35)
%37 = load i32*, i32** %5
%38 = call i32 @fflush(i32* %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %33
%42 = load i8*, i8** %9
%43 = call i32 @remove_tmpfile(i8* %42)
%44 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %45
45: ; preds = %41, %33
%46 = load i32, i32* %7
%47 = call i32 @fsync(i32 %46)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = load i8*, i8** %9
%52 = call i32 @remove_tmpfile(i8* %51)
%53 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %54
54: ; preds = %50, %45
%55 = load i32*, i32** %5
%56 = call i32 @fclose(i32* %55)
store i32 %56, i32* %6
%57 = load i32, i32* %6
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %54
%60 = load i8*, i8** %9
%61 = call i32 @remove_tmpfile(i8* %60)
%62 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %63
63: ; preds = %59, %54
%64 = load i8*, i8** %9
%65 = load i8*, i8** %4
%66 = call i32 @rename(i8* %64, i8* %65)
store i32 %66, i32* %6
%67 = load i32, i32* %6
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %63
%70 = load i8*, i8** %9
%71 = call i32 @remove_tmpfile(i8* %70)
%72 = load i8*, i8** %4
%73 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %69, %63
ret void
}

LLVM IR (-O2):
%struct.confctl = type { i32 }
@.str = [13 x i8] c"%s.XXXXXXXXX\00"
@.str.1 = [9 x i8] c"asprintf\00"
@.str.2 = [65 x i8] c"cannot create temporary file %s; use -I to rewrite file in place\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [7 x i8] c"fdopen\00"
@.str.5 = [7 x i8] c"fflush\00"
@.str.6 = [6 x i8] c"fsync\00"
@.str.7 = [7 x i8] c"fclose\00"
@.str.8 = [51 x i8] c"cannot replace %s; use -I to rewrite file in place\00"
define void @confctl_save_atomic(%struct.confctl* %0, i8* %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%5 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %1)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %9
9: ; preds = %7, %2
%10 = load i8*, i8** %3
%11 = call i32 @mkstemp(i8* %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i8*, i8** %3
%15 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.2, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %9
%17 = call i32* @fdopen(i32 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = call i32 @remove_tmpfile(i8* %20)
%22 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %23
23: ; preds = %19, %16
%24 = call i32 @confctl_write(%struct.confctl* %0, i32* %17)
%25 = call i32 @fflush(i32* %17)
%26 = icmp eq i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = call i32 @remove_tmpfile(i8* %28)
%30 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %27, %23
%32 = call i32 @fsync(i32 %11)
%33 = icmp eq i32 %32, 0
br i1 %33, label %38, label %34
34: ; preds = %31
%35 = load i8*, i8** %3
%36 = call i32 @remove_tmpfile(i8* %35)
%37 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %34, %31
%39 = call i32 @fclose(i32* %17)
%40 = icmp eq i32 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %38
%42 = load i8*, i8** %3
%43 = call i32 @remove_tmpfile(i8* %42)
%44 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
br label %45
45: ; preds = %41, %38
%46 = load i8*, i8** %3
%47 = call i32 @rename(i8* %46, i8* %1)
%48 = icmp eq i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %45
%50 = load i8*, i8** %3
%51 = call i32 @remove_tmpfile(i8* %50)
%52 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.8, i64 0, i64 0), i8* %1)
br label %53
53: ; preds = %49, %45
ret void
}