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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ write(int, void *, scalar_t__);
ssize_t writen(int rfd, void *buf, size_t size) {
  ssize_t nleft = size;
  ssize_t nwrite = 0;
  printf("sizeof(buf) = %d\n", sizeof(buf));
  while (nleft > 0) {
    if ((nwrite = write(rfd, buf, nleft)) == -1) {
      perror("write");
      close(rfd);
      exit(1);
    } else if (nwrite == 0) {
      break;
    }
    nleft -= nwrite;
    buf += nwrite;
  }
  return size - nleft;
}


x86 assembly (clang, -O0):
<writen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe86(%rip),%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
jle    1200 <writen+0xb0>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11cb <writen+0x7b>
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jne    11db <writen+0x8b>
jmp    1200 <writen+0xb0>
jmp    11e0 <writen+0x90>
mov    -0x28(%rbp),%rcx
mov    -0x20(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1184 <writen+0x34>
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%r15d
lea    0xe99(%rip),%rdi
mov    $0x8,%esi
call   1040 <printf@plt>
mov    %r14,%rbp
test   %r14,%r14
jle    11a1 <writen+0x51>
mov    %r14,%rbp
nopl   0x0(%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
je     11a1 <writen+0x51>
cmp    $0xffffffffffffffff,%rax
je     11b2 <writen+0x62>
add    %rax,%rbx
sub    %rax,%rbp
jg     1180 <writen+0x30>
sub    %rbp,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"sizeof(buf) = %d\0A\00"
@.str.1 = [6 x i8] c"write\00"
define i64 @writen(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i64, i64* %6
store i64 %9, i64* %7
store i64 0, i64* %8
%10 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 8)
br label %11
11: ; preds = %30, %3
%12 = load i64, i64* %7
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %37
14: ; preds = %11
%15 = load i32, i32* %4
%16 = load i8*, i8** %5
%17 = load i64, i64* %7
%18 = call i64 @write(i32 %15, i8* %16, i64 %17)
store i64 %18, i64* %8
%19 = icmp eq i64 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %14
%26 = load i64, i64* %8
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %25
br label %37
29: ; preds = %25
br label %30
30: ; preds = %29
%31 = load i64, i64* %8
%32 = load i64, i64* %7
%33 = sub nsw i64 %32, %31
store i64 %33, i64* %7
%34 = load i64, i64* %8
%35 = load i8*, i8** %5
%36 = getelementptr i8, i8* %35, i64 %34
store i8* %36, i8** %5
br label %11
37: ; preds = %28, %11
%38 = load i64, i64* %6
%39 = load i64, i64* %7
%40 = sub i64 %38, %39
ret i64 %40
}

LLVM IR (-O2):
@.str = [18 x i8] c"sizeof(buf) = %d\0A\00"
@.str.1 = [6 x i8] c"write\00"
define i64 @writen(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 8)
%5 = icmp sgt i64 %2, 0
br i1 %5, label %6, label %18
6: ; preds = %3, %14
%7 = phi i64 [ %15, %14 ], [ %2, %3 ]
%8 = phi i8* [ %16, %14 ], [ %1, %3 ]
%9 = tail call i64 @write(i32 %0, i8* %8, i64 %7)
switch i64 %9, label %14 [
i64 -1, label %10
i64 0, label %18
]
10: ; preds = %6
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @close(i32 %0)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %6
%15 = sub nsw i64 %7, %9
%16 = getelementptr i8, i8* %8, i64 %9
%17 = icmp sgt i64 %15, 0
br i1 %17, label %6, label %18
18: ; preds = %14, %6, %3
%19 = phi i64 [ %2, %3 ], [ %7, %6 ], [ %15, %14 ]
%20 = sub i64 %2, %19
ret i64 %20
}