#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 EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, char *, char *,
                           char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
scalar_t__ rename(char *, char *);
char *tmpnam(int /*<<< orphan*/ *);
scalar_t__ unlink(char *);
int add2mtab(char *dev, char *mntpt, char *fstype, char *rwflag) {
  FILE *inpf, *outf;
  char *tmp_fname;
  int c;
  if ((tmp_fname = tmpnam(NULL)) == NULL) {
    perror("Error getting temporary file name");
    exit(1);
  }
  inpf = fopen("/etc/mtab", "r");
  outf = fopen(tmp_fname, "w");
  if (!outf) {
    perror("Can't create temporary file");
    exit(1);
  }
  if (inpf) {
    for (;;) {
      c = fgetc(inpf);
      if (c == EOF)
        break;
      fputc(c, outf);
    }
    fclose(inpf);
  }
  fprintf(outf, "%s %s %s %s\n", dev, mntpt, fstype, rwflag);
  fclose(outf);
  if (inpf && unlink("/etc/mtab") < 0) {
    perror("Can't delete old /etc/mtab file");
    exit(1);
  }
  if (rename(tmp_fname, "/etc/mtab") < 0) {
    perror("Error installing /etc/mtab");
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<add2mtab>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <tmpnam@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11e5 <add2mtab+0x45>
lea    0xe2a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe94(%rip),%rdi
lea    0xe2f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe1d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1231 <add2mtab+0x91>
lea    0xe04(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
je     127e <add2mtab+0xde>
jmp    1241 <add2mtab+0xa1>
mov    -0x28(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2d91(%rip),%rcx
cmp    (%rcx),%eax
jne    1264 <add2mtab+0xc4>
jmp    1275 <add2mtab+0xd5>
mov    -0x3c(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1060 <fputc@plt>
jmp    1241 <add2mtab+0xa1>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
lea    0xda9(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x28(%rbp)
je     12de <add2mtab+0x13e>
lea    0xdc7(%rip),%rdi
call   1030 <unlink@plt>
cmp    $0x0,%rax
jge    12de <add2mtab+0x13e>
lea    0xd80(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%rdi
lea    0xd97(%rip),%rsi
call   10a0 <rename@plt>
cmp    $0x0,%rax
jge    130e <add2mtab+0x16e>
lea    0xd70(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add2mtab>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,0x8(%rsp)
mov    %rdi,%r13
xor    %edi,%edi
call   10c0 <tmpnam@plt>
test   %rax,%rax
je     12da <add2mtab+0x13a>
mov    %rax,%rbx
lea    0xeaa(%rip),%rdi
lea    0xe45(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%r14
lea    0xe38(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     12e3 <add2mtab+0x143>
mov    %rax,%rbp
test   %r14,%r14
je     1290 <add2mtab+0xf0>
mov    %r15,0x10(%rsp)
mov    %r14,%rdi
call   1050 <fgetc@plt>
mov    0x2dcb(%rip),%r15
cmp    (%r15),%eax
je     1247 <add2mtab+0xa7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%edi
mov    %rbp,%rsi
call   1060 <fputc@plt>
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    (%r15),%eax
jne    1230 <add2mtab+0x90>
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xdec(%rip),%rsi
mov    %rbp,%rdi
mov    %r13,%rdx
mov    0x8(%rsp),%rcx
mov    %r12,%r8
mov    0x10(%rsp),%r9
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0xe03(%rip),%rdi
call   1030 <unlink@plt>
test   %rax,%rax
jns    12b5 <add2mtab+0x115>
lea    0xdc1(%rip),%rdi
jmp    12f3 <add2mtab+0x153>
lea    0xdab(%rip),%rsi
mov    %rbp,%rdi
mov    %r13,%rdx
mov    0x8(%rsp),%rcx
mov    %r12,%r8
mov    %r15,%r9
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
lea    0xdc4(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <rename@plt>
test   %rax,%rax
js     12ec <add2mtab+0x14c>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd1f(%rip),%rdi
jmp    12f3 <add2mtab+0x153>
lea    0xd3c(%rip),%rdi
jmp    12f3 <add2mtab+0x153>
lea    0xd7c(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"Error getting temporary file name\00"
@.str.1 = [10 x i8] c"/etc/mtab\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [28 x i8] c"Can't create temporary file\00"
@EOF = global i32 0
@.str.5 = [13 x i8] c"%s %s %s %s\0A\00"
@.str.6 = [32 x i8] c"Can't delete old /etc/mtab file\00"
@.str.7 = [27 x i8] c"Error installing /etc/mtab\00"
define i32 @add2mtab(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%13 = call i8* @tmpnam(i32* null)
store i8* %13, i8** %11
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %4
%16 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %4
%19 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %19, i32** %9
%20 = load i8*, i8** %11
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %21, i32** %10
%22 = load i32*, i32** %10
%23 = icmp ne i32* %22, null
br i1 %23, label %27, label %24
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = load i32*, i32** %9
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %45
30: ; preds = %27
br label %31
31: ; preds = %38, %30
%32 = load i32*, i32** %9
%33 = call i32 @fgetc(i32* %32)
store i32 %33, i32* %12
%34 = load i32, i32* %12
%35 = load i32, i32* @EOF
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %31
br label %42
38: ; preds = %31
%39 = load i32, i32* %12
%40 = load i32*, i32** %10
%41 = call i32 @fputc(i32 %39, i32* %40)
br label %31
42: ; preds = %37
%43 = load i32*, i32** %9
%44 = call i32 @fclose(i32* %43)
br label %45
45: ; preds = %42, %27
%46 = load i32*, i32** %10
%47 = load i8*, i8** %5
%48 = load i8*, i8** %6
%49 = load i8*, i8** %7
%50 = load i8*, i8** %8
%51 = call i32 @fprintf(i32* %46, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %47, i8* %48, i8* %49, i8* %50)
%52 = load i32*, i32** %10
%53 = call i32 @fclose(i32* %52)
%54 = load i32*, i32** %9
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %62
56: ; preds = %45
%57 = call i64 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %56, %45
%63 = load i8*, i8** %11
%64 = call i64 @rename(i8* %63, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %62
%67 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %62
ret i32 0
}

LLVM IR (-O2):
@.str = [34 x i8] c"Error getting temporary file name\00"
@.str.1 = [10 x i8] c"/etc/mtab\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [28 x i8] c"Can't create temporary file\00"
@EOF = global i32 0
@.str.5 = [13 x i8] c"%s %s %s %s\0A\00"
@.str.6 = [32 x i8] c"Can't delete old /etc/mtab file\00"
@.str.7 = [27 x i8] c"Error installing /etc/mtab\00"
define i32 @add2mtab(i8* %0, i8* %1, i8* %2, i8* %3) {
%5 = tail call i8* @tmpnam(i32* null)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %4
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%12 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%16 = tail call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = icmp eq i32* %11, null
br i1 %18, label %38, label %19
19: ; preds = %17
%20 = tail call i32 @fgetc(i32* %11)
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %29, label %23
23: ; preds = %19, %23
%24 = phi i32 [ %26, %23 ], [ %20, %19 ]
%25 = tail call i32 @fputc(i32 %24, i32* %12)
%26 = tail call i32 @fgetc(i32* %11)
%27 = load i32, i32* @EOF
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %23
29: ; preds = %23, %19
%30 = tail call i32 @fclose(i32* %11)
%31 = tail call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %1, i8* %2, i8* %3)
%32 = tail call i32 @fclose(i32* %12)
%33 = tail call i64 @unlink(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %29
%36 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %17
%39 = tail call i32 @fprintf(i32* %12, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %0, i8* %1, i8* %2, i8* %3)
%40 = tail call i32 @fclose(i32* %12)
br label %41
41: ; preds = %38, %29
%42 = tail call i64 @rename(i8* %5, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0))
%46 = tail call i32 @exit(i32 1)
unreachable
47: ; preds = %41
ret i32 0
}