#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 */

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int mysem_init(int);
int /*<<< orphan*/ mysem_wait(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main() {
  int semid;
  semid = mysem_init(1234);
  if (semid < 0) {
    perror("sem open failed");
    exit(1);
  }
  printf("semid=%d\n", semid);
  while (1) {
    mysem_wait(semid);
    printf("i got semaphore\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x4d2,%edi
call   1030 <mysem_init@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    118c <main+0x3c>
lea    0xe83(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <mysem_wait@plt>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    119d <main+0x4d>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x4d2,%edi
call   1030 <mysem_init@plt>
test   %eax,%eax
js     11a1 <main+0x41>
mov    %eax,%ebx
lea    0xe95(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe8f(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
call   1060 <mysem_wait@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
jmp    1190 <main+0x30>
lea    0xe58(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"sem open failed\00"
@.str.1 = [10 x i8] c"semid=%d\0A\00"
@.str.2 = [17 x i8] c"i got semaphore\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 @mysem_init(i32 1234)
store i32 %3, i32* %2
%4 = load i32, i32* %2
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = load i32, i32* %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %9, %12
%13 = load i32, i32* %2
%14 = call i32 @mysem_wait(i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %12
}

LLVM IR (-O2):
@.str = [16 x i8] c"sem open failed\00"
@.str.1 = [10 x i8] c"semid=%d\0A\00"
@str = [16 x i8] c"i got semaphore\00"
define i32 @main() {
%1 = tail call i32 @mysem_init(i32 1234)
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %8
8: ; preds = %8, %6
%9 = tail call i32 @mysem_wait(i32 %1)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %8
}