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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *,
                           ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ perror(char const *);
scalar_t__ remove(char const *);
scalar_t__ rename(char *, char const *);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
char *strerror(int /*<<< orphan*/);
size_t strlen(char const *);
char *get_passphrase(const char *fname) {
  char *passphrase = NULL;
  size_t fname_len;
  char *fname_new;
  FILE *source, *sink;
  char linebuf[80];
  fname_len = strlen(fname);
  fname_new = malloc(fname_len + 5);
  if (fname_new == NULL) {
    perror("malloc");
    exit(1);
  }
  snprintf(fname_new, fname_len + 5, "%s.new", fname);
  source = fopen(fname, "r");
  if (!source) {
    perror(fname);
    exit(1);
  }
  sink = fopen(fname_new, "w");
  if (!sink) {
    perror(fname_new);
    exit(1);
  }
  while (fgets(linebuf, sizeof linebuf, source)) {
    linebuf[sizeof linebuf - 1] = 0;
    if (passphrase == NULL) {
      passphrase = strdup(linebuf);
      if (passphrase == NULL) {
        perror("strdup");
        exit(1);
      }
    } else
      fputs(linebuf, sink);
  }
  if (ferror(source)) {
    perror(fname);
    exit(1);
  }
  if (ferror(sink)) {
    perror(fname_new);
    exit(1);
  }
  fclose(source);
  fclose(sink);
  if (remove(fname)) {
    fprintf(stderr, "Failed to remove %s: %s", fname, strerror(errno));
    exit(1);
  }
  if (rename(fname_new, fname)) {
    fprintf(stderr, "Failed to rename %s to %s: %s", fname, fname_new,
            strerror(errno));
    exit(1);
  }
  free(fname_new);
  return passphrase;
}


x86 assembly (clang, -O0):
<get_passphrase>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
add    $0x5,%rdi
call   10c0 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1256 <get_passphrase+0x56>
lea    0xdb9(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x5,%rsi
mov    -0x8(%rbp),%rcx
lea    0xd9a(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x8(%rbp),%rdi
lea    0xd91(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12a4 <get_passphrase+0xa4>
mov    -0x8(%rbp),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xd5d(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12d6 <get_passphrase+0xd6>
mov    -0x20(%rbp),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
jmp    12db <get_passphrase+0xdb>
lea    -0x80(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x50,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%rax
je     134b <get_passphrase+0x14b>
movb   $0x0,-0x31(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1339 <get_passphrase+0x139>
lea    -0x80(%rbp),%rdi
call   1110 <strdup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1334 <get_passphrase+0x134>
lea    0xceb(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
jmp    1346 <get_passphrase+0x146>
lea    -0x80(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1090 <fputs@plt>
jmp    12db <get_passphrase+0xdb>
mov    -0x28(%rbp),%rdi
call   1050 <ferror@plt>
cmp    $0x0,%rax
je     1371 <get_passphrase+0x171>
mov    -0x8(%rbp),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1050 <ferror@plt>
cmp    $0x0,%rax
je     1397 <get_passphrase+0x197>
mov    -0x20(%rbp),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x8(%rbp),%rdi
call   1040 <remove@plt>
cmp    $0x0,%rax
je     140c <get_passphrase+0x20c>
mov    0x2c1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    0x2bfb(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    -0x8c(%rbp),%edi
mov    -0x88(%rbp),%rdx
mov    %rax,%rcx
lea    0xc1c(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10f0 <rename@plt>
cmp    $0x0,%rax
je     1485 <get_passphrase+0x285>
mov    0x2bb6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa4(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    0x2b89(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
mov    -0xa4(%rbp),%edi
mov    -0xa0(%rbp),%rdx
mov    -0x98(%rbp),%rcx
mov    %rax,%r8
lea    0xbbb(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_passphrase>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%r14
call   1070 <strlen@plt>
mov    %rax,%rbx
add    $0x5,%rbx
mov    %rbx,%rdi
call   10c0 <malloc@plt>
test   %rax,%rax
je     134b <get_passphrase+0x14b>
mov    %rax,%r15
lea    0xdcf(%rip),%rdx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rcx
call   1080 <snprintf@plt>
lea    0xdc1(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1341 <get_passphrase+0x141>
mov    %rax,%r12
lea    0xda4(%rip),%rsi
mov    %r15,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     1346 <get_passphrase+0x146>
mov    %rax,%r13
mov    %rsp,%rdi
mov    $0x50,%esi
mov    %r12,%rdx
call   10a0 <fgets@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     12e0 <get_passphrase+0xe0>
mov    %rsp,%rbp
jmp    12bd <get_passphrase+0xbd>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rsi
call   1090 <fputs@plt>
mov    %rbp,%rdi
mov    $0x50,%esi
mov    %r12,%rdx
call   10a0 <fgets@plt>
test   %rax,%rax
je     12e0 <get_passphrase+0xe0>
movb   $0x0,0x4f(%rsp)
mov    %rbp,%rdi
test   %rbx,%rbx
jne    12a0 <get_passphrase+0xa0>
call   1110 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12a8 <get_passphrase+0xa8>
lea    0xd32(%rip),%rdi
jmp    1352 <get_passphrase+0x152>
mov    %r12,%rdi
call   1050 <ferror@plt>
test   %rax,%rax
jne    1341 <get_passphrase+0x141>
mov    %r13,%rdi
call   1050 <ferror@plt>
test   %rax,%rax
jne    1346 <get_passphrase+0x146>
mov    %r12,%rdi
call   1060 <fclose@plt>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1040 <remove@plt>
test   %rax,%rax
jne    1361 <get_passphrase+0x161>
mov    %r15,%rdi
mov    %r14,%rsi
call   10f0 <rename@plt>
test   %rax,%rax
jne    1398 <get_passphrase+0x198>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
jmp    1352 <get_passphrase+0x152>
mov    %r15,%rdi
jmp    1352 <get_passphrase+0x152>
lea    0xcae(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2c78(%rip),%rax
mov    (%rax),%ebx
mov    0x2c67(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc98(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2c41(%rip),%rax
mov    (%rax),%ebx
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc79(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [7 x i8] c"%s.new\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [7 x i8] c"strdup\00"
@stderr = global i32 0
@.str.5 = [24 x i8] c"Failed to remove %s: %s\00"
@errno = global i32 0
@.str.6 = [30 x i8] c"Failed to rename %s to %s: %s\00"
define i8* @get_passphrase(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [80 x i8]
store i8* %0, i8** %2
store i8* null, i8** %3
%9 = load i8*, i8** %2
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %4
%11 = load i64, i64* %4
%12 = add i64 %11, 5
%13 = call i8* @malloc(i64 %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %1
%20 = load i8*, i8** %5
%21 = load i64, i64* %4
%22 = add i64 %21, 5
%23 = load i8*, i8** %2
%24 = call i32 @snprintf(i8* %20, i64 %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load i8*, i8** %2
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %6
%27 = load i32*, i32** %6
%28 = icmp ne i32* %27, null
br i1 %28, label %33, label %29
29: ; preds = %19
%30 = load i8*, i8** %2
%31 = call i32 @perror(i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %19
%34 = load i8*, i8** %5
%35 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %35, i32** %7
%36 = load i32*, i32** %7
%37 = icmp ne i32* %36, null
br i1 %37, label %42, label %38
38: ; preds = %33
%39 = load i8*, i8** %5
%40 = call i32 @perror(i8* %39)
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %33
br label %43
43: ; preds = %65, %42
%44 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%45 = load i32*, i32** %6
%46 = call i64 @fgets(i8* %44, i32 80, i32* %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %66
48: ; preds = %43
%49 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 79
store i8 0, i8* %49
%50 = load i8*, i8** %3
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %61
52: ; preds = %48
%53 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%54 = call i8* @strdup(i8* %53)
store i8* %54, i8** %3
%55 = load i8*, i8** %3
%56 = icmp eq i8* %55, null
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %52
br label %65
61: ; preds = %48
%62 = getelementptr inbounds [80 x i8], [80 x i8]* %8, i64 0, i64 0
%63 = load i32*, i32** %7
%64 = call i32 @fputs(i8* %62, i32* %63)
br label %65
65: ; preds = %61, %60
br label %43
66: ; preds = %43
%67 = load i32*, i32** %6
%68 = call i64 @ferror(i32* %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load i8*, i8** %2
%72 = call i32 @perror(i8* %71)
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %66
%75 = load i32*, i32** %7
%76 = call i64 @ferror(i32* %75)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %82
78: ; preds = %74
%79 = load i8*, i8** %5
%80 = call i32 @perror(i8* %79)
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %74
%83 = load i32*, i32** %6
%84 = call i32 @fclose(i32* %83)
%85 = load i32*, i32** %7
%86 = call i32 @fclose(i32* %85)
%87 = load i8*, i8** %2
%88 = call i64 @remove(i8* %87)
%89 = icmp ne i64 %88, 0
br i1 %89, label %90, label %97
90: ; preds = %82
%91 = load i32, i32* @stderr
%92 = load i8*, i8** %2
%93 = load i32, i32* @errno
%94 = call i8* @strerror(i32 %93)
%95 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %91, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %92, i8* %94)
%96 = call i32 @exit(i32 1)
unreachable
97: ; preds = %82
%98 = load i8*, i8** %5
%99 = load i8*, i8** %2
%100 = call i64 @rename(i8* %98, i8* %99)
%101 = icmp ne i64 %100, 0
br i1 %101, label %102, label %110
102: ; preds = %97
%103 = load i32, i32* @stderr
%104 = load i8*, i8** %2
%105 = load i8*, i8** %5
%106 = load i32, i32* @errno
%107 = call i8* @strerror(i32 %106)
%108 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %103, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %104, i8* %105, i8* %107)
%109 = call i32 @exit(i32 1)
unreachable
110: ; preds = %97
%111 = load i8*, i8** %5
%112 = call i32 @free(i8* %111)
%113 = load i8*, i8** %3
ret i8* %113
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [7 x i8] c"%s.new\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [7 x i8] c"strdup\00"
@stderr = global i32 0
@.str.5 = [24 x i8] c"Failed to remove %s: %s\00"
@errno = global i32 0
@.str.6 = [30 x i8] c"Failed to rename %s to %s: %s\00"
define i8* @get_passphrase(i8* %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add i64 %4, 5
%6 = tail call i8* @malloc(i64 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = tail call i32 @snprintf(i8* %6, i64 %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%13 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = tail call i32 @perror(i8* %0)
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %11
%19 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %26, label %21
21: ; preds = %18
%22 = call i64 @fgets(i8* %3, i32 80, i32* %13)
%23 = icmp eq i64 %22, 0
br i1 %23, label %44, label %24
24: ; preds = %21
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 79
br label %29
26: ; preds = %18
%27 = tail call i32 @perror(i8* %6)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %24, %40
%30 = phi i8* [ null, %24 ], [ %41, %40 ]
store i8 0, i8* %25
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = call i8* @strdup(i8* %3)
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %40
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %29
%39 = call i32 @fputs(i8* %3, i32* %19)
br label %40
40: ; preds = %32, %38
%41 = phi i8* [ %33, %32 ], [ %30, %38 ]
%42 = call i64 @fgets(i8* %3, i32 80, i32* %13)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %29
44: ; preds = %40, %21
%45 = phi i8* [ null, %21 ], [ %41, %40 ]
%46 = call i64 @ferror(i32* %13)
%47 = icmp eq i64 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %44
%49 = call i32 @perror(i8* %0)
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %44
%52 = call i64 @ferror(i32* %19)
%53 = icmp eq i64 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %51
%55 = call i32 @perror(i8* %6)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %51
%58 = call i32 @fclose(i32* %13)
%59 = call i32 @fclose(i32* %19)
%60 = call i64 @remove(i8* %0)
%61 = icmp eq i64 %60, 0
br i1 %61, label %68, label %62
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = load i32, i32* @errno
%65 = call i8* @strerror(i32 %64)
%66 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %65)
%67 = call i32 @exit(i32 1)
unreachable
68: ; preds = %57
%69 = call i64 @rename(i8* %6, i8* %0)
%70 = icmp eq i64 %69, 0
br i1 %70, label %77, label %71
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i8* %0, i8* %6, i8* %74)
%76 = call i32 @exit(i32 1)
unreachable
77: ; preds = %68
%78 = call i32 @free(i8* %6)
ret i8* %45
}