#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 utimbuf {
  int /*<<< orphan*/ modtime;
  int /*<<< orphan*/ actime;
};
struct stat {
  int /*<<< orphan*/ st_mtime;
  int /*<<< orphan*/ st_atime;
};
typedef int mode_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
scalar_t__ fclose(int /*<<< orphan*/ *);
scalar_t__ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
int getc(int /*<<< orphan*/ *);
int putc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int umask(int);
int /*<<< orphan*/ utime(char const *, struct utimbuf *);
int create_backup(const char *backup, FILE *fp) {
  struct stat sb;
  struct utimbuf ub;
  FILE *bkfp;
  int c;
  mode_t mask;
  if (fstat(fileno(fp), &sb))
    return -1;
  mask = umask(077);
  bkfp = fopen(backup, "w");
  umask(mask);
  if (!bkfp)
    return -1;
  rewind(fp);
  while ((c = getc(fp)) != EOF) {
    if (putc(c, bkfp) == EOF)
      break;
  }
  if (c != EOF || fflush(bkfp)) {
    fclose(bkfp);
    return -1;
  }
  if (fclose(bkfp))
    return -1;
  ub.actime = sb.st_atime;
  ub.modtime = sb.st_mtime;
  utime(backup, &ub);
  return 0;
}


x86 assembly (clang, -O0):
<create_backup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,%edi
lea    -0x20(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0x0,%rax
je     11da <create_backup+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <create_backup+0x13c>
mov    $0x3f,%edi
call   1070 <umask@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%edi
call   1070 <umask@plt>
cmpq   $0x0,-0x30(%rbp)
jne    121a <create_backup+0x7a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <create_backup+0x13c>
mov    -0x18(%rbp),%rdi
call   1050 <rewind@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <getc@plt>
mov    %eax,-0x34(%rbp)
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%eax
je     1263 <create_backup+0xc3>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1060 <putc@plt>
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%eax
jne    125e <create_backup+0xbe>
jmp    1263 <create_backup+0xc3>
jmp    1223 <create_backup+0x83>
mov    -0x34(%rbp),%eax
mov    0x2d7b(%rip),%rcx
cmp    (%rcx),%eax
jne    1288 <create_backup+0xe8>
mov    -0x30(%rbp),%rdi
call   1090 <fflush@plt>
cmp    $0x0,%rax
je     129d <create_backup+0xfd>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <create_backup+0x13c>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmp    $0x0,%rax
je     12bc <create_backup+0x11c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <create_backup+0x13c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <utime@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_backup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %rdi,%r14
mov    %rsi,%rdi
call   1080 <fileno@plt>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
jne    126e <create_backup+0xce>
mov    $0x3f,%edi
call   1070 <umask@plt>
mov    %eax,%ebp
lea    0xe16(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r12
mov    %ebp,%edi
call   1070 <umask@plt>
test   %r12,%r12
je     126e <create_backup+0xce>
mov    %r13,%rdi
call   1050 <rewind@plt>
mov    0x2dd8(%rip),%rbx
mov    %r13,%rdi
call   10c0 <getc@plt>
cmp    (%rbx),%eax
je     1230 <create_backup+0x90>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
call   1060 <putc@plt>
cmp    (%rbx),%eax
jne    1210 <create_backup+0x70>
cmp    %eax,%ebp
jne    123d <create_backup+0x9d>
mov    %r12,%rdi
call   1090 <fflush@plt>
test   %rax,%rax
je     1247 <create_backup+0xa7>
mov    %r12,%rdi
call   1040 <fclose@plt>
jmp    126e <create_backup+0xce>
mov    %r12,%rdi
call   1040 <fclose@plt>
test   %rax,%rax
jne    126e <create_backup+0xce>
mov    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1030 <utime@plt>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
%struct.utimbuf = type { i32, i32 }
@.str = [2 x i8] c"w\00"
@EOF = global i32 0
define i32 @create_backup(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca %struct.stat
%7 = alloca %struct.utimbuf
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%11 = load i32*, i32** %5
%12 = call i32 @fileno(i32* %11)
%13 = call i64 @fstat(i32 %12, %struct.stat* %6)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %66
16: ; preds = %2
%17 = call i32 @umask(i32 63)
store i32 %17, i32* %10
%18 = load i8*, i8** %4
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %19, i32** %8
%20 = load i32, i32* %10
%21 = call i32 @umask(i32 %20)
%22 = load i32*, i32** %8
%23 = icmp ne i32* %22, null
br i1 %23, label %25, label %24
24: ; preds = %16
store i32 -1, i32* %3
br label %66
25: ; preds = %16
%26 = load i32*, i32** %5
%27 = call i32 @rewind(i32* %26)
br label %28
28: ; preds = %40, %25
%29 = load i32*, i32** %5
%30 = call i32 @getc(i32* %29)
store i32 %30, i32* %9
%31 = load i32, i32* @EOF
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %41
33: ; preds = %28
%34 = load i32, i32* %9
%35 = load i32*, i32** %8
%36 = call i32 @putc(i32 %34, i32* %35)
%37 = load i32, i32* @EOF
%38 = icmp eq i32 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %33
br label %41
40: ; preds = %33
br label %28
41: ; preds = %39, %28
%42 = load i32, i32* %9
%43 = load i32, i32* @EOF
%44 = icmp ne i32 %42, %43
br i1 %44, label %49, label %45
45: ; preds = %41
%46 = load i32*, i32** %8
%47 = call i64 @fflush(i32* %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %45, %41
%50 = load i32*, i32** %8
%51 = call i64 @fclose(i32* %50)
store i32 -1, i32* %3
br label %66
52: ; preds = %45
%53 = load i32*, i32** %8
%54 = call i64 @fclose(i32* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %52
store i32 -1, i32* %3
br label %66
57: ; preds = %52
%58 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%59 = load i32, i32* %58
%60 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %7, i32 0, i32 1
store i32 %59, i32* %60
%61 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%62 = load i32, i32* %61
%63 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %7, i32 0, i32 0
store i32 %62, i32* %63
%64 = load i8*, i8** %4
%65 = call i32 @utime(i8* %64, %struct.utimbuf* %7)
store i32 0, i32* %3
br label %66
66: ; preds = %57, %56, %49, %24, %15
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
%struct.utimbuf = type { i32, i32 }
@.str = [2 x i8] c"w\00"
@EOF = global i32 0
define i32 @create_backup(i8* nocapture %0, i32* nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.utimbuf
%5 = bitcast %struct.stat* %3 to i8*
%6 = bitcast %struct.utimbuf* %4 to i8*
%7 = tail call i32 @fileno(i32* %1)
%8 = call i64 @fstat(i32 %7, %struct.stat* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %43
10: ; preds = %2
%11 = tail call i32 @umask(i32 63)
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @umask(i32 %11)
%14 = icmp eq i32* %12, null
br i1 %14, label %43, label %15
15: ; preds = %10
%16 = tail call i32 @rewind(i32* %1)
br label %17
17: ; preds = %21, %15
%18 = tail call i32 @getc(i32* %1)
%19 = load i32, i32* @EOF
%20 = icmp eq i32 %18, %19
br i1 %20, label %27, label %21
21: ; preds = %17
%22 = tail call i32 @putc(i32 %18, i32* %12)
%23 = load i32, i32* @EOF
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %17
25: ; preds = %21
%26 = icmp eq i32 %18, %22
br i1 %26, label %27, label %30
27: ; preds = %17, %25
%28 = tail call i64 @fflush(i32* %12)
%29 = icmp eq i64 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %27, %25
%31 = tail call i64 @fclose(i32* %12)
br label %43
32: ; preds = %27
%33 = tail call i64 @fclose(i32* %12)
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %4, i64 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.utimbuf, %struct.utimbuf* %4, i64 0, i32 0
store i32 %40, i32* %41
%42 = call i32 @utime(i8* %0, %struct.utimbuf* %4)
br label %43
43: ; preds = %32, %10, %2, %35, %30
%44 = phi i32 [ -1, %30 ], [ 0, %35 ], [ -1, %2 ], [ -1, %10 ], [ -1, %32 ]
ret i32 %44
}