#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*/ printf(char *, ...);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int main(void) {
  int i;
  sem_t *coin, *lock;
  lock = sem_open("/my_semaphor_2_name_simple_seq", 0);
  coin = sem_open("/my_semaphor_1_name_simple_seq", 0);
  printf("\nCoffee teller machine starting\n\n");
  i = 0;
  while (i < 100 * 1000 * 1000) {
    sem_wait(lock);
    printf("(%d) teller: dispense coffee & unlock\n", i);
    i++;
    sem_post(coin);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
xor    %esi,%esi
call   1060 <sem_open@plt>
mov    %rax,-0x18(%rbp)
lea    0xeb7(%rip),%rdi
xor    %esi,%esi
call   1060 <sem_open@plt>
mov    %rax,-0x10(%rbp)
lea    0xec4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x5f5e100,-0x8(%rbp)
jge    11c6 <main+0x86>
mov    -0x18(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x8(%rbp),%esi
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <sem_post@plt>
jmp    1188 <main+0x48>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0xea2(%rip),%rdi
xor    %esi,%esi
call   1070 <sem_open@plt>
mov    %rax,%r14
lea    0xeb0(%rip),%rdi
xor    %esi,%esi
call   1070 <sem_open@plt>
mov    %rax,%rbx
lea    0xee5(%rip),%rdi
call   1040 <puts@plt>
lea    0xeb2(%rip),%r15
xor    %ebp,%ebp
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <sem_wait@plt>
mov    %r15,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1060 <sem_post@plt>
cmp    $0x5f5e100,%ebp
jne    1190 <main+0x40>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"/my_semaphor_2_name_simple_seq\00"
@.str.1 = [31 x i8] c"/my_semaphor_1_name_simple_seq\00"
@.str.2 = [34 x i8] c"\0ACoffee teller machine starting\0A\0A\00"
@.str.3 = [39 x i8] c"(%d) teller: dispense coffee & unlock\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i32* @sem_open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 0)
store i32* %5, i32** %4
%6 = call i32* @sem_open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32* %6, i32** %3
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %8
8: ; preds = %11, %0
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 100000000
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = load i32*, i32** %4
%13 = call i32 @sem_wait(i32* %12)
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
%18 = load i32*, i32** %3
%19 = call i32 @sem_post(i32* %18)
br label %8
20: ; preds = %8
%21 = load i32, i32* %1
ret i32 %21
}

LLVM IR (-O2):
@.str = [31 x i8] c"/my_semaphor_2_name_simple_seq\00"
@.str.1 = [31 x i8] c"/my_semaphor_1_name_simple_seq\00"
@.str.3 = [39 x i8] c"(%d) teller: dispense coffee & unlock\0A\00"
@str = [33 x i8] c"\0ACoffee teller machine starting\0A\00"
define i32 @main() {
%1 = tail call i32* @sem_open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 0)
%2 = tail call i32* @sem_open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 0)
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
br label %4
4: ; preds = %0, %4
%5 = phi i32 [ 0, %0 ], [ %8, %4 ]
%6 = tail call i32 @sem_wait(i32* %1)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%8 = add nuw nsw i32 %5, 1
%9 = tail call i32 @sem_post(i32* %2)
%10 = icmp eq i32 %8, 100000000
br i1 %10, label %11, label %4
11: ; preds = %4
ret i32 0
}