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

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int flock(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int fwrite(char *, size_t, int, int /*<<< orphan*/ *);
int getline(char **, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  char *line;
  FILE *fd;
  ssize_t res;
  size_t n;
  int fres;
  if (2 != argc) {
    fprintf(stderr, "usage: %s <file>\n", argv[0]);
    return 1;
  }
  fd = fopen(argv[1], "w+");
  if (NULL == fd) {
    perror("fopen");
    return 1;
  }
  printf("acquiring lock...");
  fflush(stdout);
  fres = flock(fileno(fd), LOCK_EX);
  if (-1 == fres) {
    perror("flock");
    return 1;
  }
  printf("done\n");
  printf("Enter data: ");
  line = NULL;
  n = 0;
  res = getline(&line, &n, stdin);
  if (-1 == res) {
    perror("getline");
    return 1;
  }
  res = fwrite(line, n, 1, fd);
  free(line);
  printf("releasing lock...");
  fres = flock(fileno(fd), LOCK_UN);
  if (-1 == fres) {
    perror("flock");
    return 1;
  }
  printf("done\n");
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11fc <main+0x4c>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe15(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1d7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe07(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    1238 <main+0x88>
lea    0xdee(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1d7>
lea    0xddc(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
call   10b0 <flock@plt>
mov    %eax,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    -0x34(%rbp),%eax
jne    1294 <main+0xe4>
lea    0xdaa(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1d7>
lea    0xd98(%rip),%rdi
call   1050 <printf@plt>
lea    0xd92(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2d1d(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10c0 <getline@plt>
mov    %eax,-0x24(%rbp)
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    12fb <main+0x14b>
lea    0xd5c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1d7>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   10d0 <fwrite@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xd2a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fileno@plt>
mov    %eax,%edi
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
call   10b0 <flock@plt>
mov    %eax,-0x34(%rbp)
mov    $0xffffffff,%eax
cmp    -0x34(%rbp),%eax
jne    136b <main+0x1bb>
lea    0xcd3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1387 <main+0x1d7>
lea    0xcc1(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    12d8 <main+0x118>
mov    0x8(%rsi),%rdi
lea    0xe38(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12fe <main+0x13e>
mov    %rax,%rbx
lea    0xe29(%rip),%rdi
call   1060 <printf@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    %rbx,%rdi
call   1080 <fileno@plt>
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
je     1307 <main+0x147>
lea    0xe2d(%rip),%rdi
call   1040 <puts@plt>
lea    0xdfa(%rip),%rdi
call   1060 <printf@plt>
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2d89(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10d0 <getline@plt>
cmp    $0xffffffff,%eax
je     1310 <main+0x150>
mov    0x8(%rsp),%rdi
mov    0x10(%rsp),%rsi
mov    $0x1,%ebp
mov    $0x1,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
lea    0xdaa(%rip),%rdi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1080 <fileno@plt>
mov    0x2d0e(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   10c0 <flock@plt>
cmp    $0xffffffff,%eax
je     131e <main+0x15e>
lea    0xd93(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebp,%ebp
jmp    12f5 <main+0x135>
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd15(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd10(%rip),%rdi
jmp    1317 <main+0x157>
lea    0xd1f(%rip),%rdi
jmp    1317 <main+0x157>
lea    0xd29(%rip),%rdi
call   10b0 <perror@plt>
jmp    12f0 <main+0x130>
lea    0xd08(%rip),%rdi
call   10b0 <perror@plt>
jmp    12f5 <main+0x135>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s <file>\0A\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [18 x i8] c"acquiring lock...\00"
@stdout = global i32 0
@LOCK_EX = global i32 0
@.str.4 = [6 x i8] c"flock\00"
@.str.5 = [6 x i8] c"done\0A\00"
@.str.6 = [13 x i8] c"Enter data: \00"
@stdin = global i32 0
@.str.7 = [8 x i8] c"getline\00"
@.str.8 = [18 x i8] c"releasing lock...\00"
@LOCK_UN = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 2, %11
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 @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %69
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %7
%24 = load i32*, i32** %7
%25 = icmp eq i32* null, %24
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %69
28: ; preds = %19
%29 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%30 = load i32, i32* @stdout
%31 = call i32 @fflush(i32 %30)
%32 = load i32*, i32** %7
%33 = call i32 @fileno(i32* %32)
%34 = load i32, i32* @LOCK_EX
%35 = call i32 @flock(i32 %33, i32 %34)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp eq i32 -1, %36
br i1 %37, label %38, label %40
38: ; preds = %28
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %69
40: ; preds = %28
%41 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%42 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i8* null, i8** %6
store i64 0, i64* %9
%43 = load i32, i32* @stdin
%44 = call i32 @getline(i8** %6, i64* %9, i32 %43)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp eq i32 -1, %45
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %69
49: ; preds = %40
%50 = load i8*, i8** %6
%51 = load i64, i64* %9
%52 = load i32*, i32** %7
%53 = call i32 @fwrite(i8* %50, i64 %51, i32 1, i32* %52)
store i32 %53, i32* %8
%54 = load i8*, i8** %6
%55 = call i32 @free(i8* %54)
%56 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%57 = load i32*, i32** %7
%58 = call i32 @fileno(i32* %57)
%59 = load i32, i32* @LOCK_UN
%60 = call i32 @flock(i32 %58, i32 %59)
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = icmp eq i32 -1, %61
br i1 %62, label %63, label %65
63: ; preds = %49
%64 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %69
65: ; preds = %49
%66 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%67 = load i32*, i32** %7
%68 = call i32 @fclose(i32* %67)
store i32 0, i32* %3
br label %69
69: ; preds = %65, %63, %47, %38, %26, %13
%70 = load i32, i32* %3
ret i32 %70
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"usage: %s <file>\0A\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [18 x i8] c"acquiring lock...\00"
@stdout = global i32 0
@LOCK_EX = global i32 0
@.str.4 = [6 x i8] c"flock\00"
@.str.6 = [13 x i8] c"Enter data: \00"
@stdin = global i32 0
@.str.7 = [8 x i8] c"getline\00"
@.str.8 = [18 x i8] c"releasing lock...\00"
@LOCK_UN = global i32 0
@str.9 = [5 x i8] c"done\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = bitcast i8** %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %53
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %53
19: ; preds = %12
%20 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
%21 = load i32, i32* @stdout
%22 = tail call i32 @fflush(i32 %21)
%23 = tail call i32 @fileno(i32* %15)
%24 = load i32, i32* @LOCK_EX
%25 = tail call i32 @flock(i32 %23, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %53
29: ; preds = %19
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.9, i64 0, i64 0))
%31 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
store i8* null, i8** %3
store i64 0, i64* %4
%32 = load i32, i32* @stdin
%33 = call i32 @getline(i8** %3, i64* %4, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0))
br label %53
37: ; preds = %29
%38 = load i8*, i8** %3
%39 = load i64, i64* %4
%40 = call i32 @fwrite(i8* %38, i64 %39, i32 1, i32* %15)
%41 = load i8*, i8** %3
%42 = call i32 @free(i8* %41)
%43 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0))
%44 = call i32 @fileno(i32* %15)
%45 = load i32, i32* @LOCK_UN
%46 = call i32 @flock(i32 %44, i32 %45)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %37
%49 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %53
50: ; preds = %37
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.9, i64 0, i64 0))
%52 = call i32 @fclose(i32* %15)
br label %53
53: ; preds = %50, %48, %35, %27, %17, %8
%54 = phi i32 [ 1, %8 ], [ 1, %17 ], [ 1, %27 ], [ 1, %35 ], [ 1, %48 ], [ 0, %50 ]
ret i32 %54
}