#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int string_randhex(char *str, size_t len) {
  FILE *urandom = fopen("/dev/urandom", "r");
  unsigned char b;
  if (urandom == NULL)
    return -1;
  int i;
  for (i = 0; i < len; i += 2) {
    if (fread(&b, 1, 1, urandom) != 1) {
      fclose(urandom);
      return -1;
    }
    str[i] = ((((b >> 4) & 0xf) < 10) ? ('0' + ((b >> 4) & 0xf))
                                      : ('a' + (((b >> 4) & 0xf) - 10)));
    if (i + 1 < len)
      str[i + 1] =
          (((b & 0xf) < 10) ? ('0' + (b & 0xf)) : ('a' + ((b & 0xf) - 10)));
  }
  fclose(urandom);
  str[len] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<string_randhex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0xeb9(%rip),%rdi
lea    0xebf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116e <string_randhex+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <string_randhex+0x158>
movl   $0x0,-0x28(%rbp)
movslq -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    126c <string_randhex+0x13c>
mov    -0x20(%rbp),%rcx
lea    -0x21(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11b5 <string_randhex+0x85>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1288 <string_randhex+0x158>
movzbl -0x21(%rbp),%eax
sar    $0x4,%eax
and    $0xf,%eax
cmp    $0xa,%eax
jge    11dd <string_randhex+0xad>
movzbl -0x21(%rbp),%eax
sar    $0x4,%eax
and    $0xf,%eax
add    $0x30,%eax
mov    %eax,-0x2c(%rbp)
jmp    11f0 <string_randhex+0xc0>
movzbl -0x21(%rbp),%eax
sar    $0x4,%eax
and    $0xf,%eax
sub    $0xa,%eax
add    $0x61,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jae    1259 <string_randhex+0x129>
movzbl -0x21(%rbp),%eax
and    $0xf,%eax
cmp    $0xa,%eax
jge    1234 <string_randhex+0x104>
movzbl -0x21(%rbp),%eax
and    $0xf,%eax
add    $0x30,%eax
mov    %eax,-0x30(%rbp)
jmp    1244 <string_randhex+0x114>
movzbl -0x21(%rbp),%eax
and    $0xf,%eax
sub    $0xa,%eax
add    $0x61,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    125e <string_randhex+0x12e>
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    1175 <string_randhex+0x45>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<string_randhex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xeb8(%rip),%rdi
lea    0xebe(%rip),%rsi
call   1050 <fopen@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1202 <string_randhex+0xd2>
mov    %rax,%r13
test   %rbx,%rbx
je     11e8 <string_randhex+0xb8>
mov    $0x1,%ebp
lea    0x7(%rsp),%r12
jmp    1190 <string_randhex+0x60>
nopl   0x0(%rax,%rax,1)
lea    0x2(%rbp),%rax
add    $0x1,%rbp
cmp    %rbx,%rbp
mov    %rax,%rbp
jae    11e8 <string_randhex+0xb8>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    11fa <string_randhex+0xca>
movzbl 0x7(%rsp),%eax
mov    %eax,%ecx
shr    $0x4,%cl
lea    0x30(%rcx),%edx
add    $0x57,%cl
cmp    $0xa0,%al
movzbl %dl,%edx
movzbl %cl,%ecx
cmovb  %edx,%ecx
mov    %cl,-0x1(%r14,%rbp,1)
cmp    %rbx,%rbp
jae    1180 <string_randhex+0x50>
and    $0xf,%al
lea    0x30(%rax),%ecx
lea    0x57(%rax),%edx
cmp    $0xa,%al
movzbl %cl,%eax
movzbl %dl,%ecx
cmovb  %eax,%ecx
mov    %cl,(%r14,%rbp,1)
jmp    1180 <string_randhex+0x50>
mov    %r13,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%r14,%rbx,1)
xor    %r15d,%r15d
jmp    1202 <string_randhex+0xd2>
mov    %r13,%rdi
call   1040 <fclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @string_randhex(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca i8
%8 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %91
13: ; preds = %2
store i32 0, i32* %8
br label %14
14: ; preds = %82, %13
%15 = load i32, i32* %8
%16 = sext i32 %15 to i64
%17 = load i64, i64* %5
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %85
19: ; preds = %14
%20 = load i32*, i32** %6
%21 = call i32 @fread(i8* %7, i32 1, i32 1, i32* %20)
%22 = icmp ne i32 %21, 1
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
store i32 -1, i32* %3
br label %91
26: ; preds = %19
%27 = load i8, i8* %7
%28 = zext i8 %27 to i32
%29 = ashr i32 %28, 4
%30 = and i32 %29, 15
%31 = icmp slt i32 %30, 10
br i1 %31, label %32, label %38
32: ; preds = %26
%33 = load i8, i8* %7
%34 = zext i8 %33 to i32
%35 = ashr i32 %34, 4
%36 = and i32 %35, 15
%37 = add nsw i32 48, %36
br label %45
38: ; preds = %26
%39 = load i8, i8* %7
%40 = zext i8 %39 to i32
%41 = ashr i32 %40, 4
%42 = and i32 %41, 15
%43 = sub nsw i32 %42, 10
%44 = add nsw i32 97, %43
br label %45
45: ; preds = %38, %32
%46 = phi i32 [ %37, %32 ], [ %44, %38 ]
%47 = trunc i32 %46 to i8
%48 = load i8*, i8** %4
%49 = load i32, i32* %8
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 %47, i8* %51
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = load i64, i64* %5
%56 = icmp ult i64 %54, %55
br i1 %56, label %57, label %81
57: ; preds = %45
%58 = load i8, i8* %7
%59 = zext i8 %58 to i32
%60 = and i32 %59, 15
%61 = icmp slt i32 %60, 10
br i1 %61, label %62, label %67
62: ; preds = %57
%63 = load i8, i8* %7
%64 = zext i8 %63 to i32
%65 = and i32 %64, 15
%66 = add nsw i32 48, %65
br label %73
67: ; preds = %57
%68 = load i8, i8* %7
%69 = zext i8 %68 to i32
%70 = and i32 %69, 15
%71 = sub nsw i32 %70, 10
%72 = add nsw i32 97, %71
br label %73
73: ; preds = %67, %62
%74 = phi i32 [ %66, %62 ], [ %72, %67 ]
%75 = trunc i32 %74 to i8
%76 = load i8*, i8** %4
%77 = load i32, i32* %8
%78 = add nsw i32 %77, 1
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %76, i64 %79
store i8 %75, i8* %80
br label %81
81: ; preds = %73, %45
br label %82
82: ; preds = %81
%83 = load i32, i32* %8
%84 = add nsw i32 %83, 2
store i32 %84, i32* %8
br label %14
85: ; preds = %14
%86 = load i32*, i32** %6
%87 = call i32 @fclose(i32* %86)
%88 = load i8*, i8** %4
%89 = load i64, i64* %5
%90 = getelementptr inbounds i8, i8* %88, i64 %89
store i8 0, i8* %90
store i32 0, i32* %3
br label %91
91: ; preds = %85, %23, %12
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/urandom\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @string_randhex(i8* nocapture writeonly %0, i64 %1) {
%3 = alloca i8
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %37, label %6
6: ; preds = %2
%7 = icmp eq i64 %1, 0
br i1 %7, label %34, label %8
8: ; preds = %6, %31
%9 = phi i64 [ %32, %31 ], [ 0, %6 ]
%10 = call i32 @fread(i8* %3, i32 1, i32 1, i32* %4)
%11 = icmp eq i32 %10, 1
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @fclose(i32* %4)
br label %37
14: ; preds = %8
%15 = load i8, i8* %3
%16 = lshr i8 %15, 4
%17 = icmp ult i8 %15, -96
%18 = or i8 %16, 48
%19 = add nuw nsw i8 %16, 87
%20 = select i1 %17, i8 %18, i8 %19
%21 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 %20, i8* %21
%22 = or i64 %9, 1
%23 = icmp ult i64 %22, %1
br i1 %23, label %24, label %31
24: ; preds = %14
%25 = and i8 %15, 15
%26 = icmp ult i8 %25, 10
%27 = or i8 %25, 48
%28 = add nuw nsw i8 %25, 87
%29 = select i1 %26, i8 %27, i8 %28
%30 = getelementptr inbounds i8, i8* %0, i64 %22
store i8 %29, i8* %30
br label %31
31: ; preds = %14, %24
%32 = add nuw i64 %9, 2
%33 = icmp ult i64 %32, %1
br i1 %33, label %8, label %34
34: ; preds = %31, %6
%35 = tail call i32 @fclose(i32* %4)
%36 = getelementptr inbounds i8, i8* %0, i64 %1
store i8 0, i8* %36
br label %37
37: ; preds = %12, %34, %2
%38 = phi i32 [ -1, %2 ], [ -1, %12 ], [ 0, %34 ]
ret i32 %38
}