#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 ENOSPC;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int strlen(char *);
int write_block_device(char *dev_name, char **file_content) {
  FILE *output = NULL;
  int len = strlen(*file_content) + 6;
  int file_len = 0;
  ;
  output = fopen(dev_name, "r+b");
  fseek(output, 0L, SEEK_END);
  file_len = ftell(output);
  rewind(output);
  if (len > file_len) {
    printf("Insufficient space left on device: %s \n", dev_name);
    fclose(output);
    return -ENOSPC;
  }
  fwrite(*file_content, len, 1, output);
  fclose(output);
  return 0;
}


x86 assembly (clang, -O0):
<write_block_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
add    $0x6,%eax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1070 <ftell@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jle    1228 <write_block_device+0xa8>
mov    -0x10(%rbp),%rsi
lea    0xdfd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
mov    0x2dba(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1250 <write_block_device+0xd0>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_block_device>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    (%rsi),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
add    $0x6,%ebp
lea    0xe5b(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1060 <rewind@plt>
cmp    %r13d,%ebp
jle    1204 <write_block_device+0x84>
lea    0xe1f(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2ddc(%rip),%rax
xor    %r12d,%r12d
sub    (%rax),%r12d
jmp    121e <write_block_device+0x9e>
mov    (%r15),%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   10a0 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@.str.1 = [40 x i8] c"Insufficient space left on device: %s \0A\00"
@ENOSPC = global i32 0
define i32 @write_block_device(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* null, i32** %6
%9 = load i8**, i8*** %5
%10 = load i8*, i8** %9
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %11, 6
store i32 %12, i32* %7
store i32 0, i32* %8
%13 = load i8*, i8** %4
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = load i32, i32* @SEEK_END
%17 = call i32 @fseek(i32* %15, i64 0, i32 %16)
%18 = load i32*, i32** %6
%19 = call i32 @ftell(i32* %18)
store i32 %19, i32* %8
%20 = load i32*, i32** %6
%21 = call i32 @rewind(i32* %20)
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = icmp sgt i32 %22, %23
br i1 %24, label %25, label %32
25: ; preds = %2
%26 = load i8*, i8** %4
%27 = call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32*, i32** %6
%29 = call i32 @fclose(i32* %28)
%30 = load i32, i32* @ENOSPC
%31 = sub nsw i32 0, %30
store i32 %31, i32* %3
br label %40
32: ; preds = %2
%33 = load i8**, i8*** %5
%34 = load i8*, i8** %33
%35 = load i32, i32* %7
%36 = load i32*, i32** %6
%37 = call i32 @fwrite(i8* %34, i32 %35, i32 1, i32* %36)
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
store i32 0, i32* %3
br label %40
40: ; preds = %32, %25
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@SEEK_END = global i32 0
@.str.1 = [40 x i8] c"Insufficient space left on device: %s \0A\00"
@ENOSPC = global i32 0
define i32 @write_block_device(i8* %0, i8** nocapture %1) {
%3 = load i8*, i8** %1
%4 = tail call i32 @strlen(i8* %3)
%5 = add nsw i32 %4, 6
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @SEEK_END
%8 = tail call i32 @fseek(i32* %6, i64 0, i32 %7)
%9 = tail call i32 @ftell(i32* %6)
%10 = tail call i32 @rewind(i32* %6)
%11 = icmp sgt i32 %5, %9
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = tail call i32 @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%14 = tail call i32 @fclose(i32* %6)
%15 = load i32, i32* @ENOSPC
%16 = sub nsw i32 0, %15
br label %21
17: ; preds = %2
%18 = load i8*, i8** %1
%19 = tail call i32 @fwrite(i8* %18, i32 %5, i32 1, i32* %6)
%20 = tail call i32 @fclose(i32* %6)
br label %21
21: ; preds = %17, %12
%22 = phi i32 [ %16, %12 ], [ 0, %17 ]
ret i32 %22
}