#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 mode_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ edit(int, int, char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
void passwd(char *file, char *user, int op) {
  int fd;
  int is_open, cnt, flags;
  mode_t mode;
  is_open = cnt = flags = 0;
  if (op & (0x2 | 0x4 | 0x8)) {
    is_open = 1;
    flags |= O_RDWR;
    if (op & 0x4)
      flags |= O_CREAT;
    mode = S_IRUSR | S_IWUSR | S_IRGRP;
    if ((fd = open(file, flags, mode)) == -1) {
      perror("Open the file occur error: ");
      exit(EXIT_FAILURE);
    }
  }
  edit(fd, op, user);
  if (is_open)
    close(fd);
}


x86 assembly (clang, -O0):
<passwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
and    $0xe,%eax
cmp    $0x0,%eax
je     1213 <passwd+0xc3>
movl   $0x1,-0x1c(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     11bb <passwd+0x6b>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    0x2e0d(%rip),%rcx
or     (%rcx),%eax
mov    0x2df4(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    120e <passwd+0xbe>
lea    0xe05(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1213 <passwd+0xc3>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rdx
call   1030 <edit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1234 <passwd+0xe4>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<passwd>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
test   $0xe,%bpl
je     11b7 <passwd+0x67>
mov    %ebp,%esi
shl    $0x1d,%esi
sar    $0x1f,%esi
mov    0x2e8a(%rip),%rax
and    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
mov    0x2e38(%rip),%rax
mov    0x2e51(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e36(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11c5 <passwd+0x75>
mov    %eax,%ebx
mov    %eax,%edi
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <edit@plt>
mov    %ebx,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
mov    %ebp,%esi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <edit@plt>
lea    0xe34(%rip),%rdi
call   1060 <perror@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str = [28 x i8] c"Open the file occur error: \00"
@EXIT_FAILURE = global i32 0
define void @passwd(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %10
store i32 0, i32* %9
store i32 0, i32* %8
%12 = load i32, i32* %6
%13 = and i32 %12, 14
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %42
15: ; preds = %3
store i32 1, i32* %8
%16 = load i32, i32* @O_RDWR
%17 = load i32, i32* %10
%18 = or i32 %17, %16
store i32 %18, i32* %10
%19 = load i32, i32* %6
%20 = and i32 %19, 4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = load i32, i32* @O_CREAT
%24 = load i32, i32* %10
%25 = or i32 %24, %23
store i32 %25, i32* %10
br label %26
26: ; preds = %22, %15
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRGRP
%31 = or i32 %29, %30
store i32 %31, i32* %11
%32 = load i8*, i8** %4
%33 = load i32, i32* %10
%34 = load i32, i32* %11
%35 = call i32 @open(i8* %32, i32 %33, i32 %34)
store i32 %35, i32* %7
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %26
br label %42
42: ; preds = %41, %3
%43 = load i32, i32* %7
%44 = load i32, i32* %6
%45 = load i8*, i8** %5
%46 = call i32 @edit(i32 %43, i32 %44, i8* %45)
%47 = load i32, i32* %8
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %49, %42
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@.str = [28 x i8] c"Open the file occur error: \00"
@EXIT_FAILURE = global i32 0
define void @passwd(i8* nocapture %0, i8* %1, i32 %2) {
%4 = and i32 %2, 14
%5 = icmp eq i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @edit(i32 undef, i32 %2, i8* %1)
br label %29
8: ; preds = %3
%9 = load i32, i32* @O_RDWR
%10 = and i32 %2, 4
%11 = icmp eq i32 %10, 0
%12 = load i32, i32* @O_CREAT
%13 = select i1 %11, i32 0, i32 %12
%14 = or i32 %13, %9
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = load i32, i32* @S_IRGRP
%19 = or i32 %17, %18
%20 = tail call i32 @open(i8* %0, i32 %14, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %8
%23 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %8
%27 = tail call i32 @edit(i32 %20, i32 %2, i8* %1)
%28 = tail call i32 @close(i32 %20)
br label %29
29: ; preds = %6, %26
ret void
}