#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 O_CREAT;
int O_EXCL;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int fd;
  mode_t mode = S_IRUSR | S_IWUSR;
  int flags = O_WRONLY | O_CREAT | O_EXCL;
  if (argc < 2) {
    fprintf(stderr, "openclose: Too few arguments\n");
    fprintf(stderr, "Usage: openclose <filename>\n");
    exit(1);
  }
  fd = open(argv[1], flags, mode);
  if (fd < 0) {
    fprintf(stderr, "openclose: Cannot open file '%s'\n", argv[1]);
    exit(1);
  }
  if (close(fd) != 0) {
    fprintf(stderr, "Cannot close file (descriptor=%d)\n", fd);
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    0x2e7a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    0x2e7d(%rip),%rcx
or     (%rcx),%eax
mov    0x2e44(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11cb <main+0x8b>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1214 <main+0xd4>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe38(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
je     124a <main+0x10a>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     1179 <main+0x39>
mov    0x2e81(%rip),%rbx
mov    (%rbx),%edi
lea    0xea8(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xeb6(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %rsi,%rbx
mov    0x2e2d(%rip),%rax
mov    0x2e6e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e33(%rip),%rax
or     (%rax),%esi
mov    0x2e22(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x8(%rbx),%rdi
call   1050 <open@plt>
test   %eax,%eax
jns    11db <main+0x9b>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe71(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1030 <close@plt>
test   %rax,%rax
jne    11f0 <main+0xb0>
xor    %edi,%edi
call   1060 <exit@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"openclose: Too few arguments\0A\00"
@.str.1 = [29 x i8] c"Usage: openclose <filename>\0A\00"
@.str.2 = [34 x i8] c"openclose: Cannot open file '%s'\0A\00"
@.str.3 = [35 x i8] c"Cannot close file (descriptor=%d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %9, %10
store i32 %11, i32* %7
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_EXCL
%16 = or i32 %14, %15
store i32 %16, i32* %8
%17 = load i32, i32* %4
%18 = icmp slt i32 %17, 2
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @stderr
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* %8
%30 = load i32, i32* %7
%31 = call i32 @open(i8* %28, i32 %29, i32 %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %25
%35 = load i32, i32* @stderr
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %25
%42 = load i32, i32* %6
%43 = call i64 @close(i32 %42)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = load i32, i32* %6
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %41
%51 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"openclose: Too few arguments\0A\00"
@.str.1 = [29 x i8] c"Usage: openclose <filename>\0A\00"
@.str.2 = [34 x i8] c"openclose: Cannot open file '%s'\0A\00"
@.str.3 = [35 x i8] c"Cannot close file (descriptor=%d)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @O_EXCL
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %17, %16
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = tail call i32 @open(i8* %20, i32 %15, i32 %18)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %10
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %19
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = tail call i32 @exit(i32 1)
unreachable
28: ; preds = %10
%29 = tail call i64 @close(i32 %21)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %28
%36 = tail call i32 @exit(i32 0)
unreachable
}