#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_op;
  int /*<<< orphan*/ sem_flg;
  scalar_t__ sem_num;
};

/* Variables and functions */
int EOF;
int IPC_CREAT;
int /*<<< orphan*/ IPC_NOWAIT;
int atoi(int /*<<< orphan*/);
int getopt(int, char **, char *);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, int);
int semget(int, int, int);
int semop(int, struct sembuf *, int);
int main(int argc, char **argv) {
  int ch;
  int num = 1;
  int key = 0x8888;
  int id;
  int error;
  struct sembuf buf;
  while ((ch = getopt(argc, argv, "k:-n:")) != EOF) {
    switch (ch) {
    case 'k':
      key = atoi(optarg);
      break;
    case 'n':
      num = atoi(optarg);
      break;
    }
  }
  id = semget(key, num, IPC_CREAT | 0777);
  if (id == -1)
    return 1;
  buf.sem_num = 0;
  buf.sem_op = 1;
  buf.sem_flg = IPC_NOWAIT;
  error = semop(id, &buf, 1);
  printf("semop: returned %d\n", error);
  return error;
}


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   $0x1,-0x18(%rbp)
movl   $0x8888,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe7e(%rip),%rdx
call   1050 <getopt@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e47(%rip),%rcx
cmp    (%rcx),%eax
je     11ea <main+0x9a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
sub    $0x6b,%eax
je     11be <main+0x6e>
jmp    11ad <main+0x5d>
mov    -0x3c(%rbp),%eax
sub    $0x6e,%eax
je     11d4 <main+0x84>
jmp    11e5 <main+0x95>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    11e5 <main+0x95>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1060 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1174 <main+0x24>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
or     $0x1ff,%edx
call   1030 <semget@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    121d <main+0xcd>
movl   $0x1,-0x4(%rbp)
jmp    1261 <main+0x111>
movq   $0x0,-0x30(%rbp)
movl   $0x1,-0x38(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdb0(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%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,%ebp
lea    0xe96(%rip),%rdx
call   1050 <getopt@plt>
mov    0x2e62(%rip),%r14
mov    $0x8888,%r15d
mov    $0x1,%r12d
cmp    (%r14),%eax
jne    11fa <main+0xaa>
mov    0x2e3a(%rip),%rax
mov    $0x1ff,%edx
or     (%rax),%edx
mov    %r15d,%edi
mov    %r12d,%esi
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
mov    $0x1,%ebx
je     11e9 <main+0x99>
movq   $0x0,0x10(%rsp)
movl   $0x1,0x8(%rsp)
mov    0x2e26(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1070 <semop@plt>
mov    %eax,%ebx
lea    0xe24(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x8888,%r15d
mov    $0x1,%r12d
lea    0xdf3(%rip),%r13
jmp    1237 <main+0xe7>
nop
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1060 <atoi@plt>
mov    %eax,%r12d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1050 <getopt@plt>
cmp    (%r14),%eax
je     1187 <main+0x37>
cmp    $0x6e,%eax
je     1210 <main+0xc0>
cmp    $0x6b,%eax
jne    1221 <main+0xd1>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1060 <atoi@plt>
mov    %eax,%r15d
jmp    1221 <main+0xd1>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@.str = [6 x i8] c"k:-n:\00"
@EOF = global i32 0
@optarg = global i32 0
@IPC_CREAT = global i32 0
@IPC_NOWAIT = global i32 0
@.str.1 = [20 x i8] c"semop: returned %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
%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 1, i32* %7
store i32 34952, i32* %8
br label %12
12: ; preds = %26, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %6
%16 = load i32, i32* @EOF
%17 = icmp ne i32 %15, %16
br i1 %17, label %18, label %27
18: ; preds = %12
%19 = load i32, i32* %6
switch i32 %19, label %26 [
i32 107, label %20
i32 110, label %23
]
20: ; preds = %18
%21 = load i32, i32* @optarg
%22 = call i32 @atoi(i32 %21)
store i32 %22, i32* %8
br label %26
23: ; preds = %18
%24 = load i32, i32* @optarg
%25 = call i32 @atoi(i32 %24)
store i32 %25, i32* %7
br label %26
26: ; preds = %18, %23, %20
br label %12
27: ; preds = %12
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = load i32, i32* @IPC_CREAT
%31 = or i32 %30, 511
%32 = call i32 @semget(i32 %28, i32 %29, i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %27
store i32 1, i32* %3
br label %46
36: ; preds = %27
%37 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %11, i32 0, i32 2
store i64 0, i64* %37
%38 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %11, i32 0, i32 0
store i32 1, i32* %38
%39 = load i32, i32* @IPC_NOWAIT
%40 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %11, i32 0, i32 1
store i32 %39, i32* %40
%41 = load i32, i32* %9
%42 = call i32 @semop(i32 %41, %struct.sembuf* %11, i32 1)
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %10
store i32 %45, i32* %3
br label %46
46: ; preds = %36, %35
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@.str = [6 x i8] c"k:-n:\00"
@EOF = global i32 0
@optarg = global i32 0
@IPC_CREAT = global i32 0
@IPC_NOWAIT = global i32 0
@.str.1 = [20 x i8] c"semop: returned %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.sembuf
%4 = bitcast %struct.sembuf* %3 to i8*
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @EOF
%7 = icmp eq i32 %5, %6
br i1 %7, label %24, label %8
8: ; preds = %2, %18
%9 = phi i32 [ %21, %18 ], [ %5, %2 ]
%10 = phi i32 [ %20, %18 ], [ 1, %2 ]
%11 = phi i32 [ %19, %18 ], [ 34952, %2 ]
switch i32 %9, label %18 [
i32 107, label %12
i32 110, label %15
]
12: ; preds = %8
%13 = load i32, i32* @optarg
%14 = tail call i32 @atoi(i32 %13)
br label %18
15: ; preds = %8
%16 = load i32, i32* @optarg
%17 = tail call i32 @atoi(i32 %16)
br label %18
18: ; preds = %8, %15, %12
%19 = phi i32 [ %11, %8 ], [ %11, %15 ], [ %14, %12 ]
%20 = phi i32 [ %10, %8 ], [ %17, %15 ], [ %10, %12 ]
%21 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EOF
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %8
24: ; preds = %18, %2
%25 = phi i32 [ 34952, %2 ], [ %19, %18 ]
%26 = phi i32 [ 1, %2 ], [ %20, %18 ]
%27 = load i32, i32* @IPC_CREAT
%28 = or i32 %27, 511
%29 = tail call i32 @semget(i32 %25, i32 %26, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %38, label %31
31: ; preds = %24
%32 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
store i64 0, i64* %32
%33 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
store i32 1, i32* %33
%34 = load i32, i32* @IPC_NOWAIT
%35 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
store i32 %34, i32* %35
%36 = call i32 @semop(i32 %29, %struct.sembuf* %3, i32 1)
%37 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %24, %31
%39 = phi i32 [ %36, %31 ], [ 1, %24 ]
ret i32 %39
}