#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;
};

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRWXU;
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int S_IXGRP;
int S_IXOTH;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ net_printf(int, char *, ...);
int open(char *, int, int);
int read(int, unsigned char *, int);
scalar_t__ stat(char *, struct stat *);
int write(int, unsigned char *, int);
int ftpd_stor(int cs, int dp, char *filename) {
  struct stat sb;
  int j, totlen;
  if (dp < 0)
    return 1;
  if (stat(filename, &sb) == 0 && !S_ISREG(sb.st_mode)) {
    net_printf(cs, "523 Error Creating Destination File - does it already "
                   "exist read-only?\n");
    return 1;
  } else {
    int fh;
    int len;
    unsigned char buf[1024];
    net_printf(cs, "150 Ready to download %s\n", filename);
    fh = open(filename, O_WRONLY | O_CREAT,
              S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    if (fh < 0) {
      net_printf(cs, "523 Error Creating Destination File - does it already "
                     "exist read-only?\n");
      return 1;
    }
    j = 0;
    totlen = 0;
    while (j >= 0 && (len = read(dp, buf, 1024 - 1)) > 0) {
      j = write(fh, buf, len);
      if (j >= 0)
        totlen += j;
    }
    close(fh);
    if (len < 0 || j < 0)
      net_printf(cs,
                 "524 Download interrupted, created file may be incomplete\n");
    else
      net_printf(cs, "226 Transfer OK - %d bytes sent\n", totlen);
    return 0;
  }
}


x86 assembly (clang, -O0):
<ftpd_stor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    119b <ftpd_stor+0x2b>
movl   $0x1,-0x4(%rbp)
jmp    1337 <ftpd_stor+0x1c7>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jne    11e0 <ftpd_stor+0x70>
mov    -0x20(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    11e0 <ftpd_stor+0x70>
mov    -0x8(%rbp),%edi
lea    0xe33(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <ftpd_stor+0x1c7>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rdx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
mov    0x2def(%rip),%rax
or     (%rax),%esi
mov    0x2dce(%rip),%rax
mov    (%rax),%edx
mov    0x2da5(%rip),%rax
or     (%rax),%edx
mov    0x2dac(%rip),%rax
or     (%rax),%edx
mov    0x2d8b(%rip),%rax
or     (%rax),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1267 <ftpd_stor+0xf7>
mov    -0x8(%rbp),%edi
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1337 <ftpd_stor+0x1c7>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x431(%rbp)
jl     12aa <ftpd_stor+0x13a>
mov    -0xc(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,-0x30(%rbp)
cmp    $0x0,%eax
setg   %al
mov    %al,-0x431(%rbp)
mov    -0x431(%rbp),%al
test   $0x1,%al
jne    12bd <ftpd_stor+0x14d>
jmp    12ea <ftpd_stor+0x17a>
mov    -0x2c(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     12e5 <ftpd_stor+0x175>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    1275 <ftpd_stor+0x105>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x30(%rbp)
jl     1306 <ftpd_stor+0x196>
cmpl   $0x0,-0x24(%rbp)
jge    131c <ftpd_stor+0x1ac>
mov    -0x8(%rbp),%edi
lea    0xd52(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
jmp    1330 <ftpd_stor+0x1c0>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%edx
lea    0xd73(%rip),%rsi
mov    $0x0,%al
call   1090 <net_printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ftpd_stor>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    $0x1,%r12d
mov    %esi,0x4(%rsp)
test   %esi,%esi
js     12e7 <ftpd_stor+0x177>
mov    %rdx,%rbp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
mov    %rdx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
jne    11bb <ftpd_stor+0x4b>
mov    0x8(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     12d7 <ftpd_stor+0x167>
lea    0xe86(%rip),%rsi
xor    %r13d,%r13d
mov    %ebx,%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <net_printf@plt>
mov    0x2dd0(%rip),%rax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2df6(%rip),%rax
mov    0x2dcf(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd4(%rip),%rax
or     (%rax),%edx
mov    0x2db3(%rip),%rax
or     (%rax),%edx
mov    0x2dca(%rip),%rax
or     (%rax),%edx
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12b0 <ftpd_stor+0x140>
mov    %eax,%r12d
mov    %ebx,(%rsp)
lea    0x10(%rsp),%rsi
mov    0x4(%rsp),%edi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,%ebp
xor    %ebx,%ebx
test   %eax,%eax
jle    128b <ftpd_stor+0x11b>
lea    0x10(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%r15d
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %eax,%r13d
test   %eax,%eax
mov    $0x0,%ebx
cmovns %eax,%ebx
add    %r15d,%ebx
test   %eax,%eax
js     128b <ftpd_stor+0x11b>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     1250 <ftpd_stor+0xe0>
mov    %r12d,%edi
call   1040 <close@plt>
or     %r13d,%ebp
js     12c8 <ftpd_stor+0x158>
lea    0xdfd(%rip),%rsi
xor    %r12d,%r12d
mov    (%rsp),%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <net_printf@plt>
jmp    12e7 <ftpd_stor+0x177>
lea    0xd49(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1090 <net_printf@plt>
mov    $0x1,%r12d
jmp    12e7 <ftpd_stor+0x177>
lea    0xd93(%rip),%rsi
xor    %r12d,%r12d
mov    (%rsp),%edi
jmp    12e0 <ftpd_stor+0x170>
lea    0xd22(%rip),%rsi
mov    %ebx,%edi
xor    %eax,%eax
call   1090 <net_printf@plt>
mov    %r12d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [72 x i8] c"523 Error Creating Destination File - does it already exist read-only?\0A\00"
@.str.1 = [26 x i8] c"150 Ready to download %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@.str.2 = [58 x i8] c"524 Download interrupted, created file may be incomplete\0A\00"
@.str.3 = [33 x i8] c"226 Transfer OK - %d bytes sent\0A\00"
define i32 @ftpd_stor(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca [1024 x i8]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 1, i32* %4
br label %91
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = call i64 @stat(i8* %18, %struct.stat* %8)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %17
%22 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @S_ISREG(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = load i32, i32* %5
%28 = call i32 (i32, i8*, ...) @net_printf(i32 %27, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %91
29: ; preds = %21, %17
%30 = load i32, i32* %5
%31 = load i8*, i8** %7
%32 = call i32 (i32, i8*, ...) @net_printf(i32 %30, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %7
%34 = load i32, i32* @O_WRONLY
%35 = load i32, i32* @O_CREAT
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IRWXU
%38 = load i32, i32* @S_IRGRP
%39 = or i32 %37, %38
%40 = load i32, i32* @S_IXGRP
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IROTH
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IXOTH
%45 = or i32 %43, %44
%46 = call i32 @open(i8* %33, i32 %36, i32 %45)
store i32 %46, i32* %11
%47 = load i32, i32* %11
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %29
%50 = load i32, i32* %5
%51 = call i32 (i32, i8*, ...) @net_printf(i32 %50, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %4
br label %91
52: ; preds = %29
store i32 0, i32* %9
store i32 0, i32* %10
br label %53
53: ; preds = %74, %52
%54 = load i32, i32* %9
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %53
%57 = load i32, i32* %6
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 1023)
store i32 %59, i32* %12
%60 = icmp sgt i32 %59, 0
br label %61
61: ; preds = %56, %53
%62 = phi i1 [ false, %53 ], [ %60, %56 ]
br i1 %62, label %63, label %75
63: ; preds = %61
%64 = load i32, i32* %11
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%66 = load i32, i32* %12
%67 = call i32 @write(i32 %64, i8* %65, i32 %66)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp sge i32 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %63
%71 = load i32, i32* %9
%72 = load i32, i32* %10
%73 = add nsw i32 %72, %71
store i32 %73, i32* %10
br label %74
74: ; preds = %70, %63
br label %53
75: ; preds = %61
%76 = load i32, i32* %11
%77 = call i32 @close(i32 %76)
%78 = load i32, i32* %12
%79 = icmp slt i32 %78, 0
br i1 %79, label %83, label %80
80: ; preds = %75
%81 = load i32, i32* %9
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %86
83: ; preds = %80, %75
%84 = load i32, i32* %5
%85 = call i32 (i32, i8*, ...) @net_printf(i32 %84, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
br label %90
86: ; preds = %80
%87 = load i32, i32* %5
%88 = load i32, i32* %10
%89 = call i32 (i32, i8*, ...) @net_printf(i32 %87, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %88)
br label %90
90: ; preds = %86, %83
store i32 0, i32* %4
br label %91
91: ; preds = %90, %49, %26, %16
%92 = load i32, i32* %4
ret i32 %92
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [72 x i8] c"523 Error Creating Destination File - does it already exist read-only?\0A\00"
@.str.1 = [26 x i8] c"150 Ready to download %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRGRP = global i32 0
@S_IXGRP = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@.str.2 = [58 x i8] c"524 Download interrupted, created file may be incomplete\0A\00"
@.str.3 = [33 x i8] c"226 Transfer OK - %d bytes sent\0A\00"
define i32 @ftpd_stor(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca [1024 x i8]
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp slt i32 %1, 0
br i1 %7, label %64, label %8
8: ; preds = %3
%9 = call i64 @stat(i8* %2, %struct.stat* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @S_ISREG(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
br label %64
18: ; preds = %11, %8
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%20 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %22, %21
%24 = load i32, i32* @S_IRWXU
%25 = load i32, i32* @S_IRGRP
%26 = or i32 %25, %24
%27 = load i32, i32* @S_IXGRP
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IROTH
%30 = or i32 %28, %29
%31 = load i32, i32* @S_IXOTH
%32 = or i32 %30, %31
%33 = call i32 @open(i8* %2, i32 %23, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %18
%36 = call i32 @read(i32 %1, i8* %19, i32 1023)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %43, label %51
38: ; preds = %18
%39 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str, i64 0, i64 0))
br label %62
40: ; preds = %43
%41 = call i32 @read(i32 %1, i8* %19, i32 1023)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %35, %40
%44 = phi i32 [ %41, %40 ], [ %36, %35 ]
%45 = phi i32 [ %49, %40 ], [ 0, %35 ]
%46 = call i32 @write(i32 %33, i8* %19, i32 %44)
%47 = icmp sgt i32 %46, 0
%48 = select i1 %47, i32 %46, i32 0
%49 = add nuw nsw i32 %48, %45
%50 = icmp sgt i32 %46, -1
br i1 %50, label %40, label %51
51: ; preds = %40, %43, %35
%52 = phi i32 [ %36, %35 ], [ %41, %40 ], [ %44, %43 ]
%53 = phi i32 [ 0, %35 ], [ %46, %43 ], [ %46, %40 ]
%54 = phi i32 [ 0, %35 ], [ %49, %43 ], [ %49, %40 ]
%55 = call i32 @close(i32 %33)
%56 = or i32 %52, %53
%57 = icmp sgt i32 %56, -1
br i1 %57, label %60, label %58
58: ; preds = %51
%59 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
br label %62
60: ; preds = %51
%61 = call i32 (i32, i8*, ...) @net_printf(i32 %0, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i32 %54)
br label %62
62: ; preds = %58, %60, %38
%63 = phi i32 [ 1, %38 ], [ 0, %60 ], [ 0, %58 ]
br label %64
64: ; preds = %3, %62, %16
%65 = phi i32 [ %63, %62 ], [ 1, %16 ], [ 1, %3 ]
ret i32 %65
}