#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_APPEND;
int O_WRONLY;
scalar_t__ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char const *);
scalar_t__ write(int, char const *, scalar_t__);
void append1(const char *d, const char *f, const char *in) {
  int fd;
  char n[512];
  sleep(1);
  sprintf(n, "%s/%s", d, f);
  fd = open(n, O_WRONLY | O_APPEND);
  if (fd < 0) {
    fprintf(stderr, "test-lab-3-b: append open(%s): %s\n", n, strerror(errno));
    exit(1);
  }
  if (write(fd, in, strlen(in)) != strlen(in)) {
    fprintf(stderr, "test-lab-3-b: append write(%s): %s\n", n, strerror(errno));
    exit(1);
  }
  if (close(fd) != 0) {
    fprintf(stderr, "test-lab-3-b: append close(%s): %s\n", n, strerror(errno));
    exit(1);
  }
}


x86 assembly (clang, -O0):
<append1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x1,%edi
call   10b0 <sleep@plt>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe39(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x220(%rbp),%rdi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2ddd(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1248 <append1+0xb8>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x228(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x22c(%rbp),%edi
mov    -0x228(%rbp),%rdx
mov    %rax,%rcx
lea    0xdcd(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x244(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x244(%rbp),%edi
mov    -0x240(%rbp),%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,-0x238(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x238(%rbp),%rax
cmp    %rcx,%rax
je     12ee <append1+0x15e>
mov    0x2d3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x254(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x250(%rbp)
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x254(%rbp),%edi
mov    -0x250(%rbp),%rdx
mov    %rax,%rcx
lea    0xd4a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     1351 <append1+0x1c1>
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x264(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x260(%rbp)
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x264(%rbp),%edi
mov    -0x260(%rbp),%rdx
mov    %rax,%rcx
lea    0xd0b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append1>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbp
mov    $0x1,%edi
call   10b0 <sleep@plt>
lea    0xe47(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rbp,%rdx
mov    %r14,%rcx
call   1080 <sprintf@plt>
mov    0x2df7(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     122b <append1+0x9b>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <strlen@plt>
cmp    %rax,%rbx
jne    124b <append1+0xbb>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    126b <append1+0xdb>
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dae(%rip),%rax
mov    (%rax),%ebx
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdbd(%rip),%rsi
jmp    1289 <append1+0xf9>
mov    0x2d8e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d7d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc0(%rip),%rsi
jmp    1289 <append1+0xf9>
mov    0x2d6e(%rip),%rax
mov    (%rax),%ebx
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc4(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"test-lab-3-b: append open(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [36 x i8] c"test-lab-3-b: append write(%s): %s\0A\00"
@.str.3 = [36 x i8] c"test-lab-3-b: append close(%s): %s\0A\00"
define void @append1(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [512 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = call i32 @sleep(i32 1)
%10 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%11 = load i8*, i8** %4
%12 = load i8*, i8** %5
%13 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %11, i8* %12)
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_APPEND
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %14, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %23, i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %3
%29 = load i32, i32* %7
%30 = load i8*, i8** %6
%31 = load i8*, i8** %6
%32 = call i64 @strlen(i8* %31)
%33 = call i64 @write(i32 %29, i8* %30, i64 %32)
%34 = load i8*, i8** %6
%35 = call i64 @strlen(i8* %34)
%36 = icmp ne i64 %33, %35
br i1 %36, label %37, label %44
37: ; preds = %28
%38 = load i32, i32* @stderr
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %39, i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %28
%45 = load i32, i32* %7
%46 = call i64 @close(i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %44
%49 = load i32, i32* @stderr
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%51 = load i32, i32* @errno
%52 = call i8* @strerror(i32 %51)
%53 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %50, i8* %52)
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %44
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
@stderr = global i32 0
@.str.1 = [35 x i8] c"test-lab-3-b: append open(%s): %s\0A\00"
@errno = global i32 0
@.str.2 = [36 x i8] c"test-lab-3-b: append write(%s): %s\0A\00"
@.str.3 = [36 x i8] c"test-lab-3-b: append close(%s): %s\0A\00"
define void @append1(i8* %0, i8* %1, i8* nocapture %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = tail call i32 @sleep(i32 1)
%7 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_APPEND
%10 = or i32 %9, %8
%11 = call i32 @open(i8* %5, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %3
%20 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%21 = tail call i64 @write(i32 %11, i8* %2, i64 %20)
%22 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%23 = icmp eq i64 %21, %22
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = tail call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = tail call i64 @close(i32 %11)
%32 = icmp eq i64 %31, 0
br i1 %32, label %39, label %33
33: ; preds = %30
%34 = load i32, i32* @stderr
%35 = load i32, i32* @errno
%36 = tail call i8* @strerror(i32 %35)
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %30
ret void
}