#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 /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ ftruncate(int, int /*<<< orphan*/);
char *malloc(size_t);
int mkstemp(char *);
int open(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
scalar_t__ unlink(char *);
int make_file(const char *pathname, off_t sz) {
  struct stat st;
  const char *template = "/flocktempXXXXXX";
  size_t len;
  char *filename;
  int fd;
  if (stat(pathname, &st) == 0) {
    if (S_ISREG(st.st_mode)) {
      fd = open(pathname, O_RDWR);
      if (fd < 0)
        err(1, "open(%s)", pathname);
      if (ftruncate(fd, sz) < 0)
        err(1, "ftruncate");
      return (fd);
    }
  }
  len = strlen(pathname) + strlen(template) + 1;
  filename = malloc(len);
  strcpy(filename, pathname);
  strcat(filename, template);
  fd = mkstemp(filename);
  if (fd < 0)
    err(1, "mkstemp");
  if (ftruncate(fd, sz) < 0)
    err(1, "ftruncate");
  if (unlink(filename) < 0)
    err(1, "unlink");
  free(filename);
  return (fd);
}


x86 assembly (clang, -O0):
<make_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe2a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jne    1271 <make_file+0xb1>
mov    -0x18(%rbp),%edi
call   1090 <S_ISREG@plt>
cmp    $0x0,%rax
je     126c <make_file+0xac>
mov    -0x10(%rbp),%rdi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1239 <make_file+0x79>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x34(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <ftruncate@plt>
cmp    $0x0,%rax
jge    1261 <make_file+0xa1>
mov    $0x1,%edi
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1343 <make_file+0x183>
jmp    1271 <make_file+0xb1>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10e0 <strcat@plt>
mov    -0x30(%rbp),%rdi
call   10c0 <mkstemp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12e6 <make_file+0x126>
mov    $0x1,%edi
lea    0xd45(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x34(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1070 <ftruncate@plt>
cmp    $0x0,%rax
jge    130e <make_file+0x14e>
mov    $0x1,%edi
lea    0xd13(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x30(%rbp),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
jge    1334 <make_file+0x174>
mov    $0x1,%edi
lea    0xcff(%rip),%rsi
mov    $0x0,%al
call   10a0 <err@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     1298 <make_file+0xd8>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
lea    0xe11(%rip),%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
add    %ebp,%eax
add    $0x1,%eax
movslq %eax,%rdi
call   10b0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <strcpy@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10e0 <strcat@plt>
mov    %rbp,%rdi
call   10c0 <mkstemp@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    123f <make_file+0x7f>
lea    0xdf1(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <err@plt>
mov    %r15d,%edi
mov    %r14d,%esi
call   1070 <ftruncate@plt>
test   %rax,%rax
jns    1262 <make_file+0xa2>
lea    0xdc4(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <err@plt>
mov    %rbp,%rdi
call   1040 <unlink@plt>
test   %rax,%rax
jns    1282 <make_file+0xc2>
lea    0xdb6(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <err@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%edi
call   1090 <S_ISREG@plt>
test   %rax,%rax
je     11de <make_file+0x1e>
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10d0 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    12d7 <make_file+0x117>
lea    0xd49(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   10a0 <err@plt>
mov    %r15d,%edi
mov    %r14d,%esi
call   1070 <ftruncate@plt>
test   %rax,%rax
jns    128a <make_file+0xca>
lea    0xd2c(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   10a0 <err@plt>
jmp    128a <make_file+0xca>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [17 x i8] c"/flocktempXXXXXX\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"open(%s)\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@.str.3 = [8 x i8] c"mkstemp\00"
@.str.4 = [7 x i8] c"unlink\00"
define i32 @make_file(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8** %7
%11 = load i8*, i8** %4
%12 = call i64 @stat(i8* %11, %struct.stat* %6)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %38
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @S_ISREG(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %37
19: ; preds = %14
%20 = load i8*, i8** %4
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = load i8*, i8** %4
%27 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %28
28: ; preds = %25, %19
%29 = load i32, i32* %10
%30 = load i32, i32* %5
%31 = call i64 @ftruncate(i32 %29, i32 %30)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %28
%34 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
%36 = load i32, i32* %10
store i32 %36, i32* %3
br label %77
37: ; preds = %14
br label %38
38: ; preds = %37, %2
%39 = load i8*, i8** %4
%40 = call i32 @strlen(i8* %39)
%41 = load i8*, i8** %7
%42 = call i32 @strlen(i8* %41)
%43 = add nsw i32 %40, %42
%44 = add nsw i32 %43, 1
%45 = sext i32 %44 to i64
store i64 %45, i64* %8
%46 = load i64, i64* %8
%47 = call i8* @malloc(i64 %46)
store i8* %47, i8** %9
%48 = load i8*, i8** %9
%49 = load i8*, i8** %4
%50 = call i32 @strcpy(i8* %48, i8* %49)
%51 = load i8*, i8** %9
%52 = load i8*, i8** %7
%53 = call i32 @strcat(i8* %51, i8* %52)
%54 = load i8*, i8** %9
%55 = call i32 @mkstemp(i8* %54)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %38
%59 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %58, %38
%61 = load i32, i32* %10
%62 = load i32, i32* %5
%63 = call i64 @ftruncate(i32 %61, i32 %62)
%64 = icmp slt i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %60
%66 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %67
67: ; preds = %65, %60
%68 = load i8*, i8** %9
%69 = call i64 @unlink(i8* %68)
%70 = icmp slt i64 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %67
%72 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %73
73: ; preds = %71, %67
%74 = load i8*, i8** %9
%75 = call i32 @free(i8* %74)
%76 = load i32, i32* %10
store i32 %76, i32* %3
br label %77
77: ; preds = %73, %35
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [17 x i8] c"/flocktempXXXXXX\00"
@O_RDWR = global i32 0
@.str.1 = [9 x i8] c"open(%s)\00"
@.str.2 = [10 x i8] c"ftruncate\00"
@.str.3 = [8 x i8] c"mkstemp\00"
@.str.4 = [7 x i8] c"unlink\00"
define i32 @make_file(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %23
7: ; preds = %2
%8 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i64 @S_ISREG(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %23, label %12
12: ; preds = %7
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* %0, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %12
%19 = call i64 @ftruncate(i32 %14, i32 %1)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %48
21: ; preds = %18
%22 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %48
23: ; preds = %7, %2
%24 = call i32 @strlen(i8* %0)
%25 = call i32 @strlen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%26 = add i32 %24, 1
%27 = add i32 %26, %25
%28 = sext i32 %27 to i64
%29 = call i8* @malloc(i64 %28)
%30 = call i32 @strcpy(i8* %29, i8* %0)
%31 = call i32 @strcat(i8* %29, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%32 = call i32 @mkstemp(i8* %29)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %23
%35 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %23
%37 = call i64 @ftruncate(i32 %32, i32 %1)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = call i64 @unlink(i8* %29)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %41
%47 = call i32 @free(i8* %29)
br label %48
48: ; preds = %18, %21, %46
%49 = phi i32 [ %32, %46 ], [ %14, %21 ], [ %14, %18 ]
ret i32 %49
}