#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*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ printf(char *, int);
int semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int sem_getval(int semid) {
  int ret = semctl(semid, 0, GETVAL, 0);
  if (ret == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day0b/shmfifo/ipc.c",
              72, "semctl", strerror(errno));
      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    0x2e63(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
call   1050 <semctl@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11cf <sem_getval+0x7f>
jmp    1182 <sem_getval+0x32>
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0xc(%rbp),%edi
mov    %rax,%r9
lea    0xe57(%rip),%rsi
lea    0xe60(%rip),%rdx
mov    $0x48,%ecx
lea    0xeee(%rip),%r8
call   1040 <fprintf@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xed8(%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    0x2e70(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1050 <semctl@plt>
cmp    $0xffffffff,%eax
je     117c <sem_getval+0x2c>
mov    %eax,%ebx
lea    0xf40(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret
mov    0x2e5d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe66(%rip),%rsi
lea    0xe6f(%rip),%rdx
lea    0xf02(%rip),%r8
mov    %ebx,%edi
mov    $0x48,%ecx
mov    %rax,%r9
call   1040 <fprintf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@GETVAL = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [154 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day0b/shmfifo/ipc.c\00"
@.str.2 = [7 x i8] c"semctl\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [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 %18
9: ; preds = %1
br label %10
10: ; preds = %9
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([154 x i8], [154 x i8]* @.str.1, i64 0, i64 0), i32 72, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; No predecessors!
br label %18
18: ; preds = %17, %1
%19 = load i32, i32* %3
%20 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %3
ret i32 %21
}

LLVM IR (-O2):
@GETVAL = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [154 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day0b/shmfifo/ipc.c\00"
@.str.2 = [7 x i8] c"semctl\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [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 %12
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([154 x i8], [154 x i8]* @.str.1, i64 0, i64 0), i32 72, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %3)
ret i32 %3
}