#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*/ O_CREAT;
int /*<<< orphan*/ *SEM_FAILED;
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sem_close(int /*<<< orphan*/ *);
int sem_getvalue(int /*<<< orphan*/ *, int *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sem_unlink(char *);
int sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  sem_t *mysemp;
  char semname[28];
  int value = 10;
  int val;
  sprintf(semname,
          "/"
          "sem_wait"
          "_"
          "1-2"
          "_%d",
          getpid());
  mysemp = sem_open(semname, O_CREAT, 0777, value);
  if (mysemp == SEM_FAILED || mysemp == NULL) {
    perror("unexpected error: "
           "sem_wait"
           " "
           "1-2"
           ": "
           "sem_open");
    return 2;
  }
  while (value) {
    if (sem_wait(mysemp) == -1) {
      perror("unexpected error: "
             "sem_wait"
             " "
             "1-2"
             ": "
             "sem_getvalue");
      return 2;
    } else {
      value--;
    }
  }
  if (sem_getvalue(mysemp, &val) == -1) {
    perror("unexpected error: "
           "sem_wait"
           " "
           "1-2"
           ": "
           "sem_getvalue");
    return 2;
  } else if (val == 0) {
    puts("TEST PASSED");
    sem_unlink(semname);
    sem_close(mysemp);
    return 0;
  } else {
    puts("TEST FAILED: Semaphore is not locked");
    return 1;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xa,-0x34(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xe3e(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    -0x34(%rbp),%ecx
mov    $0x1ff,%edx
call   1070 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%rax
je     1204 <main+0x74>
cmpq   $0x0,-0x10(%rbp)
jne    121c <main+0x8c>
lea    0xe06(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d8 <main+0x148>
jmp    1221 <main+0x91>
cmpl   $0x0,-0x34(%rbp)
je     1263 <main+0xd3>
mov    -0x10(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    1255 <main+0xc5>
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d8 <main+0x148>
mov    -0x34(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x34(%rbp)
jmp    1221 <main+0x91>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1060 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
jne    1291 <main+0x101>
lea    0xdba(%rip),%rdi
call   1090 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    12d8 <main+0x148>
cmpl   $0x0,-0x38(%rbp)
jne    12c5 <main+0x135>
lea    0xdc5(%rip),%rdi
call   1040 <puts@plt>
lea    -0x30(%rbp),%rdi
call   10b0 <sem_unlink@plt>
mov    -0x10(%rbp),%rdi
call   1080 <sem_close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d8 <main+0x148>
lea    0xda7(%rip),%rdi
call   1040 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe5d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   10a0 <sprintf@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x1ff,%edx
mov    $0xa,%ecx
call   1070 <sem_open@plt>
test   %rax,%rax
je     12dc <main+0x14c>
mov    %rax,%rbx
mov    0x2e00(%rip),%rax
cmp    (%rax),%rbx
je     12dc <main+0x14c>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <sem_getvalue@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
cmpl   $0x0,0xc(%rsp)
je     12b6 <main+0x126>
lea    0xddc(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    12d6 <main+0x146>
lea    0xd90(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%eax
jmp    12d6 <main+0x146>
lea    0xdaa(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rdi
call   10b0 <sem_unlink@plt>
mov    %rbx,%rdi
call   1080 <sem_close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret
lea    0xd2e(%rip),%rdi
jmp    12aa <main+0x11a>

LLVM IR (-O0):
@.str = [17 x i8] c"/sem_wait_1-2_%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [41 x i8] c"unexpected error: sem_wait 1-2: sem_open\00"
@.str.2 = [45 x i8] c"unexpected error: sem_wait 1-2: sem_getvalue\00"
@.str.3 = [12 x i8] c"TEST PASSED\00"
@.str.4 = [37 x i8] c"TEST FAILED: Semaphore is not locked\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [28 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 10, i32* %4
%6 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%7 = call i32 (...) @getpid()
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* %4
%12 = call i32* @sem_open(i8* %9, i32 %10, i32 511, i32 %11)
store i32* %12, i32** %2
%13 = load i32*, i32** %2
%14 = load i32*, i32** @SEM_FAILED
%15 = icmp eq i32* %13, %14
br i1 %15, label %19, label %16
16: ; preds = %0
%17 = load i32*, i32** %2
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %16, %0
%20 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %52
21: ; preds = %16
br label %22
22: ; preds = %34, %21
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %35
25: ; preds = %22
%26 = load i32*, i32** %2
%27 = call i32 @sem_wait(i32* %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %52
31: ; preds = %25
%32 = load i32, i32* %4
%33 = add nsw i32 %32, -1
store i32 %33, i32* %4
br label %34
34: ; preds = %31
br label %22
35: ; preds = %22
%36 = load i32*, i32** %2
%37 = call i32 @sem_getvalue(i32* %36, i32* %5)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %52
41: ; preds = %35
%42 = load i32, i32* %5
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %41
%45 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%46 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%47 = call i32 @sem_unlink(i8* %46)
%48 = load i32*, i32** %2
%49 = call i32 @sem_close(i32* %48)
store i32 0, i32* %1
br label %52
50: ; preds = %41
%51 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %52
52: ; preds = %50, %44, %39, %29, %19
%53 = load i32, i32* %1
ret i32 %53
}

LLVM IR (-O2):
@.str = [17 x i8] c"/sem_wait_1-2_%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [41 x i8] c"unexpected error: sem_wait 1-2: sem_open\00"
@.str.2 = [45 x i8] c"unexpected error: sem_wait 1-2: sem_getvalue\00"
@.str.3 = [12 x i8] c"TEST PASSED\00"
@.str.4 = [37 x i8] c"TEST FAILED: Semaphore is not locked\00"
define i32 @main() {
%1 = alloca [28 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [28 x i8], [28 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @getpid()
%6 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = load i32, i32* @O_CREAT
%8 = call i32* @sem_open(i8* %3, i32 %7, i32 511, i32 10)
%9 = load i32*, i32** @SEM_FAILED
%10 = icmp eq i32* %8, %9
%11 = icmp eq i32* %8, null
%12 = or i1 %11, %10
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = call i32 @sem_wait(i32* %8)
%15 = icmp eq i32 %14, -1
br i1 %15, label %48, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %61
18: ; preds = %13
%19 = call i32 @sem_wait(i32* %8)
%20 = icmp eq i32 %19, -1
br i1 %20, label %48, label %21
21: ; preds = %18
%22 = call i32 @sem_wait(i32* %8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %48, label %24
24: ; preds = %21
%25 = call i32 @sem_wait(i32* %8)
%26 = icmp eq i32 %25, -1
br i1 %26, label %48, label %27
27: ; preds = %24
%28 = call i32 @sem_wait(i32* %8)
%29 = icmp eq i32 %28, -1
br i1 %29, label %48, label %30
30: ; preds = %27
%31 = call i32 @sem_wait(i32* %8)
%32 = icmp eq i32 %31, -1
br i1 %32, label %48, label %33
33: ; preds = %30
%34 = call i32 @sem_wait(i32* %8)
%35 = icmp eq i32 %34, -1
br i1 %35, label %48, label %36
36: ; preds = %33
%37 = call i32 @sem_wait(i32* %8)
%38 = icmp eq i32 %37, -1
br i1 %38, label %48, label %39
39: ; preds = %36
%40 = call i32 @sem_wait(i32* %8)
%41 = icmp eq i32 %40, -1
br i1 %41, label %48, label %42
42: ; preds = %39
%43 = call i32 @sem_wait(i32* %8)
%44 = icmp eq i32 %43, -1
br i1 %44, label %48, label %45
45: ; preds = %42
%46 = call i32 @sem_getvalue(i32* %8, i32* %2)
%47 = icmp eq i32 %46, -1
br i1 %47, label %50, label %52
48: ; preds = %42, %39, %36, %33, %30, %27, %24, %21, %18, %13
%49 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %61
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0))
br label %61
52: ; preds = %45
%53 = load i32, i32* %2
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @sem_unlink(i8* %3)
%58 = call i32 @sem_close(i32* %8)
br label %61
59: ; preds = %52
%60 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
br label %61
61: ; preds = %59, %55, %50, %48, %16
%62 = phi i32 [ 2, %16 ], [ 2, %48 ], [ 2, %50 ], [ 0, %55 ], [ 1, %59 ]
ret i32 %62
}