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

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ exit(int);
scalar_t__ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *const, int, int);
int /*<<< orphan*/ perror(char *const);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
size_t strlen(char *);
scalar_t__ writelocked(int, char *, size_t);
int main(int argc, char *const *argv) {
  int res = 1;
  char buffer[20 + 1];
  size_t size;
  ssize_t wres;
  int fd;
  if (argc != 2) {
    fprintf(stderr, "usage: %s outputfile\n", argv[0]);
    goto RETURN;
  }
  fd = open(argv[1], O_WRONLY | O_CREAT, 0666);
  if (fd < 0) {
    perror(argv[1]);
    goto RETURN;
  }
  while (fgets(buffer, sizeof(buffer) - 1, stdin)) {
    size = strlen(buffer);
    wres = writelocked(fd, buffer, size);
    if ((size_t)wres != size) {
      if (wres < 0) {
        perror("writelocked");
        break;
      }
      fprintf(stderr, "writelocked should write %lu bytes, but did %ld\n", size,
              wres);
    }
  }
  res = 0;
RETURN:
  exit(res);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ba <main+0x4a>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1299 <main+0x129>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    11fd <main+0x8d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <perror@plt>
jmp    1299 <main+0x129>
jmp    1202 <main+0x92>
lea    -0x30(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    $0x14,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1292 <main+0x122>
lea    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <writelocked@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     128d <main+0x11d>
cmpq   $0x0,-0x40(%rbp)
jge    126e <main+0xfe>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
jmp    1292 <main+0x122>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
lea    0xd9c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1202 <main+0x92>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1262 <main+0xf2>
mov    0x8(%rbx),%rdi
mov    0x2e2b(%rip),%rax
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1b6,%edx
call   1070 <open@plt>
test   %eax,%eax
jns    11c1 <main+0x51>
mov    0x8(%rbx),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%r15d
mov    0x2e15(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rdi
mov    $0x14,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     125b <main+0xeb>
mov    %rsp,%rbx
mov    0x2de5(%rip),%r13
lea    0xe30(%rip),%r14
jmp    1216 <main+0xa6>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1040 <fgets@plt>
test   %rax,%rax
je     125b <main+0xeb>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %rax,%rbp
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1050 <writelocked@plt>
cmp    %rbp,%rax
je     1200 <main+0x90>
test   %rax,%rax
js     124f <main+0xdf>
mov    0x0(%r13),%edi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1200 <main+0x90>
lea    0xdc0(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd8b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s outputfile\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stdin = global i32 0
@.str.1 = [12 x i8] c"writelocked\00"
@.str.2 = [49 x i8] c"writelocked should write %lu bytes, but did %ld\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [21 x i8]
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %62
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %22, i32 %25, i32 438)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %19
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @perror(i8* %32)
br label %62
34: ; preds = %19
br label %35
35: ; preds = %60, %34
%36 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* @stdin
%38 = call i64 @fgets(i8* %36, i32 20, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %61
40: ; preds = %35
%41 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%42 = call i64 @strlen(i8* %41)
store i64 %42, i64* %8
%43 = load i32, i32* %10
%44 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%45 = load i64, i64* %8
%46 = call i64 @writelocked(i32 %43, i8* %44, i64 %45)
store i64 %46, i64* %9
%47 = load i64, i64* %9
%48 = load i64, i64* %8
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %60
50: ; preds = %40
%51 = load i64, i64* %9
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %61
55: ; preds = %50
%56 = load i32, i32* @stderr
%57 = load i64, i64* %8
%58 = load i64, i64* %9
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i64 %57, i64 %58)
br label %60
60: ; preds = %55, %40
br label %35
61: ; preds = %53, %35
store i32 0, i32* %6
br label %62
62: ; preds = %61, %29, %13
%63 = load i32, i32* %6
%64 = call i32 @exit(i32 %63)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s outputfile\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@stdin = global i32 0
@.str.1 = [12 x i8] c"writelocked\00"
@.str.2 = [49 x i8] c"writelocked should write %lu bytes, but did %ld\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [21 x i8]
%4 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %40
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = tail call i32 @open(i8* %12, i32 %15, i32 438)
%17 = icmp slt i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %10
%19 = load i32, i32* @stdin
%20 = call i64 @fgets(i8* %4, i32 20, i32 %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %40, label %25
22: ; preds = %10
%23 = load i8*, i8** %11
%24 = tail call i32 @perror(i8* %23)
br label %40
25: ; preds = %18, %36
%26 = call i64 @strlen(i8* %4)
%27 = call i64 @writelocked(i32 %16, i8* %4, i64 %26)
%28 = icmp eq i64 %27, %26
br i1 %28, label %36, label %29
29: ; preds = %25
%30 = icmp slt i64 %27, 0
br i1 %30, label %31, label %33
31: ; preds = %29
%32 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %40
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i64 %26, i64 %27)
br label %36
36: ; preds = %33, %25
%37 = load i32, i32* @stdin
%38 = call i64 @fgets(i8* %4, i32 20, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %25
40: ; preds = %36, %18, %31, %22, %6
%41 = phi i32 [ 1, %6 ], [ 1, %22 ], [ 0, %31 ], [ 0, %18 ], [ 0, %36 ]
%42 = call i32 @exit(i32 %41)
unreachable
}