#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*/ EXIT_FAILURE;
int FILENAME_MAX;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int write(int, char *, int);
void write_image(char *filename, char *image, int size) {
  char new_filename[FILENAME_MAX];
  int new_fd;
  strncpy(new_filename, filename, FILENAME_MAX - 5);
  strncat(new_filename, ".new", FILENAME_MAX - strlen(filename));
  printf("Writing new image to %s\n", new_filename);
  new_fd = open(new_filename, O_WRONLY | O_CREAT | O_TRUNC | 0,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (new_fd < 0) {
    perror("Error while trying to open file");
    exit(EXIT_FAILURE);
  }
  if (write(new_fd, image, size) != size)
    perror("Error while writing");
  close(new_fd);
}


x86 assembly (clang, -O0):
<write_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
sub    $0x5,%edx
call   1030 <strncpy@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rdi
sub    %eax,%edx
lea    0xdf7(%rip),%rsi
call   1070 <strncat@plt>
mov    -0x38(%rbp),%rsi
lea    0xdec(%rip),%rdi
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
mov    0x2db5(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
mov    0x2d88(%rip),%rax
or     (%rax),%edx
mov    0x2d67(%rip),%rax
or     (%rax),%edx
mov    0x2d56(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1290 <write_image+0x100>
lea    0xda1(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    -0x14(%rbp),%eax
je     12b4 <write_image+0x124>
lea    0xd8f(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_image>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x2e22(%rip),%rbx
mov    (%rbx),%eax
mov    %rsp,%r13
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
lea    -0x5(%rax),%edx
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <strncpy@plt>
mov    (%rbx),%ebx
mov    %r12,%rdi
call   1050 <strlen@plt>
sub    %eax,%ebx
lea    0xe1b(%rip),%rsi
mov    %r13,%rdi
mov    %ebx,%edx
call   1070 <strncat@plt>
lea    0xe0f(%rip),%rdi
mov    %r13,%rsi
call   1060 <printf@plt>
mov    0x2d93(%rip),%rax
mov    0x2dec(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    0x2d88(%rip),%rax
mov    0x2db1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d8e(%rip),%rax
or     (%rax),%edx
mov    0x2d7d(%rip),%rax
or     (%rax),%edx
mov    %r13,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1281 <write_image+0xf1>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
cmp    %r15d,%eax
jne    1273 <write_image+0xe3>
mov    %ebx,%edi
call   1080 <close@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
jmp    125d <write_image+0xcd>
lea    0xd96(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@FILENAME_MAX = global i32 0
@.str = [5 x i8] c".new\00"
@.str.1 = [25 x i8] c"Writing new image to %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [32 x i8] c"Error while trying to open file\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [20 x i8] c"Error while writing\00"
define void @write_image(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i32, i32* @FILENAME_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %7
%13 = alloca i8, i64 %11
store i64 %11, i64* %8
%14 = load i8*, i8** %4
%15 = load i32, i32* @FILENAME_MAX
%16 = sub nsw i32 %15, 5
%17 = call i32 @strncpy(i8* %13, i8* %14, i32 %16)
%18 = load i32, i32* @FILENAME_MAX
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = sub nsw i32 %18, %20
%22 = call i32 @strncat(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = load i32, i32* @O_TRUNC
%28 = or i32 %26, %27
%29 = or i32 %28, 0
%30 = load i32, i32* @S_IRUSR
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IRGRP
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IROTH
%36 = or i32 %34, %35
%37 = call i32 @open(i8* %13, i32 %29, i32 %36)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %3
%41 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%42 = load i32, i32* @EXIT_FAILURE
%43 = call i32 @exit(i32 %42)
unreachable
44: ; preds = %3
%45 = load i32, i32* %9
%46 = load i8*, i8** %5
%47 = load i32, i32* %6
%48 = call i32 @write(i32 %45, i8* %46, i32 %47)
%49 = load i32, i32* %6
%50 = icmp ne i32 %48, %49
br i1 %50, label %51, label %53
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %44
%54 = load i32, i32* %9
%55 = call i32 @close(i32 %54)
%56 = load i8*, i8** %7
ret void
}

LLVM IR (-O2):
@FILENAME_MAX = global i32 0
@.str = [5 x i8] c".new\00"
@.str.1 = [25 x i8] c"Writing new image to %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.2 = [32 x i8] c"Error while trying to open file\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [20 x i8] c"Error while writing\00"
define void @write_image(i8* %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @FILENAME_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = add nsw i32 %4, -5
%8 = call i32 @strncpy(i8* %6, i8* %0, i32 %7)
%9 = load i32, i32* @FILENAME_MAX
%10 = call i32 @strlen(i8* %0)
%11 = sub nsw i32 %9, %10
%12 = call i32 @strncat(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_CREAT
%16 = or i32 %15, %14
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %20, %19
%22 = load i32, i32* @S_IRGRP
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IROTH
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %6, i32 %18, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %3
%29 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %3
%33 = call i32 @write(i32 %26, i8* %1, i32 %2)
%34 = icmp eq i32 %33, %2
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = call i32 @close(i32 %26)
ret void
}