#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*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int);
int test(char *path, char *mode) {
  int retVal = 0;
  FILE *file;
  char buf[100];
  printf("Started test for device %s with mode %s.\n", path, mode);
  file = fopen(path, mode);
  if (file == NULL) {
    printf("Open failed - %s\n", strerror(errno));
    return errno;
  }
  printf("Opened\n");
  retVal = 0;
  retVal = fread(buf, sizeof(buf), 1, file);
  printf("Read %d blocks: %s\n", retVal, buf);
  retVal = 0;
  retVal = fwrite("hello driver", sizeof("hello driver"),
                  sizeof("hello driver"), file);
  printf("Wrote %d bytes\n", retVal);
  fclose(file);
  printf("Closed\n");
  printf("Finished test for device %s with mode %s.\n\n", path, mode);
  return 0;
}


x86 assembly (clang, -O0):
<test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11dc <test+0x7c>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe66(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    128c <test+0x12c>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x1c(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x64,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%rcx
lea    0xe20(%rip),%rdi
mov    $0xd,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x70,%rsp
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0xe77(%rip),%rdi
xor    %r14d,%r14d
mov    %rbp,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     123a <test+0xca>
mov    %rax,%rbx
lea    0xee2(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%r12
mov    %r12,%rdi
mov    $0x64,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
lea    0xe61(%rip),%rdi
mov    %eax,%esi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe62(%rip),%rdi
mov    $0xd,%esi
mov    $0xd,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
lea    0xe56(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
lea    0xe42(%rip),%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    125c <test+0xec>
mov    0x2d9f(%rip),%rbx
mov    (%rbx),%edi
call   1090 <strerror@plt>
lea    0xddb(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    (%rbx),%r14d
mov    %r14d,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"Started test for device %s with mode %s.\0A\00"
@.str.1 = [18 x i8] c"Open failed - %s\0A\00"
@errno = global i32 0
@.str.2 = [8 x i8] c"Opened\0A\00"
@.str.3 = [20 x i8] c"Read %d blocks: %s\0A\00"
@.str.4 = [13 x i8] c"hello driver\00"
@.str.5 = [16 x i8] c"Wrote %d bytes\0A\00"
@.str.6 = [8 x i8] c"Closed\0A\00"
@.str.7 = [44 x i8] c"Finished test for device %s with mode %s.\0A\0A\00"
define i32 @test(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [100 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %6
%9 = load i8*, i8** %4
%10 = load i8*, i8** %5
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %9, i8* %10)
%12 = load i8*, i8** %4
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %12, i8* %13)
store i32* %14, i32** %7
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i32, i32* @errno
store i32 %21, i32* %3
br label %40
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%25 = load i32*, i32** %7
%26 = call i32 @fread(i8* %24, i32 100, i32 1, i32* %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %27, i8* %28)
store i32 0, i32* %6
%30 = load i32*, i32** %7
%31 = call i32 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 13, i32 13, i32* %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = load i32*, i32** %7
%35 = call i32 @fclose(i32* %34)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%37 = load i8*, i8** %4
%38 = load i8*, i8** %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %37, i8* %38)
store i32 0, i32* %3
br label %40
40: ; preds = %22, %17
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [42 x i8] c"Started test for device %s with mode %s.\0A\00"
@.str.1 = [18 x i8] c"Open failed - %s\0A\00"
@errno = global i32 0
@.str.3 = [20 x i8] c"Read %d blocks: %s\0A\00"
@.str.4 = [13 x i8] c"hello driver\00"
@.str.5 = [16 x i8] c"Wrote %d bytes\0A\00"
@.str.7 = [44 x i8] c"Finished test for device %s with mode %s.\0A\0A\00"
@str = [7 x i8] c"Opened\00"
@str.8 = [7 x i8] c"Closed\00"
define i32 @test(i8* %0, i8* %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%6 = tail call i32* @fopen(i8* %0, i8* %1)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @errno
br label %22
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%15 = call i32 @fread(i8* %4, i32 100, i32 1, i32* %6)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %15, i8* %4)
%17 = call i32 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 13, i32 13, i32* %6)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%19 = call i32 @fclose(i32* %6)
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.8, i64 0, i64 0))
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %1)
br label %22
22: ; preds = %13, %8
%23 = phi i32 [ %12, %8 ], [ 0, %13 ]
ret i32 %23
}