#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_FAILURE;
int /*<<< orphan*/ GETVAL;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sem_getval(int semid) {
  int ret = semctl(semid, 0, GETVAL, 0);
  if (ret == -1)
    do {
      perror("semctl");
      exit(EXIT_FAILURE);
    } while (0);
  printf("current val is %d\n", ret);
  return ret;
}


x86 assembly (clang, -O0):
<sem_getval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1050 <semctl@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    118c <sem_getval+0x4c>
jmp    1172 <sem_getval+0x32>
lea    0xe87(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sem_getval>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1050 <semctl@plt>
cmp    $0xffffffff,%eax
je     116c <sem_getval+0x2c>
mov    %eax,%ebx
lea    0xea6(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe8d(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@GETVAL = global i32 0
@.str = [7 x i8] c"semctl\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [19 x i8] c"current val is %d\0A\00"
define i32 @sem_getval(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = load i32, i32* @GETVAL
%6 = call i32 @semctl(i32 %4, i32 0, i32 %5, i32 0)
store i32 %6, i32* %3
%7 = load i32, i32* %3
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %15
9: ; preds = %1
br label %10
10: ; preds = %9
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; No predecessors!
br label %15
15: ; preds = %14, %1
%16 = load i32, i32* %3
%17 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %3
ret i32 %18
}

LLVM IR (-O2):
@GETVAL = global i32 0
@.str = [7 x i8] c"semctl\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [19 x i8] c"current val is %d\0A\00"
define i32 @sem_getval(i32 %0) {
%2 = load i32, i32* @GETVAL
%3 = tail call i32 @semctl(i32 %0, i32 0, i32 %2, i32 0)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %1
%10 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %3)
ret i32 %3
}