#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int read(int /*<<< orphan*/, char *, int);
long read_long();
int /*<<< orphan*/ send_errno(int /*<<< orphan*/);
int /*<<< orphan*/ send_success();
int /*<<< orphan*/ x_perror(char *);
void do_put(int argc, char **argv) {
  FILE *fh;
  assert(argc == 1);
  fh = fopen(argv[0], "w");
  if (fh != 0) {
    long size = read_long();
    long todo = size;
    while (todo > 0) {
      char buf[BUFSIZ];
      int this = (todo > BUFSIZ ? BUFSIZ : todo);
      this = read(0, buf, this);
      if (this < 0)
        x_perror("put-read");
      if (fwrite(buf, 1, this, fh) != this)
        x_perror("put-write");
      todo -= this;
    }
    fclose(fh);
    send_success();
  } else
    send_errno(errno);
}


x86 assembly (clang, -O0):
<do_put>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe42(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12d6 <do_put+0x146>
mov    $0x0,%al
call   1060 <read_long@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    12c1 <do_put+0x131>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2db9(%rip),%rcx
movslq (%rcx),%rcx
cmp    %rcx,%rax
jle    1246 <do_put+0xb6>
mov    0x2da6(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    124e <do_put+0xbe>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%edx
xor    %edi,%edi
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    127c <do_put+0xec>
lea    0xd8b(%rip),%rdi
call   1030 <x_perror@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
cmp    -0x3c(%rbp),%eax
je     12a6 <do_put+0x116>
lea    0xd6a(%rip),%rdi
call   1030 <x_perror@plt>
movslq -0x3c(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
jmp    11e5 <do_put+0x55>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    $0x0,%al
call   1080 <send_success@plt>
jmp    12e4 <do_put+0x154>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
call   10b0 <send_errno@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_put>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %eax,%eax
cmp    $0x1,%edi
sete   %al
mov    %eax,%edi
call   1070 <assert@plt>
mov    (%rbx),%rdi
lea    0xe46(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x30(%rbp)
test   %rax,%rax
je     127f <do_put+0xef>
xor    %eax,%eax
call   1060 <read_long@plt>
test   %rax,%rax
jle    1261 <do_put+0xd1>
mov    %rax,%r13
mov    0x2dfa(%rip),%r12
jmp    11fb <do_put+0x6b>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rsp
movslq %ebx,%rax
sub    %rax,%r13
jle    1261 <do_put+0xd1>
mov    %rsp,%r14
mov    (%r12),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
movslq (%r12),%rax
cmp    %rax,%r13
mov    %r13d,%edx
cmovg  %eax,%edx
xor    %edi,%edi
mov    %r15,%rsi
call   1050 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    123c <do_put+0xac>
lea    0xdcb(%rip),%rdi
call   1030 <x_perror@plt>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    -0x30(%rbp),%rcx
call   10a0 <fwrite@plt>
cmp    %ebx,%eax
je     11f0 <do_put+0x60>
lea    0xdb1(%rip),%rdi
call   1030 <x_perror@plt>
jmp    11f0 <do_put+0x60>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <send_success@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10b0 <send_errno@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@BUFSIZ = global i32 0
@.str.1 = [9 x i8] c"put-read\00"
@.str.2 = [10 x i8] c"put-write\00"
@errno = global i32 0
define void @do_put(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 1
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8**, i8*** %4
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %5
%19 = load i32*, i32** %5
%20 = icmp ne i32* %19, null
br i1 %20, label %21, label %68
21: ; preds = %2
%22 = call i64 (...) @read_long()
store i64 %22, i64* %6
%23 = load i64, i64* %6
store i64 %23, i64* %7
br label %24
24: ; preds = %58, %21
%25 = load i64, i64* %7
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %64
27: ; preds = %24
%28 = load i32, i32* @BUFSIZ
%29 = zext i32 %28 to i64
%30 = call i8* @llvm.stacksave()
store i8* %30, i8** %8
%31 = alloca i8, i64 %29
store i64 %29, i64* %9
%32 = load i64, i64* %7
%33 = load i32, i32* @BUFSIZ
%34 = sext i32 %33 to i64
%35 = icmp sgt i64 %32, %34
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = load i32, i32* @BUFSIZ
%38 = sext i32 %37 to i64
br label %41
39: ; preds = %27
%40 = load i64, i64* %7
br label %41
41: ; preds = %39, %36
%42 = phi i64 [ %38, %36 ], [ %40, %39 ]
%43 = trunc i64 %42 to i32
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = call i32 @read(i32 0, i8* %31, i32 %44)
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @x_perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %41
%51 = load i32, i32* %10
%52 = load i32*, i32** %5
%53 = call i32 @fwrite(i8* %31, i32 1, i32 %51, i32* %52)
%54 = load i32, i32* %10
%55 = icmp ne i32 %53, %54
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = call i32 @x_perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %56, %50
%59 = load i32, i32* %10
%60 = sext i32 %59 to i64
%61 = load i64, i64* %7
%62 = sub nsw i64 %61, %60
store i64 %62, i64* %7
%63 = load i8*, i8** %8
br label %24
64: ; preds = %24
%65 = load i32*, i32** %5
%66 = call i32 @fclose(i32* %65)
%67 = call i32 (...) @send_success()
br label %71
68: ; preds = %2
%69 = load i32, i32* @errno
%70 = call i32 @send_errno(i32 %69)
br label %71
71: ; preds = %68, %64
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@BUFSIZ = global i32 0
@.str.1 = [9 x i8] c"put-read\00"
@.str.2 = [10 x i8] c"put-write\00"
@errno = global i32 0
define void @do_put(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 1
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = load i8*, i8** %1
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %39, label %9
9: ; preds = %2
%10 = tail call i64 (...) @read_long()
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %36
12: ; preds = %9, %32
%13 = phi i64 [ %34, %32 ], [ %10, %9 ]
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = tail call i8* @llvm.stacksave()
%17 = alloca i8, i64 %15
%18 = load i32, i32* @BUFSIZ
%19 = sext i32 %18 to i64
%20 = icmp sgt i64 %13, %19
%21 = select i1 %20, i64 %19, i64 %13
%22 = trunc i64 %21 to i32
%23 = call i32 @read(i32 0, i8* %17, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %12
%26 = tail call i32 @x_perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %12
%28 = call i32 @fwrite(i8* %17, i32 1, i32 %23, i32* %7)
%29 = icmp eq i32 %28, %23
br i1 %29, label %32, label %30
30: ; preds = %27
%31 = tail call i32 @x_perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
%33 = sext i32 %23 to i64
%34 = sub nsw i64 %13, %33
tail call void @llvm.stackrestore(i8* %16)
%35 = icmp sgt i64 %34, 0
br i1 %35, label %12, label %36
36: ; preds = %32, %9
%37 = tail call i32 @fclose(i32* %7)
%38 = tail call i32 (...) @send_success()
br label %42
39: ; preds = %2
%40 = load i32, i32* @errno
%41 = tail call i32 @send_errno(i32 %40)
br label %42
42: ; preds = %39, %36
ret void
}