#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fwrite(unsigned char *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ uint16_to_buff(unsigned short, unsigned char *,
                                  unsigned int *);
int /*<<< orphan*/ uint32_to_buff(unsigned long, unsigned char *,
                                  unsigned int *);
int /*<<< orphan*/ uint8_to_buff(unsigned char, unsigned char *,
                                 unsigned int *);
int f_write(void *inptr, size_t size, size_t nmemb, FILE *stream) {
  unsigned int num, pos, count;
  unsigned char tmp[8];
  switch (size) {
  default:
  case 1:
    count = 0;
    for (num = 0; num < nmemb; num++) {
      pos = 0;
      uint8_to_buff(((unsigned char *)inptr)[num], tmp, &pos);
      if (fwrite(tmp, size, 1, stream) != 1)
        break;
      count++;
    }
    break;
    break;
  case 2:
    count = 0;
    for (num = 0; num < nmemb; num++) {
      pos = 0;
      uint16_to_buff(((unsigned short *)inptr)[num], tmp, &pos);
      if (fwrite(tmp, size, 1, stream) != 1)
        break;
      count++;
    }
    break;
  case 4:
    count = 0;
    for (num = 0; num < nmemb; num++) {
      pos = 0;
      uint32_to_buff(((unsigned long *)inptr)[num], tmp, &pos);
      if (fwrite(tmp, size, 1, stream) != 1)
        break;
      count++;
    }
    break;
  }
  if (count != nmemb)
    fprintf(stderr, "short write\n");
  return count;
}


x86 assembly (clang, -O0):
<f_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
sub    $0x1,%rax
je     11aa <f_write+0x5a>
jmp    117f <f_write+0x2f>
mov    -0x40(%rbp),%rax
sub    $0x2,%rax
je     1226 <f_write+0xd6>
jmp    1192 <f_write+0x42>
mov    -0x40(%rbp),%rax
sub    $0x4,%rax
je     12a3 <f_write+0x153>
jmp    11a5 <f_write+0x55>
jmp    11aa <f_write+0x5a>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    1221 <f_write+0xd1>
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,1),%al
lea    -0x34(%rbp),%rsi
lea    -0x28(%rbp),%rdx
movzbl %al,%edi
call   1060 <uint8_to_buff@plt>
lea    -0x34(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
je     120a <f_write+0xba>
jmp    1221 <f_write+0xd1>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11b8 <f_write+0x68>
jmp    131d <f_write+0x1cd>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    129e <f_write+0x14e>
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,2),%ax
lea    -0x34(%rbp),%rsi
lea    -0x28(%rbp),%rdx
movzwl %ax,%edi
call   1030 <uint16_to_buff@plt>
lea    -0x34(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
je     1287 <f_write+0x137>
jmp    129e <f_write+0x14e>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1234 <f_write+0xe4>
jmp    131d <f_write+0x1cd>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%rax
jae    1318 <f_write+0x1c8>
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    (%rax,%rcx,8),%rdi
lea    -0x34(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <uint32_to_buff@plt>
lea    -0x34(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
je     1301 <f_write+0x1b1>
jmp    1318 <f_write+0x1c8>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    12b1 <f_write+0x161>
jmp    131d <f_write+0x1cd>
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%rax
je     133f <f_write+0x1ef>
mov    0x2caf(%rip),%rax
mov    (%rax),%edi
lea    0xcc6(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<f_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rdi,%r12
cmp    $0x4,%rsi
je     11d3 <f_write+0x83>
mov    %rsi,%r13
xor    %ebx,%ebx
cmp    $0x2,%rsi
jne    122b <f_write+0xdb>
test   %r14,%r14
je     127a <f_write+0x12a>
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbp
nop
movl   $0x0,0xc(%rsp)
movzwl (%r12,%rbx,2),%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <uint16_to_buff@plt>
mov    $0x2,%esi
mov    %r13,%rdi
mov    $0x1,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jne    127a <f_write+0x12a>
add    $0x1,%ebx
cmp    %r14,%rbx
jb     1190 <f_write+0x40>
jmp    127a <f_write+0x12a>
xor    %ebx,%ebx
test   %r14,%r14
je     127a <f_write+0x12a>
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbp
nopl   0x0(%rax,%rax,1)
movl   $0x0,0xc(%rsp)
mov    (%r12,%rbx,8),%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1050 <uint32_to_buff@plt>
mov    $0x4,%esi
mov    %r13,%rdi
mov    $0x1,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jne    127a <f_write+0x12a>
add    $0x1,%ebx
cmp    %r14,%rbx
jb     11f0 <f_write+0xa0>
jmp    127a <f_write+0x12a>
test   %r14,%r14
je     127a <f_write+0x12a>
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
movl   $0x0,0xc(%rsp)
movzbl (%r12,%rbx,1),%edi
mov    %rbp,%rsi
lea    0xc(%rsp),%rdx
call   1060 <uint8_to_buff@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%eax
jne    127a <f_write+0x12a>
add    $0x1,%ebx
cmp    %r14,%rbx
jb     1240 <f_write+0xf0>
mov    %ebx,%eax
cmp    %r14,%rax
je     1296 <f_write+0x146>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
call   1040 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [13 x i8] c"short write\0A\00"
define i32 @f_write(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [8 x i8]
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%13 = load i64, i64* %6
switch i64 %13, label %14 [
i64 1, label %15
i64 2, label %42
i64 4, label %70
]
14: ; preds = %4
br label %15
15: ; preds = %4, %14
store i32 0, i32* %11
store i32 0, i32* %9
br label %16
16: ; preds = %38, %15
%17 = load i32, i32* %9
%18 = zext i32 %17 to i64
%19 = load i64, i64* %7
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %41
21: ; preds = %16
store i32 0, i32* %10
%22 = load i8*, i8** %5
%23 = load i32, i32* %9
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = load i8, i8* %25
%27 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
%28 = call i32 @uint8_to_buff(i8 zeroext %26, i8* %27, i32* %10)
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
%30 = load i64, i64* %6
%31 = load i32*, i32** %8
%32 = call i32 @fwrite(i8* %29, i64 %30, i32 1, i32* %31)
%33 = icmp ne i32 %32, 1
br i1 %33, label %34, label %35
34: ; preds = %21
br label %41
35: ; preds = %21
%36 = load i32, i32* %11
%37 = add i32 %36, 1
store i32 %37, i32* %11
br label %38
38: ; preds = %35
%39 = load i32, i32* %9
%40 = add i32 %39, 1
store i32 %40, i32* %9
br label %16
41: ; preds = %34, %16
br label %98
42: ; preds = %4
store i32 0, i32* %11
store i32 0, i32* %9
br label %43
43: ; preds = %66, %42
%44 = load i32, i32* %9
%45 = zext i32 %44 to i64
%46 = load i64, i64* %7
%47 = icmp ult i64 %45, %46
br i1 %47, label %48, label %69
48: ; preds = %43
store i32 0, i32* %10
%49 = load i8*, i8** %5
%50 = bitcast i8* %49 to i16*
%51 = load i32, i32* %9
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds i16, i16* %50, i64 %52
%54 = load i16, i16* %53
%55 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
%56 = call i32 @uint16_to_buff(i16 zeroext %54, i8* %55, i32* %10)
%57 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
%58 = load i64, i64* %6
%59 = load i32*, i32** %8
%60 = call i32 @fwrite(i8* %57, i64 %58, i32 1, i32* %59)
%61 = icmp ne i32 %60, 1
br i1 %61, label %62, label %63
62: ; preds = %48
br label %69
63: ; preds = %48
%64 = load i32, i32* %11
%65 = add i32 %64, 1
store i32 %65, i32* %11
br label %66
66: ; preds = %63
%67 = load i32, i32* %9
%68 = add i32 %67, 1
store i32 %68, i32* %9
br label %43
69: ; preds = %62, %43
br label %98
70: ; preds = %4
store i32 0, i32* %11
store i32 0, i32* %9
br label %71
71: ; preds = %94, %70
%72 = load i32, i32* %9
%73 = zext i32 %72 to i64
%74 = load i64, i64* %7
%75 = icmp ult i64 %73, %74
br i1 %75, label %76, label %97
76: ; preds = %71
store i32 0, i32* %10
%77 = load i8*, i8** %5
%78 = bitcast i8* %77 to i64*
%79 = load i32, i32* %9
%80 = zext i32 %79 to i64
%81 = getelementptr inbounds i64, i64* %78, i64 %80
%82 = load i64, i64* %81
%83 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
%84 = call i32 @uint32_to_buff(i64 %82, i8* %83, i32* %10)
%85 = getelementptr inbounds [8 x i8], [8 x i8]* %12, i64 0, i64 0
%86 = load i64, i64* %6
%87 = load i32*, i32** %8
%88 = call i32 @fwrite(i8* %85, i64 %86, i32 1, i32* %87)
%89 = icmp ne i32 %88, 1
br i1 %89, label %90, label %91
90: ; preds = %76
br label %97
91: ; preds = %76
%92 = load i32, i32* %11
%93 = add i32 %92, 1
store i32 %93, i32* %11
br label %94
94: ; preds = %91
%95 = load i32, i32* %9
%96 = add i32 %95, 1
store i32 %96, i32* %9
br label %71
97: ; preds = %90, %71
br label %98
98: ; preds = %97, %69, %41
%99 = load i32, i32* %11
%100 = zext i32 %99 to i64
%101 = load i64, i64* %7
%102 = icmp ne i64 %100, %101
br i1 %102, label %103, label %106
103: ; preds = %98
%104 = load i32, i32* @stderr
%105 = call i32 @fprintf(i32 %104, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %106
106: ; preds = %103, %98
%107 = load i32, i32* %11
ret i32 %107
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [13 x i8] c"short write\0A\00"
define i32 @f_write(i8* nocapture %0, i64 %1, i64 %2, i32* nocapture %3) {
%5 = alloca i32
%6 = alloca [8 x i8]
%7 = bitcast i32* %5 to i8*
%8 = getelementptr inbounds [8 x i8], [8 x i8]* %6, i64 0, i64 0
switch i64 %1, label %17 [
i64 4, label %13
i64 2, label %9
]
9: ; preds = %4
%10 = icmp eq i64 %2, 0
br i1 %10, label %61, label %11
11: ; preds = %9
%12 = bitcast i8* %0 to i16*
br label %31
13: ; preds = %4
%14 = icmp eq i64 %2, 0
br i1 %14, label %61, label %15
15: ; preds = %13
%16 = bitcast i8* %0 to i64*
br label %43
17: ; preds = %4
%18 = icmp eq i64 %2, 0
br i1 %18, label %61, label %19
19: ; preds = %17, %27
%20 = phi i64 [ %29, %27 ], [ 0, %17 ]
store i32 0, i32* %5
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = call i32 @uint8_to_buff(i8 zeroext %22, i8* %8, i32* %5)
%24 = call i32 @fwrite(i8* %8, i64 %1, i32 1, i32* %3)
%25 = icmp eq i32 %24, 1
%26 = trunc i64 %20 to i32
br i1 %25, label %27, label %55
27: ; preds = %19
%28 = add i32 %26, 1
%29 = zext i32 %28 to i64
%30 = icmp ult i64 %29, %2
br i1 %30, label %19, label %61
31: ; preds = %11, %39
%32 = phi i64 [ 0, %11 ], [ %41, %39 ]
store i32 0, i32* %5
%33 = getelementptr inbounds i16, i16* %12, i64 %32
%34 = load i16, i16* %33
%35 = call i32 @uint16_to_buff(i16 zeroext %34, i8* %8, i32* %5)
%36 = call i32 @fwrite(i8* %8, i64 2, i32 1, i32* %3)
%37 = icmp eq i32 %36, 1
%38 = trunc i64 %32 to i32
br i1 %37, label %39, label %59
39: ; preds = %31
%40 = add i32 %38, 1
%41 = zext i32 %40 to i64
%42 = icmp ult i64 %41, %2
br i1 %42, label %31, label %61
43: ; preds = %15, %51
%44 = phi i64 [ 0, %15 ], [ %53, %51 ]
store i32 0, i32* %5
%45 = getelementptr inbounds i64, i64* %16, i64 %44
%46 = load i64, i64* %45
%47 = call i32 @uint32_to_buff(i64 %46, i8* %8, i32* %5)
%48 = call i32 @fwrite(i8* %8, i64 4, i32 1, i32* %3)
%49 = icmp eq i32 %48, 1
%50 = trunc i64 %44 to i32
br i1 %49, label %51, label %57
51: ; preds = %43
%52 = add i32 %50, 1
%53 = zext i32 %52 to i64
%54 = icmp ult i64 %53, %2
br i1 %54, label %43, label %61
55: ; preds = %19
%56 = trunc i64 %20 to i32
br label %61
57: ; preds = %43
%58 = trunc i64 %44 to i32
br label %61
59: ; preds = %31
%60 = trunc i64 %32 to i32
br label %61
61: ; preds = %59, %39, %57, %51, %55, %27, %9, %13, %17
%62 = phi i32 [ 0, %17 ], [ 0, %13 ], [ 0, %9 ], [ %56, %55 ], [ %28, %27 ], [ %58, %57 ], [ %52, %51 ], [ %60, %59 ], [ %40, %39 ]
%63 = zext i32 %62 to i64
%64 = icmp eq i64 %63, %2
br i1 %64, label %68, label %65
65: ; preds = %61
%66 = load i32, i32* @stderr
%67 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %68
68: ; preds = %65, %61
ret i32 %62
}