#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 */
typedef int /*<<< orphan*/ sem_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_CREAT;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ assert(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, int);
int sem_init(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int strlen(char const *);
int /*<<< orphan*/ write(int, char const *, int);
int main(int argc, char *argv[]) {
  const char *message = "Witaj świecie\n";
  int n = strlen(message);
  int odd = n & 1;
  n /= 2;
  printf("%i\n", n);
  printf("%i\n", odd);
  sem_t *sem = sem_open("/thesem", O_CREAT, S_IRUSR | S_IWUSR);
  assert(sem != NULL);
  int r = sem_init(sem, 1, 0);
  assert(r == 0);
  pid_t pid = fork();
  int i0 = (pid == 0) ? 0 : n + odd;
  int i;
  if (pid)
    sem_wait(sem);
  int gr = 0;
  for (i = 0; i < n; i++) {
    write(1, message + i0 + i, 1);
  }
  if (pid == 0)
    sem_post(sem);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
and    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe32(%rip),%rdi
call   1060 <printf@plt>
mov    -0x20(%rbp),%esi
lea    0xe23(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    0x2dd5(%rip),%rax
or     (%rax),%edx
lea    0xe00(%rip),%rdi
call   1080 <sem_open@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   10a0 <sem_init@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1273 <main+0xe3>
xor    %eax,%eax
mov    %eax,-0x48(%rbp)
jmp    127c <main+0xec>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1296 <main+0x106>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12dc <main+0x14c>
mov    -0x18(%rbp),%rsi
movslq -0x3c(%rbp),%rax
add    %rax,%rsi
movslq -0x40(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
mov    %edx,%edi
call   1040 <write@plt>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    12a4 <main+0x114>
cmpq   $0x0,-0x38(%rbp)
jne    12f0 <main+0x160>
mov    -0x28(%rbp),%rdi
call   1070 <sem_post@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe5e(%rip),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %eax,%r12d
mov    %eax,%r15d
and    $0x1,%r15d
mov    %eax,%ebp
shr    $0x1f,%ebp
add    %eax,%ebp
sar    %ebp
lea    0xe4c(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <printf@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1060 <printf@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe19(%rip),%rdi
call   1080 <sem_open@plt>
mov    %rax,%r14
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1090 <assert@plt>
mov    %r14,%rdi
mov    $0x1,%esi
xor    %edx,%edx
call   10a0 <sem_init@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1090 <assert@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%r13
test   %rax,%rax
je     1244 <main+0xb4>
mov    %r14,%rdi
call   1030 <sem_wait@plt>
cmp    $0x2,%r12d
jl     127c <main+0xec>
add    %ebp,%r15d
test   %r13,%r13
movslq %r15d,%rax
cmove  %r13,%rax
mov    %ebp,%ebp
add    %rax,%rbx
nopl   0x0(%rax)
mov    $0x1,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0x1,%rbx
add    $0xffffffffffffffff,%rbp
jne    1260 <main+0xd0>
test   %r13,%r13
jne    1289 <main+0xf9>
mov    %r14,%rdi
call   1070 <sem_post@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Witaj \C5\9Bwiecie\0A\00"
@.str.1 = [4 x i8] c"%i\0A\00"
@.str.2 = [8 x i8] c"/thesem\00"
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %6
%15 = load i8*, i8** %6
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = and i32 %17, 1
store i32 %18, i32* %8
%19 = load i32, i32* %7
%20 = sdiv i32 %19, 2
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %8
%24 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @O_CREAT
%26 = load i32, i32* @S_IRUSR
%27 = load i32, i32* @S_IWUSR
%28 = or i32 %26, %27
%29 = call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %28)
store i32* %29, i32** %9
%30 = load i32*, i32** %9
%31 = icmp ne i32* %30, null
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32*, i32** %9
%35 = call i32 @sem_init(i32* %34, i32 1, i32 0)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp eq i32 %36, 0
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = call i64 (...) @fork()
store i64 %40, i64* %11
%41 = load i64, i64* %11
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %2
br label %48
44: ; preds = %2
%45 = load i32, i32* %7
%46 = load i32, i32* %8
%47 = add nsw i32 %45, %46
br label %48
48: ; preds = %44, %43
%49 = phi i32 [ 0, %43 ], [ %47, %44 ]
store i32 %49, i32* %12
%50 = load i64, i64* %11
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i32*, i32** %9
%54 = call i32 @sem_wait(i32* %53)
br label %55
55: ; preds = %52, %48
store i32 0, i32* %14
store i32 0, i32* %13
br label %56
56: ; preds = %69, %55
%57 = load i32, i32* %13
%58 = load i32, i32* %7
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %72
60: ; preds = %56
%61 = load i8*, i8** %6
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %61, i64 %63
%65 = load i32, i32* %13
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = call i32 @write(i32 1, i8* %67, i32 1)
br label %69
69: ; preds = %60
%70 = load i32, i32* %13
%71 = add nsw i32 %70, 1
store i32 %71, i32* %13
br label %56
72: ; preds = %56
%73 = load i64, i64* %11
%74 = icmp eq i64 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32*, i32** %9
%77 = call i32 @sem_post(i32* %76)
br label %78
78: ; preds = %75, %72
ret i32 0
}

LLVM IR (-O2):
@.str = [16 x i8] c"Witaj \C5\9Bwiecie\0A\00"
@.str.1 = [4 x i8] c"%i\0A\00"
@.str.2 = [8 x i8] c"/thesem\00"
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @strlen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%4 = and i32 %3, 1
%5 = sdiv i32 %3, 2
%6 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %4)
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32* @sem_open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 %11)
%13 = icmp ne i32* %12, null
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @sem_init(i32* %12, i32 1, i32 0)
%17 = icmp eq i32 %16, 0
%18 = zext i1 %17 to i32
%19 = tail call i32 @assert(i32 %18)
%20 = tail call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
%22 = add nsw i32 %5, %4
br i1 %21, label %25, label %23
23: ; preds = %2
%24 = tail call i32 @sem_wait(i32* %12)
br label %25
25: ; preds = %23, %2
%26 = icmp sgt i32 %3, 1
br i1 %26, label %27, label %38
27: ; preds = %25
%28 = sext i32 %22 to i64
%29 = select i1 %21, i64 0, i64 %28
%30 = getelementptr inbounds [16 x i8], [16 x i8]* @.str, i64 0, i64 %29
%31 = zext i32 %5 to i64
br label %32
32: ; preds = %27, %32
%33 = phi i64 [ 0, %27 ], [ %36, %32 ]
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = tail call i32 @write(i32 1, i8* %34, i32 1)
%36 = add nuw nsw i64 %33, 1
%37 = icmp eq i64 %36, %31
br i1 %37, label %38, label %32
38: ; preds = %32, %25
br i1 %21, label %39, label %41
39: ; preds = %38
%40 = tail call i32 @sem_post(i32* %12)
br label %41
41: ; preds = %39, %38
ret i32 0
}