#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int MAXPATHLEN;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fchmod(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getcwd(char *, int);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char *);
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void create_disk_image(char *filename, int size) {
  struct stat statbuf;
  int fd;
  char *image_buf;
  char pathname[MAXPATHLEN + 1];
  if (filename[0] == '/') {
    strncpy(pathname, filename, MAXPATHLEN);
  } else {
    getcwd(pathname, MAXPATHLEN);
    if (strlen(pathname) + strlen(filename) + 1 > MAXPATHLEN) {
      fprintf(stderr, "Filename too long\n");
      exit(1);
    }
    strcat(pathname, "/");
    strcat(pathname, filename);
  }
  if (stat(pathname, &statbuf) >= 0) {
    fprintf(stderr, "Cannot create disk image file %s: file already exists\n",
            pathname);
    exit(1);
  }
  fd = open(pathname, O_RDWR | O_CREAT);
  if (fd < 0) {
    fprintf(stderr, "Cannot create disk image file %s:\n%s\n", pathname,
            strerror(errno));
    exit(1);
  }
  fchmod(fd, S_IRUSR | S_IWUSR);
  image_buf = malloc(size);
  memset(image_buf, 0xff, size);
  write(fd, image_buf, size);
  close(fd);
  free(image_buf);
}


x86 assembly (clang, -O0):
<create_disk_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1259 <create_disk_image+0x69>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
jmp    12d7 <create_disk_image+0xe7>
mov    -0x38(%rbp),%rdi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
call   1080 <getcwd@plt>
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%eax
jle    12ba <create_disk_image+0xca>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x38(%rbp),%rdi
lea    0xd4e(%rip),%rsi
call   10f0 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   10f0 <strcat@plt>
mov    -0x38(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jl     1313 <create_disk_image+0x123>
mov    -0x38(%rbp),%rdx
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    0x2cc2(%rip),%rax
mov    (%rax),%esi
mov    0x2cd1(%rip),%rax
or     (%rax),%esi
call   10e0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1377 <create_disk_image+0x187>
mov    0x2c7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2c6a(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xce6(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2c2f(%rip),%rax
mov    (%rax),%esi
mov    0x2c46(%rip),%rax
or     (%rax),%esi
call   10d0 <fchmod@plt>
mov    -0xc(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0xff,%esi
call   1070 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0xc(%rbp),%edx
call   1050 <write@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_disk_image>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rsp,-0x30(%rbp)
mov    0x2dc6(%rip),%r13
mov    0x0(%r13),%eax
add    $0x1,%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
cmpb   $0x2f,(%rdi)
mov    0x0(%r13),%edx
mov    %r12,%rdi
jne    1240 <create_disk_image+0x50>
mov    %r15,%rsi
call   1040 <strncpy@plt>
jmp    127f <create_disk_image+0x8f>
mov    %edx,%esi
call   1080 <getcwd@plt>
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1060 <strlen@plt>
add    %ebx,%eax
cmp    0x0(%r13),%eax
jge    137a <create_disk_image+0x18a>
lea    0xda7(%rip),%rsi
mov    %r12,%rdi
call   10f0 <strcat@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   10f0 <strcat@plt>
lea    -0x38(%rbp),%rsi
mov    %r12,%rdi
call   10b0 <stat@plt>
test   %rax,%rax
jns    131f <create_disk_image+0x12f>
mov    0x2d45(%rip),%rax
mov    0x2d56(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r12,%rdi
call   10e0 <open@plt>
test   %eax,%eax
js     1343 <create_disk_image+0x153>
mov    %eax,%r15d
mov    0x2cf0(%rip),%rax
mov    0x2d09(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15d,%edi
call   10d0 <fchmod@plt>
mov    %r14d,%edi
call   10c0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0xff,%esi
mov    %r14d,%edx
call   1070 <memset@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <write@plt>
mov    %r15d,%edi
call   1090 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c9a(%rip),%rax
mov    (%rax),%edi
lea    0xce6(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2c76(%rip),%rax
mov    (%rax),%ebx
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
lea    0xceb(%rip),%rsi
mov    %ebx,%edi
mov    %r12,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    0x2c3f(%rip),%rax
mov    (%rax),%edi
lea    0xc76(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Filename too long\0A\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [55 x i8] c"Cannot create disk image file %s: file already exists\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.3 = [38 x i8] c"Cannot create disk image file %s:\0A%s\0A\00"
@errno = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define void @create_disk_image(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i32, i32* @MAXPATHLEN
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = load i32, i32* @MAXPATHLEN
%23 = call i32 @strncpy(i8* %14, i8* %21, i32 %22)
br label %42
24: ; preds = %2
%25 = load i32, i32* @MAXPATHLEN
%26 = call i32 @getcwd(i8* %14, i32 %25)
%27 = call i32 @strlen(i8* %14)
%28 = load i8*, i8** %3
%29 = call i32 @strlen(i8* %28)
%30 = add nsw i32 %27, %29
%31 = add nsw i32 %30, 1
%32 = load i32, i32* @MAXPATHLEN
%33 = icmp sgt i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %24
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %24
%39 = call i32 @strcat(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%40 = load i8*, i8** %3
%41 = call i32 @strcat(i8* %14, i8* %40)
br label %42
42: ; preds = %38, %20
%43 = call i64 @stat(i8* %14, %struct.stat* %5)
%44 = icmp sge i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %42
%50 = load i32, i32* @O_RDWR
%51 = load i32, i32* @O_CREAT
%52 = or i32 %50, %51
%53 = call i32 @open(i8* %14, i32 %52)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %49
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %14, i8* %59)
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %49
%63 = load i32, i32* %6
%64 = load i32, i32* @S_IRUSR
%65 = load i32, i32* @S_IWUSR
%66 = or i32 %64, %65
%67 = call i32 @fchmod(i32 %63, i32 %66)
%68 = load i32, i32* %4
%69 = call i8* @malloc(i32 %68)
store i8* %69, i8** %7
%70 = load i8*, i8** %7
%71 = load i32, i32* %4
%72 = call i32 @memset(i8* %70, i32 255, i32 %71)
%73 = load i32, i32* %6
%74 = load i8*, i8** %7
%75 = load i32, i32* %4
%76 = call i32 @write(i32 %73, i8* %74, i32 %75)
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = load i8*, i8** %7
%80 = call i32 @free(i8* %79)
%81 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@MAXPATHLEN = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"Filename too long\0A\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [55 x i8] c"Cannot create disk image file %s: file already exists\0A\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.3 = [38 x i8] c"Cannot create disk image file %s:\0A%s\0A\00"
@errno = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define void @create_disk_image(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @MAXPATHLEN
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = load i8, i8* %0
%11 = icmp eq i8 %10, 47
%12 = load i32, i32* @MAXPATHLEN
br i1 %11, label %13, label %15
13: ; preds = %2
%14 = call i32 @strncpy(i8* %9, i8* %0, i32 %12)
br label %29
15: ; preds = %2
%16 = call i32 @getcwd(i8* %9, i32 %12)
%17 = call i32 @strlen(i8* %9)
%18 = call i32 @strlen(i8* %0)
%19 = add nsw i32 %18, %17
%20 = load i32, i32* @MAXPATHLEN
%21 = icmp slt i32 %19, %20
br i1 %21, label %26, label %22
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = call i32 @strcat(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @strcat(i8* %9, i8* %0)
br label %29
29: ; preds = %26, %13
%30 = call i64 @stat(i8* %9, %struct.stat* %3)
%31 = icmp sgt i64 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = load i32, i32* @O_RDWR
%38 = load i32, i32* @O_CREAT
%39 = or i32 %38, %37
%40 = call i32 @open(i8* %9, i32 %39)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %36
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %9, i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %36
%49 = load i32, i32* @S_IRUSR
%50 = load i32, i32* @S_IWUSR
%51 = or i32 %50, %49
%52 = call i32 @fchmod(i32 %40, i32 %51)
%53 = call i8* @malloc(i32 %1)
%54 = call i32 @memset(i8* %53, i32 255, i32 %1)
%55 = call i32 @write(i32 %40, i8* %53, i32 %1)
%56 = call i32 @close(i32 %40)
%57 = call i32 @free(i8* %53)
ret void
}