#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, int);
void uuid_generate(unsigned char *uuid) {
  int fd;
  fd = open("/dev/urandom", O_RDONLY);
  if (-1 == fd) {
    perror("Error while opening "
           "/dev/urandom");
    return;
  }
  if (16 != read(fd, uuid, 16)) {
    perror("Error while reading from "
           "/dev/urandom");
  }
  uuid[6] &= 0x0F;
  uuid[6] |= 0x40;
  uuid[8] &= 0x3F;
  uuid[8] |= 0x80;
  close(fd);
}


x86 assembly (clang, -O0):
<uuid_generate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    1183 <uuid_generate+0x43>
lea    0xe87(%rip),%rdi
call   1060 <perror@plt>
jmp    11f2 <uuid_generate+0xb2>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    $0x10,%eax
cmp    %ecx,%eax
je     11af <uuid_generate+0x6f>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%rax
movzbl 0x6(%rax),%ecx
and    $0xf,%ecx
mov    %cl,0x6(%rax)
mov    -0x8(%rbp),%rax
movzbl 0x6(%rax),%ecx
or     $0x40,%ecx
mov    %cl,0x6(%rax)
mov    -0x8(%rbp),%rax
movzbl 0x8(%rax),%ecx
and    $0x3f,%ecx
mov    %cl,0x8(%rax)
mov    -0x8(%rbp),%rax
movzbl 0x8(%rax),%ecx
or     $0x80,%ecx
mov    %cl,0x8(%rax)
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uuid_generate>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0xebe(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1199 <uuid_generate+0x59>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1040 <read@plt>
cmp    $0x10,%eax
jne    11ab <uuid_generate+0x6b>
mov    0x6(%rbx),%al
mov    0x8(%rbx),%cl
and    $0xf,%al
or     $0x40,%al
mov    %al,0x6(%rbx)
and    $0x3f,%cl
or     $0x80,%cl
mov    %cl,0x8(%rbx)
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <close@plt>
lea    0xe60(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <perror@plt>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
jmp    1176 <uuid_generate+0x36>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [33 x i8] c"Error while opening /dev/urandom\00"
@.str.2 = [38 x i8] c"Error while reading from /dev/urandom\00"
define void @uuid_generate(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i32, i32* @O_RDONLY
%5 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 -1, %6
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %44
10: ; preds = %1
%11 = load i32, i32* %3
%12 = load i8*, i8** %2
%13 = call i32 @read(i32 %11, i8* %12, i32 16)
%14 = icmp ne i32 16, %13
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = load i8*, i8** %2
%19 = getelementptr inbounds i8, i8* %18, i64 6
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = and i32 %21, 15
%23 = trunc i32 %22 to i8
store i8 %23, i8* %19
%24 = load i8*, i8** %2
%25 = getelementptr inbounds i8, i8* %24, i64 6
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = or i32 %27, 64
%29 = trunc i32 %28 to i8
store i8 %29, i8* %25
%30 = load i8*, i8** %2
%31 = getelementptr inbounds i8, i8* %30, i64 8
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = and i32 %33, 63
%35 = trunc i32 %34 to i8
store i8 %35, i8* %31
%36 = load i8*, i8** %2
%37 = getelementptr inbounds i8, i8* %36, i64 8
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = or i32 %39, 128
%41 = trunc i32 %40 to i8
store i8 %41, i8* %37
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
br label %44
44: ; preds = %17, %8
ret void
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.1 = [33 x i8] c"Error while opening /dev/urandom\00"
@.str.2 = [38 x i8] c"Error while reading from /dev/urandom\00"
define void @uuid_generate(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %22
7: ; preds = %1
%8 = tail call i32 @read(i32 %3, i8* %0, i32 16)
%9 = icmp eq i32 %8, 16
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = getelementptr inbounds i8, i8* %0, i64 6
%14 = load i8, i8* %13
%15 = and i8 %14, 15
%16 = or i8 %15, 64
store i8 %16, i8* %13
%17 = getelementptr inbounds i8, i8* %0, i64 8
%18 = load i8, i8* %17
%19 = and i8 %18, 63
%20 = or i8 %19, -128
store i8 %20, i8* %17
%21 = tail call i32 @close(i32 %3)
br label %22
22: ; preds = %12, %5
ret void
}