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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int /*<<< orphan*/ *SEM_FAILED;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
unsigned int atoi(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getopt(int, char **, char *);
int /*<<< orphan*/ optarg;
int optind;
int /*<<< orphan*/ printf(char *, char *);
int sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int, int, unsigned int);
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int c, flags;
  sem_t *sem;
  unsigned int value;
  flags = O_RDWR | O_CREAT;
  value = 1;
  while ((c = getopt(argc, argv, "ei:")) != -1) {
    switch (c) {
    case 'e':
      flags |= O_EXCL;
      break;
    case 'i':
      value = atoi(optarg);
      break;
    }
  }
  if (optind != argc - 1) {
    printf("usage: %s [-e] [-i initvalue] <name>\n", argv[0]);
    exit(EXIT_FAILURE);
  };
  if ((sem = sem_open(argv[optind], flags,
                      (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), value)) ==
      SEM_FAILED) {
    printf("sem_open error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (sem_close(sem) == -1) {
    printf("sem_close error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    0x2e62(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe50(%rip),%rdx
call   1060 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1210 <main+0xa0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0x65,%eax
je     11e6 <main+0x76>
jmp    11d5 <main+0x65>
mov    -0x28(%rbp),%eax
sub    $0x69,%eax
je     11fa <main+0x8a>
jmp    120b <main+0x9b>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    120b <main+0x9b>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %eax,-0x24(%rbp)
jmp    11a2 <main+0x32>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
je     1248 <main+0xd8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdcf(%rip),%rdi
call   1030 <printf@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2d6d(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x18(%rbp),%esi
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
mov    0x2d5b(%rip),%rax
or     (%rax),%edx
mov    0x2d2a(%rip),%rax
or     (%rax),%edx
mov    0x2d19(%rip),%rax
or     (%rax),%edx
mov    -0x24(%rbp),%ecx
call   1040 <sem_open@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d3c(%rip),%rcx
cmp    (%rcx),%rax
jne    12c8 <main+0x158>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd75(%rip),%rdi
call   1030 <printf@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
call   1050 <sem_close@plt>
cmp    $0xffffffff,%eax
jne    1305 <main+0x195>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd4c(%rip),%rdi
call   1030 <printf@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2c74(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e51(%rip),%rax
mov    0x2e6a(%rip),%rcx
mov    (%rcx),%r15d
or     (%rax),%r15d
mov    $0x1,%r14d
lea    0xe5f(%rip),%r12
mov    0x2e48(%rip),%r13
jmp    11c0 <main+0x50>
mov    0x0(%r13),%edi
call   1070 <atoi@plt>
mov    %eax,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1060 <getopt@plt>
cmp    $0x65,%eax
je     11de <main+0x6e>
cmp    $0x69,%eax
je     11aa <main+0x3a>
cmp    $0xffffffff,%eax
jne    11c0 <main+0x50>
jmp    11ea <main+0x7a>
mov    0x2dcb(%rip),%rax
or     (%rax),%r15d
jmp    11c0 <main+0x50>
mov    0x2dcf(%rip),%rax
movslq (%rax),%rax
add    $0xffffffff,%ebp
cmp    %ebp,%eax
jne    1251 <main+0xe1>
mov    0x2d86(%rip),%rcx
mov    0x2dbf(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d8c(%rip),%rcx
or     (%rcx),%edx
mov    (%rbx,%rax,8),%rdi
mov    0x2d77(%rip),%rax
or     (%rax),%edx
mov    %r15d,%esi
mov    %r14d,%ecx
call   1040 <sem_open@plt>
mov    0x2d9b(%rip),%rcx
cmp    (%rcx),%rax
jne    125d <main+0xed>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xddb(%rip),%rdi
jmp    127f <main+0x10f>
mov    (%rbx),%rsi
lea    0xda9(%rip),%rdi
jmp    1282 <main+0x112>
mov    %rax,%rdi
call   1050 <sem_close@plt>
cmp    $0xffffffff,%eax
jne    1295 <main+0x125>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdbf(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [4 x i8] c"ei:\00"
@O_EXCL = global i32 0
@optarg = global i32 0
@optind = global i32 0
@.str.1 = [38 x i8] c"usage: %s [-e] [-i initvalue] <name>\0A\00"
@EXIT_FAILURE = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [20 x i8] c"sem_open error: %s\0A\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"sem_close error: %s\0A\00"
@EXIT_SUCCESS = global i32 0
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*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
store i32 %12, i32* %7
store i32 1, i32* %9
br label %13
13: ; preds = %27, %2
%14 = load i32, i32* %4
%15 = load i8**, i8*** %5
%16 = call i32 @getopt(i32 %14, i8** %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %16, i32* %6
%17 = icmp ne i32 %16, -1
br i1 %17, label %18, label %28
18: ; preds = %13
%19 = load i32, i32* %6
switch i32 %19, label %27 [
i32 101, label %20
i32 105, label %24
]
20: ; preds = %18
%21 = load i32, i32* @O_EXCL
%22 = load i32, i32* %7
%23 = or i32 %22, %21
store i32 %23, i32* %7
br label %27
24: ; preds = %18
%25 = load i32, i32* @optarg
%26 = call i32 @atoi(i32 %25)
store i32 %26, i32* %9
br label %27
27: ; preds = %18, %24, %20
br label %13
28: ; preds = %13
%29 = load i32, i32* @optind
%30 = load i32, i32* %4
%31 = sub nsw i32 %30, 1
%32 = icmp ne i32 %29, %31
br i1 %32, label %33, label %40
33: ; preds = %28
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %28
%41 = load i8**, i8*** %5
%42 = load i32, i32* @optind
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
%46 = load i32, i32* %7
%47 = load i32, i32* @S_IRUSR
%48 = load i32, i32* @S_IWUSR
%49 = or i32 %47, %48
%50 = load i32, i32* @S_IRGRP
%51 = or i32 %49, %50
%52 = load i32, i32* @S_IROTH
%53 = or i32 %51, %52
%54 = load i32, i32* %9
%55 = call i32* @sem_open(i8* %45, i32 %46, i32 %53, i32 %54)
store i32* %55, i32** %8
%56 = load i32*, i32** @SEM_FAILED
%57 = icmp eq i32* %55, %56
br i1 %57, label %58, label %64
58: ; preds = %40
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %60)
%62 = load i32, i32* @EXIT_FAILURE
%63 = call i32 @exit(i32 %62)
unreachable
64: ; preds = %40
%65 = load i32*, i32** %8
%66 = call i32 @sem_close(i32* %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %74
68: ; preds = %64
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %70)
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %64
%75 = load i32, i32* @EXIT_SUCCESS
%76 = call i32 @exit(i32 %75)
unreachable
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [4 x i8] c"ei:\00"
@O_EXCL = global i32 0
@optarg = global i32 0
@optind = global i32 0
@.str.1 = [38 x i8] c"usage: %s [-e] [-i initvalue] <name>\0A\00"
@EXIT_FAILURE = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@SEM_FAILED = global i32* null
@.str.2 = [20 x i8] c"sem_open error: %s\0A\00"
@errno = global i32 0
@.str.3 = [21 x i8] c"sem_close error: %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
br label %6
6: ; preds = %13, %2
%7 = phi i32 [ %15, %13 ], [ %5, %2 ]
%8 = phi i32 [ %10, %13 ], [ 1, %2 ]
br label %9
9: ; preds = %6, %16
%10 = phi i32 [ %8, %6 ], [ %18, %16 ]
br label %11
11: ; preds = %9, %11
%12 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
switch i32 %12, label %11 [
i32 -1, label %19
i32 101, label %13
i32 105, label %16
]
13: ; preds = %11
%14 = load i32, i32* @O_EXCL
%15 = or i32 %14, %7
br label %6
16: ; preds = %11
%17 = load i32, i32* @optarg
%18 = tail call i32 @atoi(i32 %17)
br label %9
19: ; preds = %11
%20 = load i32, i32* @optind
%21 = add nsw i32 %0, -1
%22 = icmp eq i32 %20, %21
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = load i8*, i8** %1
%25 = tail call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = sext i32 %20 to i64
%30 = getelementptr inbounds i8*, i8** %1, i64 %29
%31 = load i8*, i8** %30
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %33, %32
%35 = load i32, i32* @S_IRGRP
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IROTH
%38 = or i32 %36, %37
%39 = tail call i32* @sem_open(i8* %31, i32 %7, i32 %38, i32 %10)
%40 = load i32*, i32** @SEM_FAILED
%41 = icmp eq i32* %39, %40
br i1 %41, label %42, label %48
42: ; preds = %28
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %28
%49 = tail call i32 @sem_close(i32* %39)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = tail call i8* @strerror(i32 %52)
%54 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = tail call i32 @exit(i32 %55)
unreachable
57: ; preds = %48
%58 = load i32, i32* @EXIT_SUCCESS
%59 = tail call i32 @exit(i32 %58)
unreachable
}