#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 /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
int /*<<< orphan*/ confctl_write(struct confctl *, int /*<<< orphan*/ *);
int /*<<< orphan*/ err(int, char *, ...);
int fclose(int /*<<< orphan*/ *);
int fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int flock(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char const *, char *);
int fsync(int /*<<< orphan*/);
void confctl_save_in_place(struct confctl *cc, const char *path) {
  FILE *fp;
  int error;
  fp = fopen(path, "w");
  if (fp == NULL)
    err(1, "cannot open %s", path);
  error = flock(fileno(fp), LOCK_EX);
  if (error != 0)
    err(1, "unable to lock %s", path);
  confctl_write(cc, fp);
  error = fflush(fp);
  if (error != 0)
    err(1, "fflush");
  error = fsync(fileno(fp));
  if (error != 0)
    err(1, "fsync");
  error = flock(fileno(fp), LOCK_UN);
  if (error != 0)
    err(1, "unable to unlock %s", path);
  error = fclose(fp);
  if (error != 0)
    err(1, "fclose");
}


x86 assembly (clang, -O0):
<confctl_save_in_place>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c6 <confctl_save_in_place+0x46>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe43(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   10a0 <flock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1203 <confctl_save_in_place+0x83>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <confctl_write@plt>
mov    -0x18(%rbp),%rdi
call   1060 <fflush@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1239 <confctl_save_in_place+0xb9>
mov    $0x1,%edi
lea    0xdf1(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
call   1070 <fsync@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1269 <confctl_save_in_place+0xe9>
mov    $0x1,%edi
lea    0xdc8(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
mov    0x2d5d(%rip),%rax
mov    (%rax),%esi
call   10a0 <flock@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12a6 <confctl_save_in_place+0x126>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     12cf <confctl_save_in_place+0x14f>
mov    $0x1,%edi
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1050 <err@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<confctl_save_in_place>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe6e(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11b8 <confctl_save_in_place+0x38>
lea    0xe59(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <err@plt>
mov    %rbx,%rdi
call   1040 <fileno@plt>
mov    0x2e21(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   10a0 <flock@plt>
test   %eax,%eax
je     11ea <confctl_save_in_place+0x6a>
lea    0xe36(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <err@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <confctl_write@plt>
mov    %rbx,%rdi
call   1060 <fflush@plt>
test   %eax,%eax
je     1214 <confctl_save_in_place+0x94>
lea    0xe1b(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <err@plt>
mov    %rbx,%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
call   1070 <fsync@plt>
test   %eax,%eax
je     123a <confctl_save_in_place+0xba>
lea    0xdfc(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <err@plt>
mov    %rbx,%rdi
call   1040 <fileno@plt>
mov    0x2d8f(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   10a0 <flock@plt>
test   %eax,%eax
je     126c <confctl_save_in_place+0xec>
lea    0xdd3(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <err@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %eax,%eax
je     1290 <confctl_save_in_place+0x110>
lea    0xdc5(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <err@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.confctl = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [15 x i8] c"cannot open %s\00"
@LOCK_EX = global i32 0
@.str.2 = [18 x i8] c"unable to lock %s\00"
@.str.3 = [7 x i8] c"fflush\00"
@.str.4 = [6 x i8] c"fsync\00"
@LOCK_UN = global i32 0
@.str.5 = [20 x i8] c"unable to unlock %s\00"
@.str.6 = [7 x i8] c"fclose\00"
define void @confctl_save_in_place(%struct.confctl* %0, i8* %1) {
%3 = alloca %struct.confctl*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
store %struct.confctl* %0, %struct.confctl** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %5
%9 = load i32*, i32** %5
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %14
14: ; preds = %11, %2
%15 = load i32*, i32** %5
%16 = call i32 @fileno(i32* %15)
%17 = load i32, i32* @LOCK_EX
%18 = call i32 @flock(i32 %16, i32 %17)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i8*, i8** %4
%23 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %14
%25 = load %struct.confctl*, %struct.confctl** %3
%26 = load i32*, i32** %5
%27 = call i32 @confctl_write(%struct.confctl* %25, i32* %26)
%28 = load i32*, i32** %5
%29 = call i32 @fflush(i32* %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %32, %24
%35 = load i32*, i32** %5
%36 = call i32 @fileno(i32* %35)
%37 = call i32 @fsync(i32 %36)
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %34
%43 = load i32*, i32** %5
%44 = call i32 @fileno(i32* %43)
%45 = load i32, i32* @LOCK_UN
%46 = call i32 @flock(i32 %44, i32 %45)
store i32 %46, i32* %6
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = load i8*, i8** %4
%51 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %42
%53 = load i32*, i32** %5
%54 = call i32 @fclose(i32* %53)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %52
%58 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %57, %52
ret void
}

LLVM IR (-O2):
%struct.confctl = type { i32 }
@.str = [2 x i8] c"w\00"
@.str.1 = [15 x i8] c"cannot open %s\00"
@LOCK_EX = global i32 0
@.str.2 = [18 x i8] c"unable to lock %s\00"
@.str.3 = [7 x i8] c"fflush\00"
@.str.4 = [6 x i8] c"fsync\00"
@LOCK_UN = global i32 0
@.str.5 = [20 x i8] c"unable to unlock %s\00"
@.str.6 = [7 x i8] c"fclose\00"
define void @confctl_save_in_place(%struct.confctl* %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %7
7: ; preds = %5, %2
%8 = tail call i32 @fileno(i32* %3)
%9 = load i32, i32* @LOCK_EX
%10 = tail call i32 @flock(i32 %8, i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %7
%13 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %14
14: ; preds = %12, %7
%15 = tail call i32 @confctl_write(%struct.confctl* %0, i32* %3)
%16 = tail call i32 @fflush(i32* %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = tail call i32 @fileno(i32* %3)
%22 = tail call i32 @fsync(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = tail call i32 @fileno(i32* %3)
%28 = load i32, i32* @LOCK_UN
%29 = tail call i32 @flock(i32 %27, i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %1)
br label %33
33: ; preds = %31, %26
%34 = tail call i32 @fclose(i32* %3)
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
ret void
}