#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 */

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SWAPL(int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int awritechar(char *file, char *data, int length) {
  int fd;
  int size;
  if ((fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0644)) < 0) {
    fprintf(stderr, "awritechar: %s: can't create\n", file);
    return -1;
  }
  SWAPL(&length);
  if (write(fd, (char *)&length, 4) != 4) {
    fprintf(stderr, "awritechar: %s: can't write length\n", file);
    close(fd);
    return -1;
  }
  SWAPL(&length);
  size = length * sizeof(char);
  if (write(fd, data, size) != size) {
    fprintf(stderr, "awritechar: %s: can't write data\n", file);
    close(fd);
    return (-1);
  }
  printf("Wrote %d chars in %s.\n", length, file);
  close(fd);
  return length;
}


x86 assembly (clang, -O0):
<awritechar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
mov    0x2e60(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    11cd <awritechar+0x6d>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe44(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <awritechar+0x137>
lea    -0x1c(%rbp),%rdi
call   1080 <SWAPL@plt>
mov    -0x20(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
je     121d <awritechar+0xbd>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <awritechar+0x137>
lea    -0x1c(%rbp),%rdi
call   1080 <SWAPL@plt>
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1030 <write@plt>
cmp    -0x24(%rbp),%eax
je     1276 <awritechar+0x116>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde5(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1297 <awritechar+0x137>
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xde0(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<awritechar>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e84(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %edx,0x4(%rsp)
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1070 <open@plt>
test   %eax,%eax
js     11fb <awritechar+0x9b>
mov    %eax,%ebp
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <SWAPL@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x4,%eax
jne    1215 <awritechar+0xb5>
lea    0x4(%rsp),%rdi
call   1080 <SWAPL@plt>
mov    0x4(%rsp),%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    1227 <awritechar+0xc7>
mov    0x4(%rsp),%esi
lea    0xe7e(%rip),%rdi
mov    %r15,%rdx
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x4(%rsp),%eax
jmp    124b <awritechar+0xeb>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
jmp    1246 <awritechar+0xe6>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
jmp    1237 <awritechar+0xd7>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xe0b(%rip),%rsi
mov    %r15,%rdx
call   1060 <fprintf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"awritechar: %s: can't create\0A\00"
@.str.1 = [36 x i8] c"awritechar: %s: can't write length\0A\00"
@.str.2 = [34 x i8] c"awritechar: %s: can't write data\0A\00"
@.str.3 = [23 x i8] c"Wrote %d chars in %s.\0A\00"
define i32 @awritechar(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 420)
store i32 %16, i32* %8
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %5
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 -1, i32* %4
br label %59
22: ; preds = %3
%23 = call i32 @SWAPL(i32* %7)
%24 = load i32, i32* %8
%25 = bitcast i32* %7 to i8*
%26 = call i32 @write(i32 %24, i8* %25, i32 4)
%27 = icmp ne i32 %26, 4
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %5
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = load i32, i32* %8
%33 = call i32 @close(i32 %32)
store i32 -1, i32* %4
br label %59
34: ; preds = %22
%35 = call i32 @SWAPL(i32* %7)
%36 = load i32, i32* %7
%37 = sext i32 %36 to i64
%38 = mul i64 %37, 1
%39 = trunc i64 %38 to i32
store i32 %39, i32* %9
%40 = load i32, i32* %8
%41 = load i8*, i8** %6
%42 = load i32, i32* %9
%43 = call i32 @write(i32 %40, i8* %41, i32 %42)
%44 = load i32, i32* %9
%45 = icmp ne i32 %43, %44
br i1 %45, label %46, label %52
46: ; preds = %34
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %5
%49 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %4
br label %59
52: ; preds = %34
%53 = load i32, i32* %7
%54 = load i8*, i8** %5
%55 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %53, i8* %54)
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
%58 = load i32, i32* %7
store i32 %58, i32* %4
br label %59
59: ; preds = %52, %46, %28, %18
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [30 x i8] c"awritechar: %s: can't create\0A\00"
@.str.1 = [36 x i8] c"awritechar: %s: can't write length\0A\00"
@.str.2 = [34 x i8] c"awritechar: %s: can't write data\0A\00"
@.str.3 = [23 x i8] c"Wrote %d chars in %s.\0A\00"
define i32 @awritechar(i8* %0, i8* nocapture %1, i32 %2) {
%4 = alloca i32
store i32 %2, i32* %4
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 420)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %38
15: ; preds = %3
%16 = call i32 @SWAPL(i32* %4)
%17 = bitcast i32* %4 to i8*
%18 = call i32 @write(i32 %10, i8* %17, i32 4)
%19 = icmp eq i32 %18, 4
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%23 = call i32 @close(i32 %10)
br label %38
24: ; preds = %15
%25 = call i32 @SWAPL(i32* %4)
%26 = load i32, i32* %4
%27 = call i32 @write(i32 %10, i8* %1, i32 %26)
%28 = icmp eq i32 %27, %26
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%32 = call i32 @close(i32 %10)
br label %38
33: ; preds = %24
%34 = load i32, i32* %4
%35 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %0)
%36 = call i32 @close(i32 %10)
%37 = load i32, i32* %4
br label %38
38: ; preds = %33, %29, %20, %12
%39 = phi i32 [ -1, %12 ], [ -1, %20 ], [ -1, %29 ], [ %37, %33 ]
ret i32 %39
}