#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 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 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);
  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,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  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    0x2e36(%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    0x2e06(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    0x2df8(%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    0xe0a(%rip),%rsi
call   1070 <strncat@plt>
mov    -0x38(%rbp),%rsi
lea    0xdff(%rip),%rdi
call   1060 <printf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    0x2dc8(%rip),%rax
or     (%rax),%esi
mov    0x2d7f(%rip),%rax
mov    (%rax),%edx
mov    0x2d9e(%rip),%rax
or     (%rax),%edx
mov    0x2d7d(%rip),%rax
or     (%rax),%edx
mov    0x2d6c(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    -0x14(%rbp),%eax
je     127a <write_image+0xfa>
lea    0xda9(%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    0x2e32(%rip),%rbx
mov    (%rbx),%edx
mov    %rsp,%r13
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <strncpy@plt>
mov    (%rbx),%ebx
mov    %r12,%rdi
call   1050 <strlen@plt>
sub    %eax,%ebx
lea    0xe2e(%rip),%rsi
mov    %r13,%rdi
mov    %ebx,%edx
call   1070 <strncat@plt>
lea    0xe22(%rip),%rdi
mov    %r13,%rsi
call   1060 <printf@plt>
mov    0x2dae(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dec(%rip),%rax
or     (%rax),%esi
mov    0x2da3(%rip),%rax
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2da1(%rip),%rax
or     (%rax),%edx
mov    0x2d90(%rip),%rax
or     (%rax),%edx
mov    %r13,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
cmp    %r15d,%eax
jne    125c <write_image+0xdc>
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    0xdbb(%rip),%rdi
call   10a0 <perror@plt>
jmp    1246 <write_image+0xc6>

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 = [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 = call i32 @strncpy(i8* %13, i8* %14, i32 %15)
%17 = load i32, i32* @FILENAME_MAX
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %17, %19
%21 = call i32 @strncat(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IWUSR
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IRGRP
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IROTH
%34 = or i32 %32, %33
%35 = call i32 @open(i8* %13, i32 %27, i32 %34)
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%39 = call i32 @write(i32 %36, i8* %37, i32 %38)
%40 = load i32, i32* %6
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %3
%43 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %3
%45 = load i32, i32* %9
%46 = call i32 @close(i32 %45)
%47 = 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 = [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 = call i32 @strncpy(i8* %6, i8* %0, i32 %4)
%8 = load i32, i32* @FILENAME_MAX
%9 = call i32 @strlen(i8* %0)
%10 = sub nsw i32 %8, %9
%11 = call i32 @strncat(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %6, i32 %17, i32 %24)
%26 = call i32 @write(i32 %25, i8* %1, i32 %2)
%27 = icmp eq i32 %26, %2
br i1 %27, label %30, label %28
28: ; preds = %3
%29 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %3
%31 = call i32 @close(i32 %25)
ret void
}