#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 mntent {
  int mnt_freq;
  int mnt_passno;
  void *mnt_dir;
  void *mnt_fsname;
  void *mnt_opts;
  void *mnt_type;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int addmntent(int /*<<< orphan*/ *, struct mntent *);
void *canonicalize_path(char const *);
int /*<<< orphan*/ endmntent(int /*<<< orphan*/ *);
int errno;
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ lock_mtab();
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ *setmntent(char *, char *);
void *strdup(char const *);
char *strerror(int);
int /*<<< orphan*/ unlock_mtab();
void update_mtab_entry(const char *spec, const char *node, const char *type,
                       const char *opts, int flags, int freq, int pass) {
  struct mntent mnt;
  if (!opts)
    opts = "rw";
  mnt.mnt_fsname = strdup(spec);
  mnt.mnt_dir = canonicalize_path(node);
  mnt.mnt_type = strdup(type);
  mnt.mnt_opts = strdup(opts);
  mnt.mnt_freq = freq;
  mnt.mnt_passno = pass;
  FILE *fp;
  lock_mtab();
  fp = setmntent("/etc/mtab", "a+");
  if (fp == NULL) {
    int errsv = errno;
    printf("mount: can't open %s: %s", "/etc/mtab", strerror(errsv));
  } else {
    if ((addmntent(fp, &mnt)) == 1) {
      int errsv = errno;
      printf("mount: error writing %s: %s", "/etc/mtab", strerror(errsv));
    }
  }
  endmntent(fp);
  unlock_mtab();
  free(mnt.mnt_fsname);
  free(mnt.mnt_dir);
}


x86 assembly (clang, -O0):
<update_mtab_entry>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d9 <update_mtab_entry+0x39>
lea    0xe2b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <canonicalize_path@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x10(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    $0x0,%al
call   10a0 <lock_mtab@plt>
lea    0xddc(%rip),%rdi
lea    0xddf(%rip),%rsi
call   1080 <setmntent@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    1271 <update_mtab_entry+0xd1>
mov    0x2d97(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdx
lea    0xdb0(%rip),%rdi
lea    0xd9c(%rip),%rsi
call   1050 <printf@plt>
jmp    12b6 <update_mtab_entry+0x116>
mov    -0x58(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1060 <addmntent@plt>
cmp    $0x1,%eax
jne    12b1 <update_mtab_entry+0x111>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    -0x60(%rbp),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdx
lea    0xd84(%rip),%rdi
lea    0xd57(%rip),%rsi
call   1050 <printf@plt>
jmp    12b6 <update_mtab_entry+0x116>
mov    -0x58(%rbp),%rdi
call   1040 <endmntent@plt>
mov    $0x0,%al
call   1070 <unlock_mtab@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_mtab_entry>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %r9d,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    0x60(%rsp),%r12d
test   %rcx,%rcx
lea    0xe3c(%rip),%rbx
cmovne %rcx,%rbx
call   10b0 <strdup@plt>
mov    %rax,0x18(%rsp)
mov    %rbp,%rdi
call   1090 <canonicalize_path@plt>
mov    %rax,0x10(%rsp)
mov    %r15,%rdi
call   10b0 <strdup@plt>
mov    %rax,0x28(%rsp)
mov    %rbx,%rdi
call   10b0 <strdup@plt>
mov    %rax,0x20(%rsp)
mov    %r14d,0x8(%rsp)
mov    %r12d,0xc(%rsp)
xor    %eax,%eax
call   10a0 <lock_mtab@plt>
lea    0xdf2(%rip),%rdi
lea    0xdf5(%rip),%rsi
call   1080 <setmntent@plt>
mov    %rax,%rbx
test   %rax,%rax
je     124e <update_mtab_entry+0xae>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <addmntent@plt>
cmp    $0x1,%eax
jne    1272 <update_mtab_entry+0xd2>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xddd(%rip),%rdi
jmp    1263 <update_mtab_entry+0xc3>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xdad(%rip),%rdi
lea    0xd99(%rip),%rsi
mov    %rax,%rdx
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <endmntent@plt>
xor    %eax,%eax
call   1070 <unlock_mtab@plt>
mov    0x18(%rsp),%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mntent = type { i32, i32, i8*, i8*, i8*, i8* }
@.str = [3 x i8] c"rw\00"
@.str.1 = [10 x i8] c"/etc/mtab\00"
@.str.2 = [3 x i8] c"a+\00"
@errno = global i32 0
@.str.3 = [25 x i8] c"mount: can't open %s: %s\00"
@.str.4 = [28 x i8] c"mount: error writing %s: %s\00"
define void @update_mtab_entry(i8* %0, i8* %1, i8* %2, i8* %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.mntent
%16 = alloca i32*
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i8* %3, i8** %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 %6, i32* %14
%19 = load i8*, i8** %11
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %7
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8** %11
br label %22
22: ; preds = %21, %7
%23 = load i8*, i8** %8
%24 = call i8* @strdup(i8* %23)
%25 = getelementptr inbounds %struct.mntent, %struct.mntent* %15, i32 0, i32 3
store i8* %24, i8** %25
%26 = load i8*, i8** %9
%27 = call i8* @canonicalize_path(i8* %26)
%28 = getelementptr inbounds %struct.mntent, %struct.mntent* %15, i32 0, i32 2
store i8* %27, i8** %28
%29 = load i8*, i8** %10
%30 = call i8* @strdup(i8* %29)
%31 = getelementptr inbounds %struct.mntent, %struct.mntent* %15, i32 0, i32 5
store i8* %30, i8** %31
%32 = load i8*, i8** %11
%33 = call i8* @strdup(i8* %32)
%34 = getelementptr inbounds %struct.mntent, %struct.mntent* %15, i32 0, i32 4
store i8* %33, i8** %34
%35 = load i32, i32* %13
%36 = getelementptr inbounds %struct.mntent, %struct.mntent* %15, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* %14
%38 = getelementptr inbounds %struct.mntent, %struct.mntent* %15, i32 0, i32 1
store i32 %37, i32* %38
%39 = call i32 (...) @lock_mtab()
%40 = call i32* @setmntent(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %40, i32** %16
%41 = load i32*, i32** %16
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %48
43: ; preds = %22
%44 = load i32, i32* @errno
store i32 %44, i32* %17
%45 = load i32, i32* %17
%46 = call i8* @strerror(i32 %45)
%47 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %46)
br label %58
48: ; preds = %22
%49 = load i32*, i32** %16
%50 = call i32 @addmntent(i32* %49, %struct.mntent* %15)
%51 = icmp eq i32 %50, 1
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = load i32, i32* @errno
store i32 %53, i32* %18
%54 = load i32, i32* %18
%55 = call i8* @strerror(i32 %54)
%56 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %52, %48
br label %58
58: ; preds = %57, %43
%59 = load i32*, i32** %16
%60 = call i32 @endmntent(i32* %59)
%61 = call i32 (...) @unlock_mtab()
%62 = getelementptr inbounds %struct.mntent, %struct.mntent* %15, i32 0, i32 3
%63 = load i8*, i8** %62
%64 = call i32 @free(i8* %63)
%65 = getelementptr inbounds %struct.mntent, %struct.mntent* %15, i32 0, i32 2
%66 = load i8*, i8** %65
%67 = call i32 @free(i8* %66)
ret void
}

LLVM IR (-O2):
%struct.mntent = type { i32, i32, i8*, i8*, i8*, i8* }
@.str = [3 x i8] c"rw\00"
@.str.1 = [10 x i8] c"/etc/mtab\00"
@.str.2 = [3 x i8] c"a+\00"
@errno = global i32 0
@.str.3 = [25 x i8] c"mount: can't open %s: %s\00"
@.str.4 = [28 x i8] c"mount: error writing %s: %s\00"
define void @update_mtab_entry(i8* nocapture %0, i8* %1, i8* nocapture %2, i8* %3, i32 %4, i32 %5, i32 %6) {
%8 = alloca %struct.mntent
%9 = bitcast %struct.mntent* %8 to i8*
%10 = icmp eq i8* %3, null
%11 = select i1 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %3
%12 = tail call i8* @strdup(i8* %0)
%13 = getelementptr inbounds %struct.mntent, %struct.mntent* %8, i64 0, i32 3
store i8* %12, i8** %13
%14 = tail call i8* @canonicalize_path(i8* %1)
%15 = getelementptr inbounds %struct.mntent, %struct.mntent* %8, i64 0, i32 2
store i8* %14, i8** %15
%16 = tail call i8* @strdup(i8* %2)
%17 = getelementptr inbounds %struct.mntent, %struct.mntent* %8, i64 0, i32 5
store i8* %16, i8** %17
%18 = tail call i8* @strdup(i8* %11)
%19 = getelementptr inbounds %struct.mntent, %struct.mntent* %8, i64 0, i32 4
store i8* %18, i8** %19
%20 = getelementptr inbounds %struct.mntent, %struct.mntent* %8, i64 0, i32 0
store i32 %5, i32* %20
%21 = getelementptr inbounds %struct.mntent, %struct.mntent* %8, i64 0, i32 1
store i32 %6, i32* %21
%22 = tail call i32 (...) @lock_mtab()
%23 = tail call i32* @setmntent(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %7
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %27)
br label %36
29: ; preds = %7
%30 = call i32 @addmntent(i32* %23, %struct.mntent* %8)
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %29, %32, %25
%37 = call i32 @endmntent(i32* %23)
%38 = call i32 (...) @unlock_mtab()
%39 = load i8*, i8** %13
%40 = call i32 @free(i8* %39)
%41 = load i8*, i8** %15
%42 = call i32 @free(i8* %41)
ret void
}