#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_WRONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
scalar_t__ write(int, char *, int);
int write_to_stick(char *message, int len) {
  int driver_file;
  driver_file = open("/dev/usb/blinkstick0", O_WRONLY);
  if (driver_file <= 0) {
    printf("Open operation failed\n");
    return -1;
  }
  if (write(driver_file, message, len) == 0) {
    printf("Write operation failed\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<write_to_stick>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1179 <write_to_stick+0x49>
lea    0xead(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <write_to_stick+0x81>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jne    11aa <write_to_stick+0x7a>
lea    0xe93(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b1 <write_to_stick+0x81>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_stick>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
lea    0xeb8(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
jle    1170 <write_to_stick+0x40>
mov    %eax,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <write@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1181 <write_to_stick+0x51>
lea    0xea7(%rip),%rdi
jmp    1177 <write_to_stick+0x47>
lea    0xeb5(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"/dev/usb/blinkstick0\00"
@O_WRONLY = global i32 0
@.str.1 = [23 x i8] c"Open operation failed\0A\00"
@.str.2 = [24 x i8] c"Write operation failed\0A\00"
define i32 @write_to_stick(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp sle i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %22
13: ; preds = %2
%14 = load i32, i32* %6
%15 = load i8*, i8** %4
%16 = load i32, i32* %5
%17 = call i64 @write(i32 %14, i8* %15, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %22
21: ; preds = %13
store i32 0, i32* %3
br label %22
22: ; preds = %21, %19, %11
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
@.str = [21 x i8] c"/dev/usb/blinkstick0\00"
@O_WRONLY = global i32 0
@str = [23 x i8] c"Write operation failed\00"
@str.3 = [22 x i8] c"Open operation failed\00"
define i32 @write_to_stick(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 1
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = tail call i64 @write(i32 %4, i8* %0, i32 %1)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %6, %2
%10 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @str.3, i64 0, i64 0), %2 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0), %6 ]
%11 = tail call i32 @puts(i8* dereferenceable(1) %10)
br label %12
12: ; preds = %9, %6
%13 = phi i32 [ 0, %6 ], [ -1, %9 ]
ret i32 %13
}