#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 */

/* Variables and functions */
scalar_t__ ENOENT;
int O_APPEND;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ write_armag(int, char *);
int open_archive(char *archive_name, int verbose) {
  int ar_fd;
  int flags = O_RDWR | O_APPEND;
  int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  ar_fd = open(archive_name, flags, mode);
  if (ar_fd == -1) {
    if (errno == ENOENT) {
      flags |= O_CREAT;
      if (verbose)
        printf("creating %s\n", archive_name);
      ar_fd = open(archive_name, flags, mode);
    }
    if (ar_fd == -1) {
      perror("Error, opening archive file");
      exit(-1);
    }
  }
  if (flags & O_CREAT) {
    write_armag(ar_fd, archive_name);
  }
  return ar_fd;
}


x86 assembly (clang, -O0):
<open_archive>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    0x2e21(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    0x2e4c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e2b(%rip),%rcx
or     (%rcx),%eax
mov    0x2e42(%rip),%rcx
or     (%rcx),%eax
mov    0x2e11(%rip),%rcx
or     (%rcx),%eax
mov    0x2de0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1243 <open_archive+0xf3>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%rax
jne    121e <open_archive+0xce>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     120c <open_archive+0xbc>
mov    -0x8(%rbp),%rsi
lea    0xdf9(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    123e <open_archive+0xee>
lea    0xdde(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    1243 <open_archive+0xf3>
mov    -0x14(%rbp),%eax
mov    0x2dab(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1264 <open_archive+0x114>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1040 <write_armag@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_archive>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e81(%rip),%rax
mov    0x2e2a(%rip),%rcx
mov    (%rcx),%r15d
or     (%rax),%r15d
mov    0x2e35(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%ebp
or     (%rax),%ebp
mov    0x2e33(%rip),%rax
or     (%rax),%ebp
mov    0x2e4a(%rip),%rax
or     (%rax),%ebp
mov    0x2e19(%rip),%rax
or     (%rax),%ebp
mov    %esi,%r12d
mov    %rdi,%r14
mov    0x2de2(%rip),%rax
or     (%rax),%ebp
mov    %r15d,%esi
mov    %ebp,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11bb <open_archive+0x6b>
mov    %eax,%ebx
jmp    1203 <open_archive+0xb3>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2dcc(%rip),%rcx
cmp    (%rcx),%rax
jne    1224 <open_archive+0xd4>
mov    0x2e20(%rip),%rax
or     (%rax),%r15d
test   %r12d,%r12d
je     11ef <open_archive+0x9f>
lea    0xe19(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    %r14,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
call   1050 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1224 <open_archive+0xd4>
mov    0x2dee(%rip),%rax
test   %r15d,(%rax)
je     1219 <open_archive+0xc9>
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <write_armag@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde2(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@O_CREAT = global i32 0
@.str = [13 x i8] c"creating %s\0A\00"
@.str.1 = [28 x i8] c"Error, opening archive file\00"
define i32 @open_archive(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_APPEND
%10 = or i32 %8, %9
store i32 %10, i32* %6
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IRGRP
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IROTH
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IWOTH
%21 = or i32 %19, %20
store i32 %21, i32* %7
%22 = load i8*, i8** %3
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = call i32 @open(i8* %22, i32 %23, i32 %24)
store i32 %25, i32* %5
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %53
28: ; preds = %2
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOENT
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %46
32: ; preds = %28
%33 = load i32, i32* @O_CREAT
%34 = load i32, i32* %6
%35 = or i32 %34, %33
store i32 %35, i32* %6
%36 = load i32, i32* %4
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i8*, i8** %3
%40 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %38, %32
%42 = load i8*, i8** %3
%43 = load i32, i32* %6
%44 = load i32, i32* %7
%45 = call i32 @open(i8* %42, i32 %43, i32 %44)
store i32 %45, i32* %5
br label %46
46: ; preds = %41, %28
%47 = load i32, i32* %5
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %52
49: ; preds = %46
%50 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%51 = call i32 @exit(i32 -1)
unreachable
52: ; preds = %46
br label %53
53: ; preds = %52, %2
%54 = load i32, i32* %6
%55 = load i32, i32* @O_CREAT
%56 = and i32 %54, %55
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %53
%59 = load i32, i32* %5
%60 = load i8*, i8** %3
%61 = call i32 @write_armag(i32 %59, i8* %60)
br label %62
62: ; preds = %58, %53
%63 = load i32, i32* %5
ret i32 %63
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@O_CREAT = global i32 0
@.str = [13 x i8] c"creating %s\0A\00"
@.str.1 = [28 x i8] c"Error, opening archive file\00"
define i32 @open_archive(i8* %0, i32 %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_APPEND
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IWGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IWOTH
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %0, i32 %5, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %35
19: ; preds = %2
%20 = load i64, i64* @errno
%21 = load i64, i64* @ENOENT
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %5
%26 = icmp eq i32 %1, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %29
29: ; preds = %23, %27
%30 = tail call i32 @open(i8* %0, i32 %25, i32 %16)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %19, %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %29, %2
%36 = phi i32 [ %25, %29 ], [ %5, %2 ]
%37 = phi i32 [ %30, %29 ], [ %17, %2 ]
%38 = load i32, i32* @O_CREAT
%39 = and i32 %38, %36
%40 = icmp eq i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %35
%42 = tail call i32 @write_armag(i32 %37, i8* %0)
br label %43
43: ; preds = %41, %35
ret i32 %37
}