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

/* Variables and functions */
int EXIT_SUCCESS;
int /*<<< orphan*/ O_APPEND;
int /*<<< orphan*/ close(int);
int lock_txt();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ unlock_txt();
int /*<<< orphan*/ write(int, char *, size_t);
int main(void) {
  int respone = 0;
  int delay = 500;
  char sentence[] = "I am a sentence!";
  size_t sentence_len = sizeof(sentence);
  while (delay != 0 && (respone = lock_txt()) == 1) {
    sleep(1);
    --delay;
  }
  if (respone == 0) {
    int txt_fd = open("txt", O_APPEND);
    write(txt_fd, sentence, sentence_len);
    unlock_txt();
    close(txt_fd);
  } else {
    printf("Failed to append!\n");
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x1f4,-0xc(%rbp)
mov    0xe6c(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe69(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe67(%rip),%al
mov    %al,-0x10(%rbp)
movq   $0x11,-0x28(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x2d(%rbp)
je     11d6 <main+0x66>
mov    $0x0,%al
call   1080 <lock_txt@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x1,%eax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11e6 <main+0x76>
jmp    11fe <main+0x8e>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    11b4 <main+0x44>
cmpl   $0x0,-0x8(%rbp)
jne    1244 <main+0xd4>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    0xdf9(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
mov    $0x0,%al
call   1070 <unlock_txt@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
jmp    1250 <main+0xe0>
lea    0xdca(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
movaps 0xe84(%rip),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
mov    $0xfffffe0c,%ebx
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1080 <lock_txt@plt>
cmp    $0x1,%eax
jne    11ac <main+0x3c>
mov    $0x1,%edi
call   1090 <sleep@plt>
inc    %ebx
jne    1190 <main+0x20>
jmp    11e6 <main+0x76>
test   %eax,%eax
jne    11e6 <main+0x76>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    $0x11,%edx
mov    %eax,%edi
call   1040 <write@plt>
xor    %eax,%eax
call   1070 <unlock_txt@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11f2 <main+0x82>
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.main.sentence = [17 x i8] c"I am a sentence!\00"
@.str = [4 x i8] c"txt\00"
@O_APPEND = global i32 0
@.str.1 = [19 x i8] c"Failed to append!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [17 x i8]
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
store i32 500, i32* %3
%7 = bitcast [17 x i8]* %4 to i8*
store i64 17, i64* %5
br label %8
8: ; preds = %16, %0
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = call i32 (...) @lock_txt()
store i32 %12, i32* %2
%13 = icmp eq i32 %12, 1
br label %14
14: ; preds = %11, %8
%15 = phi i1 [ false, %8 ], [ %13, %11 ]
br i1 %15, label %16, label %20
16: ; preds = %14
%17 = call i32 @sleep(i32 1)
%18 = load i32, i32* %3
%19 = add nsw i32 %18, -1
store i32 %19, i32* %3
br label %8
20: ; preds = %14
%21 = load i32, i32* %2
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i32, i32* @O_APPEND
%25 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = getelementptr inbounds [17 x i8], [17 x i8]* %4, i64 0, i64 0
%28 = load i64, i64* %5
%29 = call i32 @write(i32 %26, i8* %27, i64 %28)
%30 = call i32 (...) @unlock_txt()
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
br label %35
33: ; preds = %20
%34 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %23
%36 = load i32, i32* @EXIT_SUCCESS
ret i32 %36
}

LLVM IR (-O2):
@__const.main.sentence = [17 x i8] c"I am a sentence!\00"
@.str = [4 x i8] c"txt\00"
@O_APPEND = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [18 x i8] c"Failed to append!\00"
define i32 @main() {
%1 = alloca [17 x i8]
%2 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
br label %3
3: ; preds = %0, %6
%4 = phi i32 [ 500, %0 ], [ %8, %6 ]
%5 = tail call i32 (...) @lock_txt()
switch i32 %5, label %16 [
i32 1, label %6
i32 0, label %10
]
6: ; preds = %3
%7 = tail call i32 @sleep(i32 1)
%8 = add nsw i32 %4, -1
%9 = icmp eq i32 %8, 0
br i1 %9, label %16, label %3
10: ; preds = %3
%11 = load i32, i32* @O_APPEND
%12 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 @write(i32 %12, i8* %2, i64 17)
%14 = tail call i32 (...) @unlock_txt()
%15 = tail call i32 @close(i32 %12)
br label %18
16: ; preds = %6, %3
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %18
18: ; preds = %16, %10
%19 = load i32, i32* @EXIT_SUCCESS
ret i32 %19
}