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

/* Variables and functions */
scalar_t__ deleted(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ rename(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strtok(char *, char *);
int rewrite_mtab(void) {
  FILE *inpf, *outf;
  char *tmp_fname;
  static char tmp[160];
  static char tmp2[160];
  char *dev;
  inpf = fopen("/etc/mtab", "r");
  if (!inpf) {
    perror("Can't open /etc/mtab");
    exit(1);
  }
  outf = fopen("/etc/mtab.new", "w");
  if (!outf) {
    perror("Can't create temporary file");
    exit(1);
  }
  while (fgets(tmp, sizeof(tmp), inpf)) {
    strncpy(tmp2, tmp, 160);
    dev = strtok(tmp, " \t");
    if (dev && deleted(dev)) {
      continue;
    } else {
      fputs(tmp2, outf);
    }
  }
  fclose(inpf);
  fclose(outf);
  if (rename("/etc/mtab.new", "/etc/mtab") < 0) {
    perror("Error installing /etc/mtab");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<rewrite_mtab>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xea6(%rip),%rdi
lea    0xe4a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11e0 <rewrite_mtab+0x40>
lea    0xe31(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xe30(%rip),%rdi
lea    0xe35(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1218 <rewrite_mtab+0x78>
lea    0xe1c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    121d <rewrite_mtab+0x7d>
mov    -0x8(%rbp),%rdx
lea    0x2e58(%rip),%rdi
mov    $0xa0,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12a3 <rewrite_mtab+0x103>
lea    0x2edd(%rip),%rdi
lea    0x2e36(%rip),%rsi
mov    $0xa0,%edx
call   1030 <strncpy@plt>
lea    0x2e25(%rip),%rdi
lea    0xddf(%rip),%rsi
call   10a0 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     128e <rewrite_mtab+0xee>
mov    -0x20(%rbp),%rdi
call   1040 <deleted@plt>
cmp    $0x0,%rax
je     128e <rewrite_mtab+0xee>
jmp    121d <rewrite_mtab+0x7d>
mov    -0x10(%rbp),%rsi
lea    0x2e87(%rip),%rdi
call   1060 <fputs@plt>
jmp    121d <rewrite_mtab+0x7d>
mov    -0x8(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xd5b(%rip),%rdi
lea    0xd92(%rip),%rsi
call   10b0 <rename@plt>
cmp    $0x0,%rax
jge    12e8 <rewrite_mtab+0x148>
lea    0xd6b(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rewrite_mtab>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
lea    0xea5(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12a3 <rewrite_mtab+0x103>
mov    %rax,%r12
lea    0xe48(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12ac <rewrite_mtab+0x10c>
mov    %rax,%r14
lea    0x2e92(%rip),%rdi
mov    $0xa0,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     126f <rewrite_mtab+0xcf>
lea    0x2f19(%rip),%r15
lea    0x2e72(%rip),%rbx
lea    0xe2c(%rip),%r13
jmp    1240 <rewrite_mtab+0xa0>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <fputs@plt>
mov    %rbx,%rdi
mov    $0xa0,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     126f <rewrite_mtab+0xcf>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0xa0,%edx
call   1030 <strncpy@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strtok@plt>
test   %rax,%rax
je     1220 <rewrite_mtab+0x80>
mov    %rax,%rdi
call   1040 <deleted@plt>
test   %rax,%rax
jne    122b <rewrite_mtab+0x8b>
jmp    1220 <rewrite_mtab+0x80>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
lea    0xd91(%rip),%rdi
lea    0xdc8(%rip),%rsi
call   10b0 <rename@plt>
test   %rax,%rax
js     12b5 <rewrite_mtab+0x115>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd58(%rip),%rdi
jmp    12bc <rewrite_mtab+0x11c>
lea    0xd72(%rip),%rdi
jmp    12bc <rewrite_mtab+0x11c>
lea    0xd88(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@rewrite_mtab.tmp = internal global [160 x i8] zeroinitializer
@rewrite_mtab.tmp2 = internal global [160 x i8] zeroinitializer
@.str = [10 x i8] c"/etc/mtab\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"Can't open /etc/mtab\00"
@.str.3 = [14 x i8] c"/etc/mtab.new\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [28 x i8] c"Can't create temporary file\00"
@.str.6 = [3 x i8] c" \09\00"
@.str.7 = [27 x i8] c"Error installing /etc/mtab\00"
define i32 @rewrite_mtab() {
%1 = alloca i32*
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8*
%5 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %1
%6 = load i32*, i32** %1
%7 = icmp ne i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %12, i32** %2
%13 = load i32*, i32** %2
%14 = icmp ne i32* %13, null
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11
br label %19
19: ; preds = %36, %32, %18
%20 = load i32*, i32** %1
%21 = call i64 @fgets(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp, i64 0, i64 0), i32 160, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %37
23: ; preds = %19
%24 = call i32 @strncpy(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp2, i64 0, i64 0), i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp, i64 0, i64 0), i32 160)
%25 = call i8* @strtok(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
store i8* %25, i8** %4
%26 = load i8*, i8** %4
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = load i8*, i8** %4
%30 = call i64 @deleted(i8* %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
br label %19
33: ; preds = %28, %23
%34 = load i32*, i32** %2
%35 = call i32 @fputs(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp2, i64 0, i64 0), i32* %34)
br label %36
36: ; preds = %33
br label %19
37: ; preds = %19
%38 = load i32*, i32** %1
%39 = call i32 @fclose(i32* %38)
%40 = load i32*, i32** %2
%41 = call i32 @fclose(i32* %40)
%42 = call i64 @rename(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %37
%45 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %37
ret i32 0
}

LLVM IR (-O2):
@rewrite_mtab.tmp = internal global [160 x i8] zeroinitializer
@rewrite_mtab.tmp2 = internal global [160 x i8] zeroinitializer
@.str = [10 x i8] c"/etc/mtab\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [21 x i8] c"Can't open /etc/mtab\00"
@.str.3 = [14 x i8] c"/etc/mtab.new\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [28 x i8] c"Can't create temporary file\00"
@.str.6 = [3 x i8] c" \09\00"
@.str.7 = [27 x i8] c"Error installing /etc/mtab\00"
define i32 @rewrite_mtab() {
%1 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%2 = icmp eq i32* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %6
%10 = tail call i64 @fgets(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp, i64 0, i64 0), i32 160, i32* %1)
%11 = icmp eq i64 %10, 0
br i1 %11, label %27, label %15
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %9, %24
%16 = tail call i32 @strncpy(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp2, i64 0, i64 0), i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp, i64 0, i64 0), i32 160)
%17 = tail call i8* @strtok(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
%18 = icmp eq i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %15
%20 = tail call i64 @deleted(i8* %17)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19, %15
%23 = tail call i32 @fputs(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp2, i64 0, i64 0), i32* %7)
br label %24
24: ; preds = %22, %19
%25 = tail call i64 @fgets(i8* getelementptr inbounds ([160 x i8], [160 x i8]* @rewrite_mtab.tmp, i64 0, i64 0), i32 160, i32* %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %15
27: ; preds = %24, %9
%28 = tail call i32 @fclose(i32* %1)
%29 = tail call i32 @fclose(i32* %7)
%30 = tail call i64 @rename(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %27
ret i32 0
}