#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 */
scalar_t__ EACCES;
scalar_t__ EEXIST;
scalar_t__ EINVAL;
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
int /*<<< orphan*/ rand();
int shmget(int /*<<< orphan*/, int, int);
int /*<<< orphan*/ throw(char *);
int create_segment(int total_size) {
  int id;
  assert(total_size > 0);
  while ((id = shmget(rand(), total_size, IPC_CREAT | IPC_EXCL | 0666)) == -1) {
    if (errno != EEXIST && errno != EINVAL && errno != EACCES) {
      throw("Error creating segment");
    }
  }
  return id;
}


x86 assembly (clang, -O0):
<create_segment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    $0x0,%al
call   1060 <rand@plt>
mov    %eax,%edi
mov    -0x4(%rbp),%esi
mov    0x2e49(%rip),%rax
mov    (%rax),%edx
mov    0x2e78(%rip),%rax
or     (%rax),%edx
or     $0x1b6,%edx
call   1050 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11f0 <create_segment+0xb0>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e2e(%rip),%rcx
cmp    (%rcx),%rax
je     11eb <create_segment+0xab>
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    0x2e04(%rip),%rcx
cmp    (%rcx),%rax
je     11eb <create_segment+0xab>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%rax
je     11eb <create_segment+0xab>
lea    0xe1a(%rip),%rdi
call   1040 <throw@plt>
jmp    115c <create_segment+0x1c>
mov    -0x8(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_segment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %edi,%edi
test   %ebx,%ebx
setg   %dil
call   1030 <assert@plt>
xor    %eax,%eax
call   1060 <rand@plt>
mov    0x2e50(%rip),%r15
mov    (%r15),%edx
mov    0x2e7e(%rip),%r12
or     (%r12),%edx
or     $0x1b6,%edx
mov    %eax,%edi
mov    %ebx,%esi
call   1050 <shmget@plt>
cmp    $0xffffffff,%eax
je     1199 <create_segment+0x59>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e28(%rip),%r13
mov    0x2e29(%rip),%rbp
mov    0x2e12(%rip),%r14
jmp    11d2 <create_segment+0x92>
xor    %eax,%eax
call   1060 <rand@plt>
mov    (%r15),%edx
or     (%r12),%edx
or     $0x1b6,%edx
mov    %eax,%edi
mov    %ebx,%esi
call   1050 <shmget@plt>
cmp    $0xffffffff,%eax
jne    118a <create_segment+0x4a>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     11b0 <create_segment+0x70>
cmp    (%r14),%rax
je     11b0 <create_segment+0x70>
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <create_segment+0x70>
lea    0xe0c(%rip),%rdi
call   1040 <throw@plt>
jmp    11b0 <create_segment+0x70>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@EINVAL = global i64 0
@EACCES = global i64 0
@.str = [23 x i8] c"Error creating segment\00"
define i32 @create_segment(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = icmp sgt i32 %4, 0
%6 = zext i1 %5 to i32
%7 = call i32 @assert(i32 %6)
br label %8
8: ; preds = %31, %1
%9 = call i32 (...) @rand()
%10 = load i32, i32* %2
%11 = load i32, i32* @IPC_CREAT
%12 = load i32, i32* @IPC_EXCL
%13 = or i32 %11, %12
%14 = or i32 %13, 438
%15 = call i32 @shmget(i32 %9, i32 %10, i32 %14)
store i32 %15, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %32
17: ; preds = %8
%18 = load i64, i64* @errno
%19 = load i64, i64* @EEXIST
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %31
21: ; preds = %17
%22 = load i64, i64* @errno
%23 = load i64, i64* @EINVAL
%24 = icmp ne i64 %22, %23
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i64, i64* @errno
%27 = load i64, i64* @EACCES
%28 = icmp ne i64 %26, %27
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @throw(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %29, %25, %21, %17
br label %8
32: ; preds = %8
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@EINVAL = global i64 0
@EACCES = global i64 0
@.str = [23 x i8] c"Error creating segment\00"
define i32 @create_segment(i32 %0) {
%2 = icmp sgt i32 %0, 0
%3 = zext i1 %2 to i32
%4 = tail call i32 @assert(i32 %3)
%5 = tail call i32 (...) @rand()
%6 = load i32, i32* @IPC_CREAT
%7 = load i32, i32* @IPC_EXCL
%8 = or i32 %6, %7
%9 = or i32 %8, 438
%10 = tail call i32 @shmget(i32 %5, i32 %0, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %32
12: ; preds = %1, %24
%13 = load i64, i64* @errno
%14 = load i64, i64* @EEXIST
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @EINVAL
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
%19 = load i64, i64* @EACCES
%20 = icmp eq i64 %13, %19
%21 = select i1 %18, i1 true, i1 %20
br i1 %21, label %24, label %22
22: ; preds = %12
%23 = tail call i32 @throw(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %12
%25 = tail call i32 (...) @rand()
%26 = load i32, i32* @IPC_CREAT
%27 = load i32, i32* @IPC_EXCL
%28 = or i32 %26, %27
%29 = or i32 %28, 438
%30 = tail call i32 @shmget(i32 %25, i32 %0, i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %12, label %32
32: ; preds = %24, %1
%33 = phi i32 [ %10, %1 ], [ %30, %24 ]
ret i32 %33
}