#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 scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ MAP_PRIVATE;
int O_CREAT;
int /*<<< orphan*/ O_RDWR;
int O_TRUNC;
int O_WRONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
void *mmap(int /*<<< orphan*/, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
int open(char *, int, ...);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, ...);
scalar_t__ write(int, void *, size_t);
int mkfile(char *path, size_t size) {
  int result = 0;
  void *addr;
  int fd;
  fd = open("/dev/zero", O_RDWR);
  if (fd < 0) {
    syslog(LOG_ERR, "[mkfile] could not map /dev/zero");
    return 0;
  }
  addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  close(fd);
  fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0644);
  if (fd < 0) {
    syslog(LOG_ERR, "[mkfile] open failed creating %s", path);
    goto done;
  }
  ssize_t bytes = write(fd, addr, size);
  if ((size_t)bytes == size) {
    result = 1;
  } else {
    syslog(LOG_ERR, "write failed creating %s", path);
  }
  close(fd);
done:
  munmap(addr, size);
  return result;
}


x86 assembly (clang, -O0):
<mkfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11be <mkfile+0x5e>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b4 <mkfile+0x154>
mov    -0x18(%rbp),%rsi
mov    0x2e1f(%rip),%rax
mov    (%rax),%edx
mov    0x2dee(%rip),%rax
or     (%rax),%edx
mov    0x2ddd(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    0x2d9c(%rip),%rax
or     (%rax),%esi
mov    0x2ddb(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1250 <mkfile+0xf0>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
jmp    12a1 <mkfile+0x141>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
jne    127e <mkfile+0x11e>
movl   $0x1,-0x1c(%rbp)
jmp    1299 <mkfile+0x139>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb0(%rip),%rsi
mov    $0x0,%al
call   1060 <syslog@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <munmap@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkfile>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
xor    %ebp,%ebp
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     1234 <mkfile+0xd4>
mov    %eax,%ebx
mov    0x2e50(%rip),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0e(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    %r12,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e29(%rip),%rax
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   1080 <open@plt>
test   %eax,%eax
js     124d <mkfile+0xed>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   1030 <write@plt>
mov    $0x1,%ebp
cmp    %r12,%rax
je     122b <mkfile+0xcb>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
xor    %ebp,%ebp
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1267 <mkfile+0x107>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xdbc(%rip),%rsi
xor    %eax,%eax
call   1060 <syslog@plt>
jmp    1272 <mkfile+0x112>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xdc4(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1060 <syslog@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <munmap@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [33 x i8] c"[mkfile] could not map /dev/zero\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [33 x i8] c"[mkfile] open failed creating %s\00"
@.str.3 = [25 x i8] c"write failed creating %s\00"
define i32 @mkfile(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 0, i32* %6
%10 = load i32, i32* @O_RDWR
%11 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @LOG_ERR
%16 = call i32 (i32, i8*, ...) @syslog(i32 %15, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %61
17: ; preds = %2
%18 = load i64, i64* %5
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @PROT_WRITE
%21 = or i32 %19, %20
%22 = load i32, i32* @MAP_PRIVATE
%23 = load i32, i32* %8
%24 = call i8* @mmap(i32 0, i64 %18, i32 %21, i32 %22, i32 %23, i32 0)
store i8* %24, i8** %7
%25 = load i32, i32* %8
%26 = call i32 @close(i32 %25)
%27 = load i8*, i8** %4
%28 = load i32, i32* @O_CREAT
%29 = load i32, i32* @O_WRONLY
%30 = or i32 %28, %29
%31 = load i32, i32* @O_TRUNC
%32 = or i32 %30, %31
%33 = call i32 (i8*, i32, ...) @open(i8* %27, i32 %32, i32 420)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %17
%37 = load i32, i32* @LOG_ERR
%38 = load i8*, i8** %4
%39 = call i32 (i32, i8*, ...) @syslog(i32 %37, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %38)
br label %56
40: ; preds = %17
%41 = load i32, i32* %8
%42 = load i8*, i8** %7
%43 = load i64, i64* %5
%44 = call i64 @write(i32 %41, i8* %42, i64 %43)
store i64 %44, i64* %9
%45 = load i64, i64* %9
%46 = load i64, i64* %5
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %40
store i32 1, i32* %6
br label %53
49: ; preds = %40
%50 = load i32, i32* @LOG_ERR
%51 = load i8*, i8** %4
%52 = call i32 (i32, i8*, ...) @syslog(i32 %50, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %49, %48
%54 = load i32, i32* %8
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %53, %36
%57 = load i8*, i8** %7
%58 = load i64, i64* %5
%59 = call i32 @munmap(i8* %57, i64 %58)
%60 = load i32, i32* %6
store i32 %60, i32* %3
br label %61
61: ; preds = %56, %14
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@LOG_ERR = global i32 0
@.str.1 = [33 x i8] c"[mkfile] could not map /dev/zero\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [33 x i8] c"[mkfile] open failed creating %s\00"
@.str.3 = [25 x i8] c"write failed creating %s\00"
define i32 @mkfile(i8* %0, i64 %1) {
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @LOG_ERR
%8 = tail call i32 (i32, i8*, ...) @syslog(i32 %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %38
9: ; preds = %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_PRIVATE
%14 = tail call i8* @mmap(i32 0, i64 %1, i32 %12, i32 %13, i32 %4, i32 0)
%15 = tail call i32 @close(i32 %4)
%16 = load i32, i32* @O_CREAT
%17 = load i32, i32* @O_WRONLY
%18 = or i32 %17, %16
%19 = load i32, i32* @O_TRUNC
%20 = or i32 %18, %19
%21 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %20, i32 420)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %9
%24 = load i32, i32* @LOG_ERR
%25 = tail call i32 (i32, i8*, ...) @syslog(i32 %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %35
26: ; preds = %9
%27 = tail call i64 @write(i32 %21, i8* %14, i64 %1)
%28 = icmp eq i64 %27, %1
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* @LOG_ERR
%31 = tail call i32 (i32, i8*, ...) @syslog(i32 %30, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %32
32: ; preds = %26, %29
%33 = phi i32 [ 0, %29 ], [ 1, %26 ]
%34 = tail call i32 @close(i32 %21)
br label %35
35: ; preds = %32, %23
%36 = phi i32 [ 0, %23 ], [ %33, %32 ]
%37 = tail call i32 @munmap(i8* %14, i64 %1)
br label %38
38: ; preds = %35, %6
%39 = phi i32 [ 0, %6 ], [ %36, %35 ]
ret i32 %39
}