#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, j;
  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++;
    for (j = 0; j < 3; j++) {
      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,-0x20(%rbp)
lea    0xeb7(%rip),%rdi
xor    %esi,%esi
call   1060 <sem_open@plt>
mov    %rax,-0x18(%rbp)
lea    0xec4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x5f5e100,-0x8(%rbp)
jge    11e5 <main+0xa5>
mov    -0x20(%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)
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    11e0 <main+0xa0>
mov    -0x18(%rbp),%rdi
call   1050 <sem_post@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11bf <main+0x7f>
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>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
add    $0x1,%ebp
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*
%5 = alloca i32*
store i32 0, i32* %1
%6 = call i32* @sem_open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 0)
store i32* %6, i32** %5
%7 = call i32* @sem_open(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32* %7, i32** %4
%8 = 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 %9
9: ; preds = %28, %0
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 100000000
br i1 %11, label %12, label %29
12: ; preds = %9
%13 = load i32*, i32** %5
%14 = call i32 @sem_wait(i32* %13)
%15 = load i32, i32* %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = load i32, i32* %2
%18 = add nsw i32 %17, 1
store i32 %18, i32* %2
store i32 0, i32* %3
br label %19
19: ; preds = %25, %12
%20 = load i32, i32* %3
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32*, i32** %4
%24 = call i32 @sem_post(i32* %23)
br label %25
25: ; preds = %22
%26 = load i32, i32* %3
%27 = add nsw i32 %26, 1
store i32 %27, i32* %3
br label %19
28: ; preds = %19
br label %9
29: ; preds = %9
%30 = load i32, i32* %1
ret i32 %30
}

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 ], [ %11, %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 = tail call i32 @sem_post(i32* %2)
%9 = tail call i32 @sem_post(i32* %2)
%10 = tail call i32 @sem_post(i32* %2)
%11 = add nuw nsw i32 %5, 1
%12 = icmp eq i32 %11, 100000000
br i1 %12, label %13, label %4
13: ; preds = %4
ret i32 0
}