#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 abs(int);
int /*<<< orphan*/ read(int, char *, int);
int read_random(int random_fd, int total, char *people) {
  int dog;
  char random_bytes[sizeof(int)];
  do {
    read(random_fd, random_bytes, sizeof(int));
    dog = abs(*((int *)random_bytes) % total);
  } while (people[dog]);
  people[dog] = 1;
  return dog;
}


x86 assembly (clang, -O0):
<read_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
mov    -0x18(%rbp),%eax
cltd
idivl  -0x8(%rbp)
mov    %edx,%edi
call   1030 <abs@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
jne    1132 <read_random+0x12>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_random>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x4(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <read@plt>
mov    0x4(%rsp),%eax
cltd
idiv   %ebp
mov    %edx,%eax
neg    %eax
cmovs  %edx,%eax
cmpb   $0x0,(%r14,%rax,1)
jne    1130 <read_random+0x20>
mov    %eax,%ecx
movb   $0x1,(%r14,%rcx,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_random(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [4 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
br label %9
9: ; preds = %19, %3
%10 = load i32, i32* %4
%11 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 4)
%13 = getelementptr inbounds [4 x i8], [4 x i8]* %8, i64 0, i64 0
%14 = bitcast i8* %13 to i32*
%15 = load i32, i32* %14
%16 = load i32, i32* %5
%17 = srem i32 %15, %16
%18 = call i32 @abs(i32 %17)
store i32 %18, i32* %7
br label %19
19: ; preds = %9
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i8, i8* %23
%25 = icmp ne i8 %24, 0
br i1 %25, label %9, label %26
26: ; preds = %19
%27 = load i8*, i8** %6
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 1, i8* %30
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
define i32 @read_random(i32 %0, i32 %1, i8* nocapture %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
br label %6
6: ; preds = %6, %3
%7 = call i32 @read(i32 %0, i8* %5, i32 4)
%8 = load i32, i32* %4
%9 = srem i32 %8, %1
%10 = tail call i32 @llvm.abs.i32(i32 %9, i1 true)
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %2, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 0
br i1 %14, label %15, label %6
15: ; preds = %6
%16 = zext i32 %10 to i64
%17 = getelementptr inbounds i8, i8* %2, i64 %16
store i8 1, i8* %17
ret i32 %10
}