#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__ off_t;

/* Variables and functions */
scalar_t__ EEXIST;
int O_CREAT;
int O_WRONLY;
scalar_t__ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
scalar_t__ fsync(int);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int write(int, char *, int);
void write1(const char *d, const char *f, int start, int n, char c) {
  int fd;
  char name[512];
  sleep(1);
  sprintf(name, "%s/%s", d, f);
  fd = open(name, O_WRONLY | O_CREAT, 0666);
  if (fd < 0 && errno == EEXIST)
    fd = open(name, O_WRONLY, 0666);
  if (fd < 0) {
    fprintf(stderr, "test-lab-3-b: open(%s): %s\n", name, strerror(errno));
    exit(1);
  }
  if (lseek(fd, start, 0) != (off_t)start) {
    fprintf(stderr, "test-lab-3-b: lseek(%s, %d): %s\n", name, start,
            strerror(errno));
    exit(1);
  }
  for (int i = 0; i < n; i++) {
    if (write(fd, &c, 1) != 1) {
      fprintf(stderr, "test-lab-3-b: write(%s): %s\n", name, strerror(errno));
      exit(1);
    }
    if (fsync(fd) != 0) {
      fprintf(stderr, "test-lab-3-b: fsync(%s): %s\n", name, strerror(errno));
      exit(1);
    }
  }
  if (close(fd) != 0) {
    fprintf(stderr, "test-lab-3-b: close(%s): %s\n", name, strerror(errno));
    exit(1);
  }
}


x86 assembly (clang, -O0):
<write1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
mov    %r8b,%al
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    -0x220(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe21(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x220(%rbp),%rdi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    0x2dfd(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    124b <write1+0xab>
mov    0x2dad(%rip),%rax
mov    (%rax),%rax
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%rax
jne    124b <write1+0xab>
lea    -0x220(%rbp),%rdi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12a9 <write1+0x109>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x234(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x234(%rbp),%edi
mov    -0x230(%rbp),%rdx
mov    %rax,%rcx
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1040 <lseek@plt>
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
je     1326 <write1+0x186>
mov    0x2d06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x244(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x240(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x238(%rbp)
mov    0x2cd8(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x244(%rbp),%edi
mov    -0x240(%rbp),%rdx
mov    -0x238(%rbp),%ecx
mov    %rax,%r8
lea    0xd0d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x224(%rbp)
mov    -0x224(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    142c <write1+0x28c>
mov    -0x20(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
je     13ad <write1+0x20d>
mov    0x2c70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x254(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x250(%rbp)
mov    0x2c4b(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x254(%rbp),%edi
mov    -0x250(%rbp),%rdx
mov    %rax,%rcx
lea    0xca7(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%edi
call   1070 <fsync@plt>
cmp    $0x0,%rax
je     1413 <write1+0x273>
mov    0x2c0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x264(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x260(%rbp)
mov    0x2be5(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x264(%rbp),%edi
mov    -0x260(%rbp),%rdx
mov    %rax,%rcx
lea    0xc5e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1418 <write1+0x278>
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
jmp    1330 <write1+0x190>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     1492 <write1+0x2f2>
mov    0x2b8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x274(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x270(%rbp)
mov    0x2b66(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x274(%rbp),%edi
mov    -0x270(%rbp),%rdx
mov    %rax,%rcx
lea    0xbfc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write1>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x210,%rsp
mov    %ecx,%ebx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    %r8b,0xf(%rsp)
mov    $0x1,%edi
call   10c0 <sleep@plt>
lea    0xe30(%rip),%rsi
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1090 <sprintf@plt>
mov    0x2dce(%rip),%r15
mov    0x2e07(%rip),%rax
mov    (%rax),%esi
or     (%r15),%esi
mov    %r12,%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1240 <write1+0xa0>
mov    0x2db8(%rip),%r12
mov    (%r12),%rdi
mov    0x2dbd(%rip),%rax
cmp    (%rax),%rdi
jne    1372 <write1+0x1d2>
mov    (%r15),%esi
lea    0x10(%rsp),%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
js     136e <write1+0x1ce>
mov    %ebp,%edi
mov    %r14d,%esi
xor    %edx,%edx
call   1040 <lseek@plt>
movslq %r14d,%rcx
cmp    %rcx,%rax
jne    1310 <write1+0x170>
test   %ebx,%ebx
jle    1295 <write1+0xf5>
lea    0xf(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x1,%eax
jne    12b5 <write1+0x115>
mov    %ebp,%edi
call   1070 <fsync@plt>
test   %rax,%rax
jne    12d6 <write1+0x136>
add    $0xffffffff,%ebx
jne    1270 <write1+0xd0>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    134d <write1+0x1ad>
add    $0x210,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d14(%rip),%rax
mov    (%rax),%ebx
mov    0x2d03(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
lea    0xd6f(%rip),%rsi
jmp    12f5 <write1+0x155>
mov    0x2cf3(%rip),%rax
mov    (%rax),%ebx
mov    0x2ce2(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
lea    0xd6b(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%ebx
mov    0x2ca8(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
lea    0xcf3(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %r14d,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2c7c(%rip),%rax
mov    (%rax),%ebx
mov    0x2c6b(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
lea    0xd11(%rip),%rsi
jmp    12f5 <write1+0x155>
mov    (%r12),%rdi
mov    0x2c57(%rip),%rax
mov    (%rax),%ebx
call   10b0 <strerror@plt>
lea    0xc7f(%rip),%rsi
jmp    12f5 <write1+0x155>

LLVM IR (-O0):
@.str = [6 x i8] c"%s/%s\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"test-lab-3-b: open(%s): %s\0A\00"
@.str.2 = [33 x i8] c"test-lab-3-b: lseek(%s, %d): %s\0A\00"
@.str.3 = [29 x i8] c"test-lab-3-b: write(%s): %s\0A\00"
@.str.4 = [29 x i8] c"test-lab-3-b: fsync(%s): %s\0A\00"
@.str.5 = [29 x i8] c"test-lab-3-b: close(%s): %s\0A\00"
define void @write1(i8* %0, i8* %1, i32 %2, i32 %3, i8 signext %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
%12 = alloca [512 x i8]
%13 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8 %4, i8* %10
%14 = call i32 @sleep(i32 1)
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%16 = load i8*, i8** %6
%17 = load i8*, i8** %7
%18 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %17)
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%20 = load i32, i32* @O_WRONLY
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %19, i32 %22, i32 438)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %5
%27 = load i64, i64* @errno
%28 = load i64, i64* @EEXIST
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %34
30: ; preds = %26
%31 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%32 = load i32, i32* @O_WRONLY
%33 = call i32 @open(i8* %31, i32 %32, i32 438)
store i32 %33, i32* %11
br label %34
34: ; preds = %30, %26, %5
%35 = load i32, i32* %11
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %34
%38 = load i32, i32* @stderr
%39 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%40 = load i64, i64* @errno
%41 = call i8* @strerror(i64 %40)
%42 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8* %41)
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34
%45 = load i32, i32* %11
%46 = load i32, i32* %8
%47 = call i64 @lseek(i32 %45, i32 %46, i32 0)
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = icmp ne i64 %47, %49
br i1 %50, label %51, label %59
51: ; preds = %44
%52 = load i32, i32* @stderr
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%54 = load i32, i32* %8
%55 = load i64, i64* @errno
%56 = call i8* @strerror(i64 %55)
%57 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %53, i32 %54, i8* %56)
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %44
store i32 0, i32* %13
br label %60
60: ; preds = %87, %59
%61 = load i32, i32* %13
%62 = load i32, i32* %9
%63 = icmp slt i32 %61, %62
br i1 %63, label %64, label %90
64: ; preds = %60
%65 = load i32, i32* %11
%66 = call i32 @write(i32 %65, i8* %10, i32 1)
%67 = icmp ne i32 %66, 1
br i1 %67, label %68, label %75
68: ; preds = %64
%69 = load i32, i32* @stderr
%70 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%71 = load i64, i64* @errno
%72 = call i8* @strerror(i64 %71)
%73 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %70, i8* %72)
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %64
%76 = load i32, i32* %11
%77 = call i64 @fsync(i32 %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %86
79: ; preds = %75
%80 = load i32, i32* @stderr
%81 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%82 = load i64, i64* @errno
%83 = call i8* @strerror(i64 %82)
%84 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %81, i8* %83)
%85 = call i32 @exit(i32 1)
unreachable
86: ; preds = %75
br label %87
87: ; preds = %86
%88 = load i32, i32* %13
%89 = add nsw i32 %88, 1
store i32 %89, i32* %13
br label %60
90: ; preds = %60
%91 = load i32, i32* %11
%92 = call i64 @close(i32 %91)
%93 = icmp ne i64 %92, 0
br i1 %93, label %94, label %101
94: ; preds = %90
%95 = load i32, i32* @stderr
%96 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%97 = load i64, i64* @errno
%98 = call i8* @strerror(i64 %97)
%99 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %96, i8* %98)
%100 = call i32 @exit(i32 1)
unreachable
101: ; preds = %90
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"%s/%s\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@stderr = global i32 0
@.str.1 = [28 x i8] c"test-lab-3-b: open(%s): %s\0A\00"
@.str.2 = [33 x i8] c"test-lab-3-b: lseek(%s, %d): %s\0A\00"
@.str.3 = [29 x i8] c"test-lab-3-b: write(%s): %s\0A\00"
@.str.4 = [29 x i8] c"test-lab-3-b: fsync(%s): %s\0A\00"
@.str.5 = [29 x i8] c"test-lab-3-b: close(%s): %s\0A\00"
define void @write1(i8* %0, i8* %1, i32 %2, i32 %3, i8 signext %4) {
%6 = alloca i8
%7 = alloca [512 x i8]
store i8 %4, i8* %6
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%9 = tail call i32 @sleep(i32 1)
%10 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = call i32 @open(i8* %8, i32 %13, i32 438)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %32
16: ; preds = %5
%17 = load i64, i64* @errno
%18 = load i64, i64* @EEXIST
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %26
20: ; preds = %16
%21 = load i32, i32* @O_WRONLY
%22 = call i32 @open(i8* %8, i32 %21, i32 438)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i64, i64* @errno
br label %26
26: ; preds = %24, %16
%27 = phi i64 [ %25, %24 ], [ %17, %16 ]
%28 = load i32, i32* @stderr
%29 = tail call i8* @strerror(i64 %27)
%30 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %5, %20
%33 = phi i32 [ %22, %20 ], [ %14, %5 ]
%34 = tail call i64 @lseek(i32 %33, i32 %2, i32 0)
%35 = sext i32 %2 to i64
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = icmp sgt i32 %3, 0
br i1 %38, label %50, label %47
39: ; preds = %32
%40 = load i32, i32* @stderr
%41 = load i64, i64* @errno
%42 = tail call i8* @strerror(i64 %41)
%43 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %8, i32 %2, i8* %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %60
%46 = icmp eq i32 %63, %3
br i1 %46, label %47, label %50
47: ; preds = %45, %37
%48 = tail call i64 @close(i32 %33)
%49 = icmp eq i64 %48, 0
br i1 %49, label %76, label %70
50: ; preds = %37, %45
%51 = phi i32 [ %63, %45 ], [ 0, %37 ]
%52 = call i32 @write(i32 %33, i8* %6, i32 1)
%53 = icmp eq i32 %52, 1
br i1 %53, label %60, label %54
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = load i64, i64* @errno
%57 = tail call i8* @strerror(i64 %56)
%58 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %8, i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %50
%61 = tail call i64 @fsync(i32 %33)
%62 = icmp eq i64 %61, 0
%63 = add nuw nsw i32 %51, 1
br i1 %62, label %45, label %64
64: ; preds = %60
%65 = load i32, i32* @stderr
%66 = load i64, i64* @errno
%67 = tail call i8* @strerror(i64 %66)
%68 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* %8, i8* %67)
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %47
%71 = load i32, i32* @stderr
%72 = load i64, i64* @errno
%73 = tail call i8* @strerror(i64 %72)
%74 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0), i8* %8, i8* %73)
%75 = call i32 @exit(i32 1)
unreachable
76: ; preds = %47
ret void
}