#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*/ printf(char *, int);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int,
                             int /*<<< orphan*/);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int main() {
  sem_t *psem = sem_open("/uplooking", O_CREAT, 0666, 0);
  if (psem == SEM_FAILED) {
    perror("sem_open");
    goto err;
  }
  printf("pid %d sem_post\n", getpid());
  sem_post(psem);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
jne    11a4 <main+0x54>
lea    0xe71(%rip),%rdi
call   1070 <perror@plt>
jmp    11ce <main+0x7e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe60(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <sem_post@plt>
movl   $0x0,-0x4(%rbp)
jmp    11d5 <main+0x85>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
lea    0xe9d(%rip),%rdi
xor    %ebp,%ebp
mov    $0x1b6,%edx
xor    %ecx,%ecx
call   1060 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e65(%rip),%rax
cmp    (%rax),%rbx
je     11a6 <main+0x56>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe86(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <sem_post@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    119d <main+0x4d>

LLVM IR (-O0):
@.str = [11 x i8] c"/uplooking\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@.str.2 = [17 x i8] c"pid %d sem_post\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
%3 = load i32, i32* @O_CREAT
%4 = call i32* @sem_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438, i32 0)
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = load i32*, i32** @SEM_FAILED
%7 = icmp eq i32* %5, %6
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %15
10: ; preds = %0
%11 = call i32 (...) @getpid()
%12 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i32*, i32** %2
%14 = call i32 @sem_post(i32* %13)
store i32 0, i32* %1
br label %16
15: ; preds = %8
store i32 -1, i32* %1
br label %16
16: ; preds = %15, %10
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
@.str = [11 x i8] c"/uplooking\00"
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@.str.2 = [17 x i8] c"pid %d sem_post\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = tail call i32* @sem_open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %1, i32 438, i32 0)
%3 = load i32*, i32** @SEM_FAILED
%4 = icmp eq i32* %2, %3
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %11
7: ; preds = %0
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = tail call i32 @sem_post(i32* %2)
br label %11
11: ; preds = %5, %7
%12 = phi i32 [ -1, %5 ], [ 0, %7 ]
ret i32 %12
}