#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 open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int write(int, char *, int);
void write_driver(char *string) {
  int LENGTH = -1;
  if ((LENGTH = strlen(string)) == 0) {
    printf("ERROR: Gmem_test: write expects a string to be written\nPlease "
           "check gmem_test -help for usage");
    return;
  }
  int fp = open("/dev/gmem", 0);
  int num = write(fp, string, LENGTH);
  if (num == 0) {
    printf("WRITE FAILED");
    return;
  } else if (num < LENGTH)
    printf("Sorry only %d bytes could be written\n", num);
}


x86 assembly (clang, -O0):
<write_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    117b <write_driver+0x3b>
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11dd <write_driver+0x9d>
lea    0xedc(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11bb <write_driver+0x7b>
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11dd <write_driver+0x9d>
mov    -0x14(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11d8 <write_driver+0x98>
mov    -0x14(%rbp),%esi
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11dd <write_driver+0x9d>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_driver>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
test   %eax,%eax
je     118b <write_driver+0x4b>
mov    %eax,%ebx
lea    0xf05(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
test   %eax,%eax
je     1194 <write_driver+0x54>
cmp    %ebx,%eax
jge    11a9 <write_driver+0x69>
lea    0xefa(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <printf@plt>
lea    0xe6e(%rip),%rdi
jmp    119b <write_driver+0x5b>
lea    0xecd(%rip),%rdi
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1050 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [94 x i8] c"ERROR: Gmem_test: write expects a string to be written\0APlease check gmem_test -help for usage\00"
@.str.1 = [10 x i8] c"/dev/gmem\00"
@.str.2 = [13 x i8] c"WRITE FAILED\00"
@.str.3 = [38 x i8] c"Sorry only %d bytes could be written\0A\00"
define void @write_driver(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 -1, i32* %3
%6 = load i8*, i8** %2
%7 = call i32 @strlen(i8* %6)
store i32 %7, i32* %3
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([94 x i8], [94 x i8]* @.str, i64 0, i64 0))
br label %29
11: ; preds = %1
%12 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = load i8*, i8** %2
%15 = load i32, i32* %3
%16 = call i32 @write(i32 %13, i8* %14, i32 %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %29
21: ; preds = %11
%22 = load i32, i32* %5
%23 = load i32, i32* %3
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = load i32, i32* %5
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %21
br label %29
29: ; preds = %9, %19, %28
ret void
}

LLVM IR (-O2):
@.str = [94 x i8] c"ERROR: Gmem_test: write expects a string to be written\0APlease check gmem_test -help for usage\00"
@.str.1 = [10 x i8] c"/dev/gmem\00"
@.str.2 = [13 x i8] c"WRITE FAILED\00"
@.str.3 = [38 x i8] c"Sorry only %d bytes could be written\0A\00"
define void @write_driver(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([94 x i8], [94 x i8]* @.str, i64 0, i64 0))
br label %16
6: ; preds = %1
%7 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 0)
%8 = tail call i32 @write(i32 %7, i8* %0, i32 %2)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %16
12: ; preds = %6
%13 = icmp slt i32 %8, %2
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %8)
br label %16
16: ; preds = %10, %12, %14, %4
ret void
}