#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 /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/);
scalar_t__ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_unlink(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int main(void) {
  sem_t *mysemp;
  char semname[28];
  sprintf(semname,
          "/"
          "sem_post"
          "_"
          "4-1"
          "_%d",
          getpid());
  mysemp = sem_open(semname, O_CREAT, 0777, 0);
  if (mysemp == SEM_FAILED || mysemp == NULL) {
    perror("unexpected error: "
           "sem_post"
           " "
           "4-1"
           ": "
           "sem_open");
    return 2;
  }
  if (sem_post(mysemp) == 0) {
    puts("TEST PASSED");
    sem_close(mysemp);
    sem_unlink(semname);
    return 0;
  }
  puts("TEST FAILED: value of sem_post is not returning zero");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
lea    0xe55(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x30(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1060 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e08(%rip),%rcx
cmp    (%rcx),%rax
je     11ec <main+0x6c>
cmpq   $0x0,-0x10(%rbp)
jne    1204 <main+0x84>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1254 <main+0xd4>
mov    -0x10(%rbp),%rdi
call   1050 <sem_post@plt>
cmp    $0x0,%rax
jne    1241 <main+0xc1>
lea    0xe1c(%rip),%rdi
call   1030 <puts@plt>
mov    -0x10(%rbp),%rdi
call   1070 <sem_close@plt>
lea    -0x30(%rbp),%rdi
call   10a0 <sem_unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    1254 <main+0xd4>
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe6d(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %eax,%edx
call   1090 <sprintf@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x1ff,%edx
xor    %ecx,%ecx
call   1060 <sem_open@plt>
test   %rax,%rax
je     1210 <main+0x90>
mov    %rax,%rbx
mov    0x2e19(%rip),%rax
cmp    (%rax),%rbx
je     1210 <main+0x90>
mov    %rbx,%rdi
call   1050 <sem_post@plt>
test   %rax,%rax
je     11ec <main+0x6c>
lea    0xe66(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    120a <main+0x8a>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1070 <sem_close@plt>
mov    %rsp,%rdi
call   10a0 <sem_unlink@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xdfa(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%eax
jmp    120a <main+0x8a>

LLVM IR (-O0):
@.str = [17 x i8] c"/sem_post_4-1_%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [41 x i8] c"unexpected error: sem_post 4-1: sem_open\00"
@.str.2 = [12 x i8] c"TEST PASSED\00"
@.str.3 = [53 x i8] c"TEST FAILED: value of sem_post is not returning zero\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [28 x i8]
store i32 0, i32* %1
%4 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%5 = call i32 (...) @getpid()
%6 = call i32 @sprintf(i8* %4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%8 = load i32, i32* @O_CREAT
%9 = call i32* @sem_open(i8* %7, i32 %8, i32 511, i32 0)
store i32* %9, i32** %2
%10 = load i32*, i32** %2
%11 = load i32*, i32** @SEM_FAILED
%12 = icmp eq i32* %10, %11
br i1 %12, label %16, label %13
13: ; preds = %0
%14 = load i32*, i32** %2
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13, %0
%17 = 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 %30
18: ; preds = %13
%19 = load i32*, i32** %2
%20 = call i64 @sem_post(i32* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32*, i32** %2
%25 = call i32 @sem_close(i32* %24)
%26 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%27 = call i32 @sem_unlink(i8* %26)
store i32 0, i32* %1
br label %30
28: ; preds = %18
%29 = call i32 @puts(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %30
30: ; preds = %28, %22, %16
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@.str = [17 x i8] c"/sem_post_4-1_%d\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [41 x i8] c"unexpected error: sem_post 4-1: sem_open\00"
@.str.2 = [12 x i8] c"TEST PASSED\00"
@.str.3 = [53 x i8] c"TEST FAILED: value of sem_post is not returning zero\00"
define i32 @main() {
%1 = alloca [28 x i8]
%2 = getelementptr inbounds [28 x i8], [28 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (...) @getpid()
%4 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = load i32, i32* @O_CREAT
%6 = call i32* @sem_open(i8* %2, i32 %5, i32 511, i32 0)
%7 = load i32*, i32** @SEM_FAILED
%8 = icmp eq i32* %6, %7
%9 = icmp eq i32* %6, null
%10 = or i1 %9, %8
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
br label %22
13: ; preds = %0
%14 = call i64 @sem_post(i32* %6)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @sem_close(i32* %6)
%19 = call i32 @sem_unlink(i8* %2)
br label %22
20: ; preds = %13
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %16, %11
%23 = phi i32 [ 2, %11 ], [ 0, %16 ], [ 1, %20 ]
ret i32 %23
}