#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*/ EINVAL;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int read(int, unsigned char *, int);
int uuid_new(unsigned char *uuid) {
  int res;
  int fd;
  fd = open("/dev/urandom", O_RDONLY);
  if (fd < 0)
    return -1;
  res = read(fd, uuid, 16);
  close(fd);
  if (res < 0)
    return -1;
  if (res != 16) {
    errno = EINVAL;
    return -1;
  }
  uuid[6] = (uuid[6] & 0x0F) | 0x40;
  uuid[8] = (uuid[8] & 0x3F) | 0x80;
  return 0;
}


x86 assembly (clang, -O0):
<uuid_new>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
lea    0xeb4(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    116a <uuid_new+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <uuid_new+0xcb>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x14(%rbp)
jge    119c <uuid_new+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <uuid_new+0xcb>
cmpl   $0x10,-0x14(%rbp)
je     11c4 <uuid_new+0x94>
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
mov    0x2e22(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <uuid_new+0xcb>
mov    -0x10(%rbp),%rax
movzbl 0x6(%rax),%eax
and    $0xf,%eax
or     $0x40,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x6(%rax)
mov    -0x10(%rbp),%rax
movzbl 0x8(%rax),%eax
and    $0x3f,%eax
or     $0x80,%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,0x8(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uuid_new>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11ad <uuid_new+0x7d>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    $0x10,%edx
call   1040 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
test   %ebx,%ebx
js     11ad <uuid_new+0x7d>
cmp    $0x10,%ebx
jne    119b <uuid_new+0x6b>
mov    0x6(%r15),%al
mov    0x8(%r15),%cl
and    $0xf,%al
or     $0x40,%al
mov    %al,0x6(%r15)
and    $0x3f,%cl
or     $0x80,%cl
mov    %cl,0x8(%r15)
xor    %r14d,%r14d
jmp    11ad <uuid_new+0x7d>
mov    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    0x2e2d(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @uuid_new(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 slt i32 %3, 0
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = tail call i32 @read(i32 %3, i8* %0, i32 16)
%7 = tail call i32 @close(i32 %3)
%8 = icmp slt i32 %6, 0
br i1 %8, label %22, label %9
9: ; preds = %5
%10 = icmp eq i32 %6, 16
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
br label %22
13: ; preds = %9
%14 = getelementptr inbounds i8, i8* %0, i64 6
%15 = load i8, i8* %14
%16 = and i8 %15, 15
%17 = or i8 %16, 64
store i8 %17, i8* %14
%18 = getelementptr inbounds i8, i8* %0, i64 8
%19 = load i8, i8* %18
%20 = and i8 %19, 63
%21 = or i8 %20, -128
store i8 %21, i8* %18
br label %22
22: ; preds = %5, %1, %13, %11
%23 = phi i32 [ -1, %11 ], [ 0, %13 ], [ -1, %1 ], [ -1, %5 ]
ret i32 %23
}