#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 */
struct pollfd {
  int dummy;
};
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
char *write_usb_file(const char *filename, size_t length) {
  char *buffer;
  int fd;
  static int u_random_fd = -1;
  ssize_t bytes_read;
  unsigned random;
  struct pollfd pfds[2];
  unsigned cnt = 0;
  buffer = (char *)malloc(5);
  if (buffer == NULL)
    return NULL;
  fd = open(filename, O_RDWR);
  if (fd == -1) {
    printf("Open  %s failed!\n", filename);
    free(buffer);
    return NULL;
  }
  if (u_random_fd == -1) {
    u_random_fd = open("/dev/urandom", O_RDONLY);
    if (u_random_fd == -1) {
      printf("Open  /dev/random failed!\n");
      free(buffer);
      return NULL;
    }
  }
  printf("Now read from random!\n");
  if (read(u_random_fd, buffer, 5) != 5) {
    free(buffer);
    return NULL;
  }
  printf("Now write to USB device!\n");
  if (write(fd, buffer, 5) != 5) {
    printf("Error while writing to USB device: %s.\n", strerror(errno));
    free(buffer);
    return NULL;
  }
  free(buffer);
  return NULL;
}


x86 assembly (clang, -O0):
<write_usb_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    $0x5,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ad <write_usb_file+0x3d>
movq   $0x0,-0x8(%rbp)
jmp    1301 <write_usb_file+0x191>
mov    -0x10(%rbp),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11f4 <write_usb_file+0x84>
mov    -0x10(%rbp),%rsi
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1301 <write_usb_file+0x191>
cmpl   $0xffffffff,0x2e5d(%rip)
jne    1252 <write_usb_file+0xe2>
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    0xe01(%rip),%rdi
call   1080 <open@plt>
mov    %eax,0x2e3c(%rip)
cmpl   $0xffffffff,0x2e35(%rip)
jne    124d <write_usb_file+0xdd>
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1301 <write_usb_file+0x191>
jmp    1252 <write_usb_file+0xe2>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2df2(%rip),%edi
mov    -0x20(%rbp),%rsi
mov    $0x5,%edx
call   1060 <read@plt>
cmp    $0x5,%eax
je     1293 <write_usb_file+0x123>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1301 <write_usb_file+0x191>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
cmp    $0x5,%eax
je     12f0 <write_usb_file+0x180>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1301 <write_usb_file+0x191>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_usb_file>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbp
mov    $0x5,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     125b <write_usb_file+0xdb>
mov    %rax,%rbx
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1242 <write_usb_file+0xc2>
mov    %eax,%r14d
cmpl   $0xffffffff,0x2e9f(%rip)
jne    11e3 <write_usb_file+0x63>
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
lea    0xe3f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,0x2e82(%rip)
cmp    $0xffffffff,%eax
je     1262 <write_usb_file+0xe2>
lea    0xe5d(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e6b(%rip),%edi
mov    %rbx,%rsi
mov    $0x5,%edx
call   1070 <read@plt>
cmp    $0x5,%eax
jne    1253 <write_usb_file+0xd3>
lea    0xe4f(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x5,%edx
call   1050 <write@plt>
cmp    $0x5,%eax
je     1253 <write_usb_file+0xd3>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xde2(%rip),%rdi
mov    %rax,%rsi
jmp    124c <write_usb_file+0xcc>
lea    0xdb7(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe0d(%rip),%rdi
call   1040 <puts@plt>
jmp    1253 <write_usb_file+0xd3>

LLVM IR (-O0):
%struct.pollfd = type { i32 }
@write_usb_file.u_random_fd = internal global i32 -1
@O_RDWR = global i32 0
@.str = [18 x i8] c"Open %s failed!\0A\00"
@.str.1 = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.2 = [27 x i8] c"Open /dev/random failed!\0A\00"
@.str.3 = [23 x i8] c"Now read from random!\0A\00"
@.str.4 = [26 x i8] c"Now write to USB device!\0A\00"
@.str.5 = [40 x i8] c"Error while writing to USB device: %s.\0A\00"
@errno = global i32 0
define i8* @write_usb_file(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [2 x %struct.pollfd]
%11 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %11
%12 = call i64 @malloc(i32 5)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %2
store i8* null, i8** %3
br label %65
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %28
23: ; preds = %17
%24 = load i8*, i8** %4
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %6
%27 = call i32 @free(i8* %26)
store i8* null, i8** %3
br label %65
28: ; preds = %17
%29 = load i32, i32* @write_usb_file.u_random_fd
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %41
31: ; preds = %28
%32 = load i32, i32* @O_RDONLY
%33 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %32)
store i32 %33, i32* @write_usb_file.u_random_fd
%34 = load i32, i32* @write_usb_file.u_random_fd
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %31
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%38 = load i8*, i8** %6
%39 = call i32 @free(i8* %38)
store i8* null, i8** %3
br label %65
40: ; preds = %31
br label %41
41: ; preds = %40, %28
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @write_usb_file.u_random_fd
%44 = load i8*, i8** %6
%45 = call i32 @read(i32 %43, i8* %44, i32 5)
%46 = icmp ne i32 %45, 5
br i1 %46, label %47, label %50
47: ; preds = %41
%48 = load i8*, i8** %6
%49 = call i32 @free(i8* %48)
store i8* null, i8** %3
br label %65
50: ; preds = %41
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%52 = load i32, i32* %7
%53 = load i8*, i8** %6
%54 = call i32 @write(i32 %52, i8* %53, i32 5)
%55 = icmp ne i32 %54, 5
br i1 %55, label %56, label %62
56: ; preds = %50
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %58)
%60 = load i8*, i8** %6
%61 = call i32 @free(i8* %60)
store i8* null, i8** %3
br label %65
62: ; preds = %50
%63 = load i8*, i8** %6
%64 = call i32 @free(i8* %63)
store i8* null, i8** %3
br label %65
65: ; preds = %62, %56, %47, %36, %23, %16
%66 = load i8*, i8** %3
ret i8* %66
}

LLVM IR (-O2):
%struct.pollfd = type { i32 }
@write_usb_file.u_random_fd = internal unnamed_addr global i32 -1
@O_RDWR = global i32 0
@.str = [18 x i8] c"Open %s failed!\0A\00"
@.str.1 = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
@.str.5 = [40 x i8] c"Error while writing to USB device: %s.\0A\00"
@errno = global i32 0
@str = [22 x i8] c"Now read from random!\00"
@str.6 = [25 x i8] c"Now write to USB device!\00"
@str.7 = [26 x i8] c"Open /dev/random failed!\00"
define noalias i8* @write_usb_file(i8* %0, i64 %1) {
%3 = tail call i64 @malloc(i32 5)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %36, label %6
6: ; preds = %2
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* %0, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %34
12: ; preds = %6
%13 = load i32, i32* @write_usb_file.u_random_fd
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @O_RDONLY
%17 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %16)
store i32 %17, i32* @write_usb_file.u_random_fd
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.7, i64 0, i64 0))
br label %34
21: ; preds = %15, %12
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @write_usb_file.u_random_fd
%24 = tail call i32 @read(i32 %23, i8* %4, i32 5)
%25 = icmp eq i32 %24, 5
br i1 %25, label %26, label %34
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
%28 = tail call i32 @write(i32 %8, i8* %4, i32 5)
%29 = icmp eq i32 %28, 5
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = tail call i8* @strerror(i32 %31)
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %26, %21, %10, %19, %30
%35 = tail call i32 @free(i8* %4)
br label %36
36: ; preds = %34, %2
ret i8* null
}