#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*/ del_sem(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sem_down(int);
int sem_get(int);
int /*<<< orphan*/ sem_up(int);
int /*<<< orphan*/ set_sem(int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  int sem_id = sem_get(12);
  if (argc > 1 && (!set_sem(sem_id))) {
    printf("set sem failed.\n");
    return -1;
  }
  sem_down(sem_id);
  printf("sem lock...\n");
  printf("do something...\n");
  sleep(10);
  sem_up(sem_id);
  printf("sem unlock...\n");
  if (argc == 1)
    del_sem(sem_id);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0xc,%edi
call   1080 <sem_get@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11c6 <main+0x56>
mov    -0x14(%rbp),%edi
call   1040 <set_sem@plt>
cmp    $0x0,%eax
jne    11c6 <main+0x56>
lea    0xe4b(%rip),%rdi
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121d <main+0xad>
mov    -0x14(%rbp),%edi
call   1030 <sem_down@plt>
lea    0xe3c(%rip),%rdi
call   1070 <printf@plt>
lea    0xe3d(%rip),%rdi
call   1070 <printf@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    -0x14(%rbp),%edi
call   1050 <sem_up@plt>
lea    0xe30(%rip),%rdi
call   1070 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jne    1216 <main+0xa6>
mov    -0x14(%rbp),%edi
call   1060 <del_sem@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
mov    $0xc,%edi
call   1080 <sem_get@plt>
mov    %eax,%ebp
cmp    $0x2,%r14d
jl     1194 <main+0x24>
mov    %ebp,%edi
call   1050 <set_sem@plt>
test   %eax,%eax
je     11e1 <main+0x71>
mov    %ebp,%edi
call   1030 <sem_down@plt>
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
lea    0xe5e(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1090 <sleep@plt>
mov    %ebp,%edi
call   1060 <sem_up@plt>
lea    0xe51(%rip),%rdi
call   1040 <puts@plt>
xor    %ebx,%ebx
cmp    $0x1,%r14d
jne    11f2 <main+0x82>
mov    %ebp,%edi
call   1070 <del_sem@plt>
jmp    11f2 <main+0x82>
lea    0xe42(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"set sem failed.\0A\00"
@.str.1 = [13 x i8] c"sem lock...\0A\00"
@.str.2 = [17 x i8] c"do something...\0A\00"
@.str.3 = [15 x i8] c"sem unlock...\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i32 @sem_get(i32 12)
store i32 %7, i32* %6
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i32, i32* %6
%12 = call i32 @set_sem(i32 %11)
%13 = icmp ne i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %10
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %31
16: ; preds = %10, %2
%17 = load i32, i32* %6
%18 = call i32 @sem_down(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%21 = call i32 @sleep(i32 10)
%22 = load i32, i32* %6
%23 = call i32 @sem_up(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %30
27: ; preds = %16
%28 = load i32, i32* %6
%29 = call i32 @del_sem(i32 %28)
br label %30
30: ; preds = %27, %16
store i32 0, i32* %3
br label %31
31: ; preds = %30, %14
%32 = load i32, i32* %3
ret i32 %32
}

LLVM IR (-O2):
@str = [12 x i8] c"sem lock...\00"
@str.4 = [16 x i8] c"do something...\00"
@str.5 = [14 x i8] c"sem unlock...\00"
@str.6 = [16 x i8] c"set sem failed.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @sem_get(i32 12)
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = tail call i32 @set_sem(i32 %3)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
br label %20
10: ; preds = %5, %2
%11 = tail call i32 @sem_down(i32 %3)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.4, i64 0, i64 0))
%14 = tail call i32 @sleep(i32 10)
%15 = tail call i32 @sem_up(i32 %3)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
%17 = icmp eq i32 %0, 1
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @del_sem(i32 %3)
br label %20
20: ; preds = %10, %18, %8
%21 = phi i32 [ -1, %8 ], [ 0, %18 ], [ 0, %10 ]
ret i32 %21
}