#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 /*<<< orphan*/ O_WRONLY;
long long RAND_MAX;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *, ...);
scalar_t__ rand();
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ srand(int);
int /*<<< orphan*/ usleep(int);
scalar_t__ write(int, char *, int);
void do_main(int num) {
  int fd;
  int pos;
  pos = 16 + (num * 6);
  srand(pos);
  printf("task: %d opening %s\n", num, "DEV");
  fd = open("DEV", O_WRONLY);
  if (fd < 0) {
    perror("cannot open device\n");
    return;
  }
  do {
    char buffer[5];
    int val;
    int len;
    if (lseek(fd, pos, SEEK_SET) < 0) {
      perror("lseek error\n");
      return;
    }
    val = ((long long)rand() * 1000 / RAND_MAX);
    len = snprintf(buffer, 4, "%d", val);
    buffer[len] = '\0';
    if (write(fd, buffer, len) < 0) {
      perror("write error\n");
      return;
    }
    printf("task: %d => wrote [ %s ] on pos [ %d ]\n", num, buffer, pos);
    usleep((500000));
  } while (1);
  close(fd);
}


x86 assembly (clang, -O0):
<do_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
imul   $0x6,-0x4(%rbp),%eax
add    $0x10,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <srand@plt>
mov    -0x4(%rbp),%esi
lea    0xe39(%rip),%rdi
lea    0xe47(%rip),%rdx
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
lea    0xe30(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1208 <do_main+0x68>
lea    0xe1b(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e0 <do_main+0x140>
jmp    120d <do_main+0x6d>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    123c <do_main+0x9c>
lea    0xdfb(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e0 <do_main+0x140>
mov    $0x0,%al
call   10b0 <rand@plt>
imul   $0x3e8,%rax,%rax
mov    0x2d97(%rip),%rcx
cqto
idivq  (%rcx)
mov    %eax,-0x18(%rbp)
lea    -0x11(%rbp),%rdi
mov    -0x18(%rbp),%ecx
mov    $0x4,%esi
lea    0xdce(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x11(%rbp,%rax,1)
mov    -0x8(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    12a7 <do_main+0x107>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e0 <do_main+0x140>
mov    -0x4(%rbp),%esi
lea    -0x11(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x7a120,%edi
call   10c0 <usleep@plt>
mov    $0x1,%al
test   $0x1,%al
jne    120d <do_main+0x6d>
jmp    12d8 <do_main+0x138>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
lea    (%r14,%r14,2),%eax
lea    (%rax,%rax,1),%r13d
add    $0x10,%r13d
mov    %r13d,%edi
call   1070 <srand@plt>
lea    0xe47(%rip),%rdi
lea    0xe55(%rip),%rbx
mov    %r14d,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
jns    11fc <do_main+0x6c>
lea    0xe30(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <perror@plt>
mov    %eax,%ebx
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r13d,%esi
call   1060 <lseek@plt>
lea    0xe15(%rip),%rdi
test   %rax,%rax
js     12e6 <do_main+0x156>
lea    0xe12(%rip),%r12
lea    0x3(%rsp),%rbp
lea    0xe16(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %eax,%eax
call   10a0 <rand@plt>
imul   $0x3e8,%rax,%rax
mov    0x2d93(%rip),%rcx
mov    (%rcx),%rcx
mov    %rax,%rdx
or     %rcx,%rdx
shr    $0x20,%rdx
je     1270 <do_main+0xe0>
cqto
idiv   %rcx
jmp    1274 <do_main+0xe4>
nopl   0x0(%rax,%rax,1)
xor    %edx,%edx
div    %ecx
mov    %rbp,%rdi
mov    $0x4,%esi
mov    %r12,%rdx
mov    %eax,%ecx
call   1050 <snprintf@plt>
movslq %eax,%rdx
movb   $0x0,0x3(%rsp,%rdx,1)
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <write@plt>
test   %rax,%rax
js     12df <do_main+0x14f>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x7a120,%edi
call   10b0 <usleep@plt>
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    %r13d,%esi
call   1060 <lseek@plt>
test   %rax,%rax
jns    1240 <do_main+0xb0>
lea    0xd50(%rip),%rdi
jmp    12e6 <do_main+0x156>
lea    0xd57(%rip),%rdi
call   1090 <perror@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"task: %d opening %s\0A\00"
@.str.1 = [4 x i8] c"DEV\00"
@O_WRONLY = global i32 0
@.str.2 = [20 x i8] c"cannot open device\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"lseek error\0A\00"
@RAND_MAX = global i64 0
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [13 x i8] c"write error\0A\00"
@.str.6 = [40 x i8] c"task: %d => wrote [ %s ] on pos [ %d ]\0A\00"
define void @do_main(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [5 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = load i32, i32* %2
%9 = mul nsw i32 %8, 6
%10 = add nsw i32 16, %9
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = call i32 @srand(i32 %11)
%13 = load i32, i32* %2
%14 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %13, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %59
21: ; preds = %1
br label %22
22: ; preds = %55, %21
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = load i32, i32* @SEEK_SET
%26 = call i64 @lseek(i32 %23, i32 %24, i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %59
30: ; preds = %22
%31 = call i64 (...) @rand()
%32 = mul nsw i64 %31, 1000
%33 = load i64, i64* @RAND_MAX
%34 = sdiv i64 %32, %33
%35 = trunc i64 %34 to i32
store i32 %35, i32* %6
%36 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%37 = load i32, i32* %6
%38 = call i32 @snprintf(i8* %36, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 %40
store i8 0, i8* %41
%42 = load i32, i32* %3
%43 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%44 = load i32, i32* %7
%45 = call i64 @write(i32 %42, i8* %43, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %30
%48 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %59
49: ; preds = %30
%50 = load i32, i32* %2
%51 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%52 = load i32, i32* %4
%53 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %50, i8* %51, i32 %52)
%54 = call i32 @usleep(i32 500000)
br label %55
55: ; preds = %49
br i1 true, label %22, label %56
56: ; preds = %55
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56, %47, %28, %19
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"task: %d opening %s\0A\00"
@.str.1 = [4 x i8] c"DEV\00"
@O_WRONLY = global i32 0
@.str.2 = [20 x i8] c"cannot open device\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"lseek error\0A\00"
@RAND_MAX = global i64 0
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [13 x i8] c"write error\0A\00"
@.str.6 = [40 x i8] c"task: %d => wrote [ %s ] on pos [ %d ]\0A\00"
define void @do_main(i32 %0) {
%2 = alloca [5 x i8]
%3 = mul nsw i32 %0, 6
%4 = add nsw i32 %3, 16
%5 = tail call i32 @srand(i32 %4)
%6 = tail call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @O_WRONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%12 = load i32, i32* @SEEK_SET
%13 = call i64 @lseek(i32 %8, i32 %4, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %34, label %17
15: ; preds = %1
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %37
17: ; preds = %10, %28
%18 = call i64 (...) @rand()
%19 = mul nsw i64 %18, 1000
%20 = load i64, i64* @RAND_MAX
%21 = sdiv i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = call i32 @snprintf(i8* %11, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i64 @write(i32 %8, i8* %11, i32 %23)
%27 = icmp slt i64 %26, 0
br i1 %27, label %34, label %28
28: ; preds = %17
%29 = call i32 (i8*, i32, i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.6, i64 0, i64 0), i32 %0, i8* %11, i32 %4)
%30 = call i32 @usleep(i32 500000)
%31 = load i32, i32* @SEEK_SET
%32 = call i64 @lseek(i32 %8, i32 %4, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %17
34: ; preds = %17, %28, %10
%35 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), %10 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), %28 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), %17 ]
%36 = call i32 @perror(i8* %35)
br label %37
37: ; preds = %34, %15
ret void
}