#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 sembuf {
  int sem_num;
  int sem_flg;
  int /*<<< orphan*/ sem_op;
};

/* Variables and functions */
int EXIT_SUCCESS;
int IPC_NOWAIT;
int SEM_UNDO;
int /*<<< orphan*/ atoi(char *);
struct sembuf *calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int getopt(int, char **, char *);
int optind;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int semget(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int semop(int, struct sembuf *, int);
int main(int argc, char *argv[]) {
  int c, i, flag, semid, nops;
  struct sembuf *ptr;
  flag = 0;
  while ((c = getopt(argc, argv, "nu")) != -1) {
    switch (c) {
    case 'n':
      flag |= IPC_NOWAIT;
      break;
    case 'u':
      flag |= SEM_UNDO;
      break;
    }
  }
  if (argc - optind < 2) {
    printf("usage: semops [-n] [-u] <pathname> operation...");
    exit(0);
  }
  if ((semid = semget(ftok(argv[optind], 0), 0, 0)) == -1) {
    perror("semget() error");
    exit(-1);
  }
  optind++;
  nops = argc - optind;
  ptr = calloc(nops, sizeof(struct sembuf));
  for (i = 0; i < nops; ++i) {
    ptr[i].sem_num = i;
    ptr[i].sem_op = atoi(argv[optind + i]);
    ptr[i].sem_flg = flag;
  }
  if (semop(semid, ptr, nops) == -1) {
    perror("semop() error");
    exit(-1);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe45(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1219 <main+0x89>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0x6e,%eax
je     11f1 <main+0x61>
jmp    11e0 <main+0x50>
mov    -0x34(%rbp),%eax
sub    $0x75,%eax
je     1205 <main+0x75>
jmp    1214 <main+0x84>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1214 <main+0x84>
mov    0x2de4(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11ad <main+0x1d>
mov    -0x8(%rbp),%eax
mov    0x2db5(%rip),%rcx
sub    (%rcx),%eax
cmp    $0x2,%eax
jge    1241 <main+0xb1>
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x2d8c(%rip),%rcx
movslq (%rcx),%rcx
mov    (%rax,%rcx,8),%rdi
xor    %esi,%esi
call   10b0 <ftok@plt>
mov    %eax,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <semget@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    1287 <main+0xf7>
lea    0xdbb(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    0x2d3e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%eax
mov    0x2d32(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    $0xc,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1336 <main+0x1a6>
mov    -0x18(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq -0x18(%rbp),%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    0x2cea(%rip),%rcx
mov    (%rcx),%ecx
add    -0x18(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
call   1080 <atoi@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq -0x18(%rbp),%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,0x8(%rax)
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq -0x18(%rbp),%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,0x4(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12c3 <main+0x133>
mov    -0x20(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1090 <semop@plt>
cmp    $0xffffffff,%eax
jne    1364 <main+0x1d4>
lea    0xced(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2c5d(%rip),%rax
mov    (%rax),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%r12d
xor    %r13d,%r13d
lea    0xe52(%rip),%rbp
mov    0x2e33(%rip),%r14
mov    0x2e34(%rip),%r15
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <getopt@plt>
cmp    $0xffffffff,%eax
je     11e7 <main+0x57>
cmp    $0x6e,%eax
je     11e2 <main+0x52>
cmp    $0x75,%eax
jne    11c0 <main+0x30>
or     (%r15),%r13d
jmp    11c0 <main+0x30>
or     (%r14),%r13d
jmp    11c0 <main+0x30>
mov    0x2dea(%rip),%rbp
movslq 0x0(%rbp),%rax
mov    %r12d,%ecx
sub    %eax,%ecx
cmp    $0x1,%ecx
jle    12af <main+0x11f>
mov    (%rbx,%rax,8),%rdi
xor    %esi,%esi
call   10b0 <ftok@plt>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <semget@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
je     12c2 <main+0x132>
movslq 0x0(%rbp),%r14
add    $0x1,%r14
mov    %r14d,0x0(%rbp)
mov    %r12d,%edi
sub    %r14d,%edi
mov    $0xc,%esi
call   1050 <calloc@plt>
mov    %rax,0x10(%rsp)
sub    %r14d,%r12d
jle    1281 <main+0xf1>
mov    %r12d,%ebp
lea    (%rbx,%r14,8),%rbx
mov    0x10(%rsp),%r15
add    $0x8,%r15
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r14d,-0x8(%r15)
mov    (%rbx,%r14,8),%rdi
call   1080 <atoi@plt>
mov    %eax,(%r15)
mov    %r13d,-0x4(%r15)
add    $0x1,%r14
add    $0xc,%r15
cmp    %r14,%rbp
jne    1260 <main+0xd0>
mov    0xc(%rsp),%edi
mov    0x10(%rsp),%rsi
mov    %r12d,%edx
call   1090 <semop@plt>
cmp    $0xffffffff,%eax
je     12cb <main+0x13b>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xd6a(%rip),%rdi
jmp    12d2 <main+0x142>
lea    0xd70(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i32 }
@.str = [3 x i8] c"nu\00"
@IPC_NOWAIT = global i32 0
@SEM_UNDO = global i32 0
@optind = global i32 0
@.str.1 = [48 x i8] c"usage: semops [-n] [-u] <pathname> operation...\00"
@.str.2 = [15 x i8] c"semget() error\00"
@.str.3 = [14 x i8] c"semop() error\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
%10 = alloca i32
%11 = alloca %struct.sembuf*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
br label %12
12: ; preds = %27, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %6
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i32, i32* %6
switch i32 %18, label %27 [
i32 110, label %19
i32 117, label %23
]
19: ; preds = %17
%20 = load i32, i32* @IPC_NOWAIT
%21 = load i32, i32* %8
%22 = or i32 %21, %20
store i32 %22, i32* %8
br label %27
23: ; preds = %17
%24 = load i32, i32* @SEM_UNDO
%25 = load i32, i32* %8
%26 = or i32 %25, %24
store i32 %26, i32* %8
br label %27
27: ; preds = %17, %23, %19
br label %12
28: ; preds = %12
%29 = load i32, i32* %4
%30 = load i32, i32* @optind
%31 = sub nsw i32 %29, %30
%32 = icmp slt i32 %31, 2
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = call i32 @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %28
%37 = load i8**, i8*** %5
%38 = load i32, i32* @optind
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8*, i8** %37, i64 %39
%41 = load i8*, i8** %40
%42 = call i32 @ftok(i8* %41, i32 0)
%43 = call i32 @semget(i32 %42, i32 0, i32 0)
store i32 %43, i32* %9
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %36
%46 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %36
%49 = load i32, i32* @optind
%50 = add nsw i32 %49, 1
store i32 %50, i32* @optind
%51 = load i32, i32* %4
%52 = load i32, i32* @optind
%53 = sub nsw i32 %51, %52
store i32 %53, i32* %10
%54 = load i32, i32* %10
%55 = call %struct.sembuf* @calloc(i32 %54, i32 12)
store %struct.sembuf* %55, %struct.sembuf** %11
store i32 0, i32* %7
br label %56
56: ; preds = %86, %48
%57 = load i32, i32* %7
%58 = load i32, i32* %10
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %89
60: ; preds = %56
%61 = load i32, i32* %7
%62 = load %struct.sembuf*, %struct.sembuf** %11
%63 = load i32, i32* %7
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %62, i64 %64
%66 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %65, i32 0, i32 0
store i32 %61, i32* %66
%67 = load i8**, i8*** %5
%68 = load i32, i32* @optind
%69 = load i32, i32* %7
%70 = add nsw i32 %68, %69
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8*, i8** %67, i64 %71
%73 = load i8*, i8** %72
%74 = call i32 @atoi(i8* %73)
%75 = load %struct.sembuf*, %struct.sembuf** %11
%76 = load i32, i32* %7
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %75, i64 %77
%79 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %78, i32 0, i32 2
store i32 %74, i32* %79
%80 = load i32, i32* %8
%81 = load %struct.sembuf*, %struct.sembuf** %11
%82 = load i32, i32* %7
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %81, i64 %83
%85 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %84, i32 0, i32 1
store i32 %80, i32* %85
br label %86
86: ; preds = %60
%87 = load i32, i32* %7
%88 = add nsw i32 %87, 1
store i32 %88, i32* %7
br label %56
89: ; preds = %56
%90 = load i32, i32* %9
%91 = load %struct.sembuf*, %struct.sembuf** %11
%92 = load i32, i32* %10
%93 = call i32 @semop(i32 %90, %struct.sembuf* %91, i32 %92)
%94 = icmp eq i32 %93, -1
br i1 %94, label %95, label %98
95: ; preds = %89
%96 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%97 = call i32 @exit(i32 -1)
unreachable
98: ; preds = %89
%99 = load i32, i32* @EXIT_SUCCESS
ret i32 %99
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@.str = [3 x i8] c"nu\00"
@IPC_NOWAIT = global i32 0
@SEM_UNDO = global i32 0
@optind = global i32 0
@.str.1 = [48 x i8] c"usage: semops [-n] [-u] <pathname> operation...\00"
@.str.2 = [15 x i8] c"semget() error\00"
@.str.3 = [14 x i8] c"semop() error\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
br label %3
3: ; preds = %8, %2
%4 = phi i32 [ 0, %2 ], [ %11, %8 ]
br label %5
5: ; preds = %3, %5
%6 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %6, label %5 [
i32 -1, label %12
i32 110, label %8
i32 117, label %7
]
7: ; preds = %5
br label %8
8: ; preds = %5, %7
%9 = phi i32* [ @SEM_UNDO, %7 ], [ @IPC_NOWAIT, %5 ]
%10 = load i32, i32* %9
%11 = or i32 %10, %4
br label %3
12: ; preds = %5
%13 = load i32, i32* @optind
%14 = sub nsw i32 %0, %13
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 0)
unreachable
19: ; preds = %12
%20 = sext i32 %13 to i64
%21 = getelementptr inbounds i8*, i8** %1, i64 %20
%22 = load i8*, i8** %21
%23 = tail call i32 @ftok(i8* %22, i32 0)
%24 = tail call i32 @semget(i32 %23, i32 0, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 -1)
unreachable
29: ; preds = %19
%30 = load i32, i32* @optind
%31 = add nsw i32 %30, 1
store i32 %31, i32* @optind
%32 = sub nsw i32 %0, %31
%33 = tail call %struct.sembuf* @calloc(i32 %32, i32 12)
%34 = icmp sgt i32 %32, 0
br i1 %34, label %35, label %50
35: ; preds = %29
%36 = sext i32 %31 to i64
%37 = zext i32 %32 to i64
br label %38
38: ; preds = %35, %38
%39 = phi i64 [ 0, %35 ], [ %48, %38 ]
%40 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %33, i64 %39, i32 0
%41 = trunc i64 %39 to i32
store i32 %41, i32* %40
%42 = add nsw i64 %39, %36
%43 = getelementptr inbounds i8*, i8** %1, i64 %42
%44 = load i8*, i8** %43
%45 = tail call i32 @atoi(i8* %44)
%46 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %33, i64 %39, i32 2
store i32 %45, i32* %46
%47 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %33, i64 %39, i32 1
store i32 %4, i32* %47
%48 = add nuw nsw i64 %39, 1
%49 = icmp eq i64 %48, %37
br i1 %49, label %50, label %38
50: ; preds = %38, %29
%51 = tail call i32 @semop(i32 %24, %struct.sembuf* %33, i32 %32)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %50
%54 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%55 = tail call i32 @exit(i32 -1)
unreachable
56: ; preds = %50
%57 = load i32, i32* @EXIT_SUCCESS
ret i32 %57
}