#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_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("count of argument less than 2\n");
    return -1;
  }
  int fd = 0;
  fd = open("./file_atomic.txt", O_CREAT | O_RDWR, S_IRWXU);
  if (fd < 0) {
    perror("open file error");
    return -1;
  }
  lseek(fd, 0, SEEK_END);
  sleep(2);
  char buffer[10];
  int i = 0;
  for (; i < 10; i++)
    buffer[i] = *argv[1];
  int ret = write(fd, buffer, 10);
  if (ret != 10) {
    perror("write error");
    return -1;
  }
  ret = write(fd, buffer, 10);
  if (ret != 10) {
    perror("write again error");
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11a8 <main+0x38>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <main+0x154>
movl   $0x0,-0x14(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
or     (%rax),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
lea    0xe4e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fb <main+0x8b>
lea    0xe47(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <main+0x154>
mov    -0x14(%rbp),%edi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
movl   $0x0,-0x24(%rbp)
cmpl   $0xa,-0x24(%rbp)
jge    1249 <main+0xd9>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%cl
movslq -0x24(%rbp),%rax
mov    %cl,-0x1e(%rbp,%rax,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    121f <main+0xaf>
mov    -0x14(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xa,-0x28(%rbp)
je     127f <main+0x10f>
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <main+0x154>
mov    -0x14(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xa,-0x28(%rbp)
je     12b5 <main+0x145>
lea    0xda9(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <main+0x154>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
cmp    $0x1,%edi
jg     1192 <main+0x22>
lea    0xebd(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1232 <main+0xc2>
mov    %rsi,%r14
mov    0x2e5c(%rip),%rax
mov    0x2e35(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
lea    0xe49(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     123a <main+0xca>
mov    %eax,%ebx
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    0x8(%r14),%rax
movzbl (%rax),%eax
movabs $0x101010101010101,%rcx
01 01 01
imul   %rax,%rcx
mov    %rcx,0x8(%rsp)
mov    %cx,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1040 <write@plt>
cmp    $0xa,%eax
jne    1243 <main+0xd3>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1040 <write@plt>
cmp    $0xa,%eax
jne    124c <main+0xdc>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd1(%rip),%rdi
jmp    1253 <main+0xe3>
lea    0xdd8(%rip),%rdi
jmp    1253 <main+0xe3>
lea    0xddb(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
jmp    1232 <main+0xc2>

LLVM IR (-O0):
@.str = [31 x i8] c"count of argument less than 2\0A\00"
@.str.1 = [18 x i8] c"./file_atomic.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.2 = [16 x i8] c"open file error\00"
@SEEK_END = global i32 0
@.str.3 = [12 x i8] c"write error\00"
@.str.4 = [18 x i8] c"write again error\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
14: ; preds = %2
store i32 0, i32* %6
%15 = load i32, i32* @O_CREAT
%16 = load i32, i32* @O_RDWR
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRWXU
%19 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
24: ; preds = %14
%25 = load i32, i32* %6
%26 = load i32, i32* @SEEK_END
%27 = call i32 @lseek(i32 %25, i32 0, i32 %26)
%28 = call i32 @sleep(i32 2)
store i32 0, i32* %8
br label %29
29: ; preds = %40, %24
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 10
br i1 %31, label %32, label %43
32: ; preds = %29
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = load i8, i8* %35
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 %38
store i8 %36, i8* %39
br label %40
40: ; preds = %32
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %29
43: ; preds = %29
%44 = load i32, i32* %6
%45 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%46 = call i32 @write(i32 %44, i8* %45, i32 10)
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = icmp ne i32 %47, 10
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
51: ; preds = %43
%52 = load i32, i32* %6
%53 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%54 = call i32 @write(i32 %52, i8* %53, i32 10)
store i32 %54, i32* %9
%55 = load i32, i32* %9
%56 = icmp ne i32 %55, 10
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
59: ; preds = %51
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
store i32 0, i32* %3
br label %62
62: ; preds = %59, %57, %49, %22, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"./file_atomic.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRWXU = global i32 0
@.str.2 = [16 x i8] c"open file error\00"
@SEEK_END = global i32 0
@.str.3 = [12 x i8] c"write error\00"
@.str.4 = [18 x i8] c"write again error\00"
@str = [30 x i8] c"count of argument less than 2\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [10 x i8]
%4 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %38
8: ; preds = %2
%9 = load i32, i32* @O_CREAT
%10 = load i32, i32* @O_RDWR
%11 = or i32 %10, %9
%12 = load i32, i32* @S_IRWXU
%13 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %38
17: ; preds = %8
%18 = load i32, i32* @SEEK_END
%19 = tail call i32 @lseek(i32 %13, i32 0, i32 %18)
%20 = tail call i32 @sleep(i32 2)
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%22 = getelementptr inbounds i8*, i8** %1, i64 1
%23 = load i8*, i8** %22
%24 = load i8, i8* %23
%25 = call i32 @write(i32 %13, i8* %21, i32 10)
%26 = icmp eq i32 %25, 10
br i1 %26, label %29, label %27
27: ; preds = %17
%28 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %36
29: ; preds = %17
%30 = call i32 @write(i32 %13, i8* %21, i32 10)
%31 = icmp eq i32 %30, 10
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %36
34: ; preds = %29
%35 = tail call i32 @close(i32 %13)
br label %36
36: ; preds = %34, %32, %27
%37 = phi i32 [ -1, %27 ], [ -1, %32 ], [ 0, %34 ]
br label %38
38: ; preds = %15, %36, %6
%39 = phi i32 [ -1, %6 ], [ -1, %15 ], [ %37, %36 ]
ret i32 %39
}