#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ link(char const *, char *);
int /*<<< orphan*/ log_debug(char *, char const *, ...);
int /*<<< orphan*/ log_error(char *, char const *, int /*<<< orphan*/);
scalar_t__ maildir_ensure_exists(char const *);
char *malloc(size_t);
char *md_make_filename(struct stat *);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
size_t strlen(char const *);
int maildir_deliver_link(const char *src, const char *mdir, char **out_fn) {
  int status = -1;
  char *fn = NULL;
  char *tgt = NULL;
  log_debug("delivering file %s to maildir %s", src, mdir);
  if (maildir_ensure_exists(mdir)) {
    return -1;
  }
  struct stat buf;
  if (stat(src, &buf)) {
    log_error("maildir: %s: %s", src, strerror(errno));
    return -1;
  }
  fn = md_make_filename(&buf);
  if (!fn) {
    goto done;
  }
  log_debug("delivering mail with filename %s", fn);
  size_t md_len = strlen(mdir);
  size_t fn_len = strlen(fn);
  size_t tgt_size = md_len + 5 + fn_len + 1;
  tgt = malloc(tgt_size);
  if (!tgt) {
    goto done;
  }
  memset(tgt, 0, tgt_size);
  memcpy(tgt, mdir, md_len);
  memcpy(tgt + md_len, "/new/", 5);
  memcpy(tgt + md_len + 5, fn, fn_len);
  log_debug("delivering mail to file %s", tgt);
  if (link(src, tgt)) {
    log_error("delivery to %s failed: %s", mdir, strerror(errno));
  } else {
    status = 0;
    if (out_fn) {
      *out_fn = tgt;
      tgt = NULL;
    }
  }
done:
  if (fn) {
    free(fn);
  }
  if (tgt) {
    free(tgt);
  }
  return status;
}


x86 assembly (clang, -O0):
<maildir_deliver_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1060 <log_debug@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <maildir_ensure_exists@plt>
cmp    $0x0,%rax
je     1220 <maildir_deliver_link+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <maildir_deliver_link+0x22a>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     126b <maildir_deliver_link+0xab>
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
lea    0xdc7(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <maildir_deliver_link+0x22a>
lea    -0x40(%rbp),%rdi
call   10e0 <md_make_filename@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1288 <maildir_deliver_link+0xc8>
jmp    13bc <maildir_deliver_link+0x1fc>
mov    -0x30(%rbp),%rsi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1060 <log_debug@plt>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
add    $0x5,%rax
add    -0x50(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12e5 <maildir_deliver_link+0x125>
jmp    13bc <maildir_deliver_link+0x1fc>
mov    -0x38(%rbp),%rdi
mov    -0x58(%rbp),%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x38(%rbp),%rdi
add    -0x48(%rbp),%rdi
lea    0xd3e(%rip),%rsi
mov    $0x5,%edx
call   1090 <memcpy@plt>
mov    -0x38(%rbp),%rdi
add    -0x48(%rbp),%rdi
add    $0x5,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1090 <memcpy@plt>
mov    -0x38(%rbp),%rsi
lea    0xd16(%rip),%rdi
mov    $0x0,%al
call   1060 <log_debug@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   10c0 <link@plt>
cmp    $0x0,%rax
je     138d <maildir_deliver_link+0x1cd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2c71(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
lea    0xcf0(%rip),%rdi
call   1040 <log_error@plt>
jmp    13b7 <maildir_deliver_link+0x1f7>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     13b2 <maildir_deliver_link+0x1f2>
mov    -0x38(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x38(%rbp)
jmp    13b7 <maildir_deliver_link+0x1f7>
jmp    13bc <maildir_deliver_link+0x1fc>
cmpq   $0x0,-0x30(%rbp)
je     13d0 <maildir_deliver_link+0x210>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpq   $0x0,-0x38(%rbp)
je     13e4 <maildir_deliver_link+0x224>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<maildir_deliver_link>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe22(%rip),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <log_debug@plt>
mov    %rbx,%rdi
call   10b0 <maildir_ensure_exists@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    1375 <maildir_deliver_link+0x1b5>
lea    0x10(%rsp),%rsi
mov    %r15,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     123c <maildir_deliver_link+0x7c>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xdf9(%rip),%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <log_error@plt>
mov    $0xffffffff,%ebp
jmp    1375 <maildir_deliver_link+0x1b5>
lea    0x10(%rsp),%rdi
call   10e0 <md_make_filename@plt>
test   %rax,%rax
je     134d <maildir_deliver_link+0x18d>
mov    %rax,%rbp
lea    0xdd8(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <log_debug@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r13
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %rbp,%r12
mov    %r13,0x8(%rsp)
mov    %rax,0x20(%rsp)
add    %rax,%r13
add    $0x6,%r13
mov    %r13,%rdi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1354 <maildir_deliver_link+0x194>
xor    %ebp,%ebp
mov    %r12,0x18(%rsp)
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1070 <memset@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    0x8(%rsp),%r13
mov    %r13,%rdx
call   1090 <memcpy@plt>
lea    (%r12,%r13,1),%rdi
lea    0xd82(%rip),%rsi
mov    $0x5,%edx
call   1090 <memcpy@plt>
lea    (%r12,%r13,1),%rdi
add    $0x5,%rdi
mov    0x18(%rsp),%r13
mov    %r13,%rsi
mov    0x20(%rsp),%rdx
call   1090 <memcpy@plt>
lea    0xd5d(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <log_debug@plt>
mov    %r15,%rdi
mov    %r12,0x8(%rsp)
mov    %r12,%rsi
call   10c0 <link@plt>
test   %rax,%rax
je     135e <maildir_deliver_link+0x19e>
mov    0x2cbf(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd44(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <log_error@plt>
mov    $0xffffffff,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x8(%rsp),%rdi
jmp    1370 <maildir_deliver_link+0x1b0>
mov    $0xffffffff,%ebp
jmp    1375 <maildir_deliver_link+0x1b5>
mov    $0xffffffff,%ebp
mov    %r12,%r13
jmp    136d <maildir_deliver_link+0x1ad>
test   %r14,%r14
je     133e <maildir_deliver_link+0x17e>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
xor    %ebp,%ebp
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [33 x i8] c"delivering file %s to maildir %s\00"
@.str.1 = [16 x i8] c"maildir: %s: %s\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"delivering mail with filename %s\00"
@.str.3 = [6 x i8] c"/new/\00"
@.str.4 = [27 x i8] c"delivering mail to file %s\00"
@.str.5 = [26 x i8] c"delivery to %s failed: %s\00"
define i32 @maildir_deliver_link(i8* %0, i8* %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.stat
%12 = alloca i64
%13 = alloca i64
%14 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i32 -1, i32* %8
store i8* null, i8** %9
store i8* null, i8** %10
%15 = load i8*, i8** %5
%16 = load i8*, i8** %6
%17 = call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16)
%18 = load i8*, i8** %6
%19 = call i64 @maildir_ensure_exists(i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 -1, i32* %4
br label %105
22: ; preds = %3
%23 = load i8*, i8** %5
%24 = call i64 @stat(i8* %23, %struct.stat* %11)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i8*, i8** %5
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 @log_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %27, i32 %29)
store i32 -1, i32* %4
br label %105
31: ; preds = %22
%32 = call i8* @md_make_filename(%struct.stat* %11)
store i8* %32, i8** %9
%33 = load i8*, i8** %9
%34 = icmp ne i8* %33, null
br i1 %34, label %36, label %35
35: ; preds = %31
br label %91
36: ; preds = %31
%37 = load i8*, i8** %9
%38 = call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = load i8*, i8** %6
%40 = call i64 @strlen(i8* %39)
store i64 %40, i64* %12
%41 = load i8*, i8** %9
%42 = call i64 @strlen(i8* %41)
store i64 %42, i64* %13
%43 = load i64, i64* %12
%44 = add i64 %43, 5
%45 = load i64, i64* %13
%46 = add i64 %44, %45
%47 = add i64 %46, 1
store i64 %47, i64* %14
%48 = load i64, i64* %14
%49 = call i8* @malloc(i64 %48)
store i8* %49, i8** %10
%50 = load i8*, i8** %10
%51 = icmp ne i8* %50, null
br i1 %51, label %53, label %52
52: ; preds = %36
br label %91
53: ; preds = %36
%54 = load i8*, i8** %10
%55 = load i64, i64* %14
%56 = call i32 @memset(i8* %54, i32 0, i64 %55)
%57 = load i8*, i8** %10
%58 = load i8*, i8** %6
%59 = load i64, i64* %12
%60 = call i32 @memcpy(i8* %57, i8* %58, i64 %59)
%61 = load i8*, i8** %10
%62 = load i64, i64* %12
%63 = getelementptr inbounds i8, i8* %61, i64 %62
%64 = call i32 @memcpy(i8* %63, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 5)
%65 = load i8*, i8** %10
%66 = load i64, i64* %12
%67 = getelementptr inbounds i8, i8* %65, i64 %66
%68 = getelementptr inbounds i8, i8* %67, i64 5
%69 = load i8*, i8** %9
%70 = load i64, i64* %13
%71 = call i32 @memcpy(i8* %68, i8* %69, i64 %70)
%72 = load i8*, i8** %10
%73 = call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %72)
%74 = load i8*, i8** %5
%75 = load i8*, i8** %10
%76 = call i64 @link(i8* %74, i8* %75)
%77 = icmp ne i64 %76, 0
br i1 %77, label %78, label %83
78: ; preds = %53
%79 = load i8*, i8** %6
%80 = load i32, i32* @errno
%81 = call i32 @strerror(i32 %80)
%82 = call i32 @log_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %79, i32 %81)
br label %90
83: ; preds = %53
store i32 0, i32* %8
%84 = load i8**, i8*** %7
%85 = icmp ne i8** %84, null
br i1 %85, label %86, label %89
86: ; preds = %83
%87 = load i8*, i8** %10
%88 = load i8**, i8*** %7
store i8* %87, i8** %88
store i8* null, i8** %10
br label %89
89: ; preds = %86, %83
br label %90
90: ; preds = %89, %78
br label %91
91: ; preds = %90, %52, %35
%92 = load i8*, i8** %9
%93 = icmp ne i8* %92, null
br i1 %93, label %94, label %97
94: ; preds = %91
%95 = load i8*, i8** %9
%96 = call i32 @free(i8* %95)
br label %97
97: ; preds = %94, %91
%98 = load i8*, i8** %10
%99 = icmp ne i8* %98, null
br i1 %99, label %100, label %103
100: ; preds = %97
%101 = load i8*, i8** %10
%102 = call i32 @free(i8* %101)
br label %103
103: ; preds = %100, %97
%104 = load i32, i32* %8
store i32 %104, i32* %4
br label %105
105: ; preds = %103, %26, %21
%106 = load i32, i32* %4
ret i32 %106
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [33 x i8] c"delivering file %s to maildir %s\00"
@.str.1 = [16 x i8] c"maildir: %s: %s\00"
@errno = global i32 0
@.str.2 = [33 x i8] c"delivering mail with filename %s\00"
@.str.3 = [6 x i8] c"/new/\00"
@.str.4 = [27 x i8] c"delivering mail to file %s\00"
@.str.5 = [26 x i8] c"delivery to %s failed: %s\00"
define i32 @maildir_deliver_link(i8* %0, i8* %1, i8** writeonly %2) {
%4 = alloca %struct.stat
%5 = tail call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%6 = tail call i64 @maildir_ensure_exists(i8* %1)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %53
8: ; preds = %3
%9 = bitcast %struct.stat* %4 to i8*
%10 = call i64 @stat(i8* %0, %struct.stat* %4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 @log_error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %14)
br label %51
16: ; preds = %8
%17 = call i8* @md_make_filename(%struct.stat* %4)
%18 = icmp eq i8* %17, null
br i1 %18, label %51, label %19
19: ; preds = %16
%20 = call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%21 = call i64 @strlen(i8* dereferenceable(1) %1)
%22 = call i64 @strlen(i8* %17)
%23 = add i64 %21, 6
%24 = add i64 %23, %22
%25 = call i8* @malloc(i64 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %44, label %27
27: ; preds = %19
%28 = call i32 @memset(i8* %25, i32 0, i64 %24)
%29 = call i32 @memcpy(i8* %25, i8* %1, i64 %21)
%30 = getelementptr inbounds i8, i8* %25, i64 %21
%31 = call i32 @memcpy(i8* %30, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 5)
%32 = getelementptr inbounds i8, i8* %30, i64 5
%33 = call i32 @memcpy(i8* %32, i8* %17, i64 %22)
%34 = call i32 (i8*, i8*, ...) @log_debug(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %25)
%35 = call i64 @link(i8* %0, i8* %25)
%36 = icmp eq i64 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %27
%38 = load i32, i32* @errno
%39 = call i32 @strerror(i32 %38)
%40 = call i32 @log_error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0), i8* %1, i32 %39)
br label %47
41: ; preds = %27
%42 = icmp eq i8** %2, null
br i1 %42, label %47, label %43
43: ; preds = %41
store i8* %25, i8** %2
br label %44
44: ; preds = %19, %43
%45 = phi i32 [ 0, %43 ], [ -1, %19 ]
%46 = call i32 @free(i8* %17)
br label %51
47: ; preds = %37, %41
%48 = phi i32 [ 0, %41 ], [ -1, %37 ]
%49 = call i32 @free(i8* %17)
%50 = call i32 @free(i8* %25)
br label %51
51: ; preds = %16, %44, %47, %12
%52 = phi i32 [ -1, %12 ], [ %48, %47 ], [ %45, %44 ], [ -1, %16 ]
br label %53
53: ; preds = %3, %51
%54 = phi i32 [ %52, %51 ], [ -1, %3 ]
ret i32 %54
}