#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*/ binary_semaphore_post(int);
int /*<<< orphan*/ binary_semaphore_wait(int);
int getpid();
int /*<<< orphan*/ printf(char *, int, int);
scalar_t__ shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
void print_hundred(int semid, int segment_id) {
  char *shared_memory;
  int number;
  shared_memory = (char *)shmat(segment_id, 0, 0);
  binary_semaphore_wait(semid);
  sscanf(shared_memory, "%d", &number);
  number++;
  sprintf(shared_memory, "%d", number);
  sscanf(shared_memory, "%d", &number);
  number--;
  sprintf(shared_memory, "%d", number);
  sscanf(shared_memory, "%d", &number);
  printf("Number: %d - PID: %d\n", number, getpid());
  binary_semaphore_post(semid);
  shmdt(shared_memory);
}


x86 assembly (clang, -O0):
<print_hundred>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1090 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <binary_semaphore_wait@plt>
mov    -0x10(%rbp),%rdi
lea    0xe4f(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe2f(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xe1f(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdff(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    0xdef(%rip),%rsi
lea    -0x14(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xdd0(%rip),%rdi
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   1070 <binary_semaphore_post@plt>
mov    -0x10(%rbp),%rdi
call   1030 <shmdt@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_hundred>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
mov    %esi,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,%rbx
mov    %r14d,%edi
call   1050 <binary_semaphore_wait@plt>
lea    0xe59(%rip),%rbp
lea    0x4(%rsp),%r15
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1080 <sscanf@plt>
mov    0x4(%rsp),%edx
add    $0x1,%edx
mov    %edx,0x4(%rsp)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1080 <sscanf@plt>
mov    0x4(%rsp),%edx
add    $0xffffffff,%edx
mov    %edx,0x4(%rsp)
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1080 <sscanf@plt>
mov    0x4(%rsp),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdef(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1060 <printf@plt>
mov    %r14d,%edi
call   1070 <binary_semaphore_post@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [22 x i8] c"Number: %d - PID: %d\0A\00"
define void @print_hundred(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = call i64 @shmat(i32 %7, i32 0, i32 0)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %5
%10 = load i32, i32* %3
%11 = call i32 @binary_semaphore_wait(i32 %10)
%12 = load i8*, i8** %5
%13 = call i32 @sscanf(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%14 = load i32, i32* %6
%15 = add nsw i32 %14, 1
store i32 %15, i32* %6
%16 = load i8*, i8** %5
%17 = load i32, i32* %6
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i8*, i8** %5
%20 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%21 = load i32, i32* %6
%22 = add nsw i32 %21, -1
store i32 %22, i32* %6
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = load i8*, i8** %5
%27 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %6)
%28 = load i32, i32* %6
%29 = call i32 (...) @getpid()
%30 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %28, i32 %29)
%31 = load i32, i32* %3
%32 = call i32 @binary_semaphore_post(i32 %31)
%33 = load i8*, i8** %5
%34 = call i32 @shmdt(i8* %33)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [22 x i8] c"Number: %d - PID: %d\0A\00"
define void @print_hundred(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 @shmat(i32 %1, i32 0, i32 0)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i32 @binary_semaphore_wait(i32 %0)
%8 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%9 = load i32, i32* %3
%10 = add nsw i32 %9, 1
store i32 %10, i32* %3
%11 = call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%13 = load i32, i32* %3
%14 = add nsw i32 %13, -1
store i32 %14, i32* %3
%15 = call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%17 = load i32, i32* %3
%18 = call i32 (...) @getpid()
%19 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
%20 = call i32 @binary_semaphore_post(i32 %0)
%21 = call i32 @shmdt(i8* %6)
ret void
}