#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_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fileCalculateOffset(int);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ write(int, char *, int);
int fileWriteKV(int shardID, int KVID, char *key, char *value) {
  int fd, i;
  char fileName[5];
  char cleanValue[20], cleanKey[20];
  sprintf(fileName, "f%d", shardID);
  fd = open(fileName, O_RDWR, S_IRUSR | S_IWUSR);
  if (fd < 0)
    return -1;
  for (i = 0; i < 20; i++) {
    cleanValue[i] = '\0';
    cleanKey[i] = '\0';
  }
  sprintf(cleanValue, "%s", value);
  sprintf(cleanKey, "%s", key);
  lseek(fd, fileCalculateOffset(KVID), SEEK_SET);
  write(fd, cleanKey, 20);
  write(fd, cleanValue, 20);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<fileWriteKV>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x2d(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe79(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x2d(%rbp),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    0x2e35(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11cb <fileWriteKV+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <fileWriteKV+0x11a>
movl   $0x0,-0x28(%rbp)
cmpl   $0x14,-0x28(%rbp)
jge    11fc <fileWriteKV+0x9c>
movslq -0x28(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
movslq -0x28(%rbp),%rax
movb   $0x0,-0x70(%rbp,%rax,1)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11d2 <fileWriteKV+0x72>
lea    -0x50(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x74(%rbp)
mov    -0xc(%rbp),%edi
call   1080 <fileCalculateOffset@plt>
mov    -0x74(%rbp),%edi
mov    %eax,%esi
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x24(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileWriteKV>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %esi,%r14d
mov    %edi,%edx
lea    0xe70(%rip),%rsi
xor    %r15d,%r15d
lea    0xb(%rsp),%rbp
mov    %rbp,%rdi
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    0x2e16(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbp,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     124d <fileWriteKV+0xdd>
mov    %eax,%ebp
xorps  %xmm0,%xmm0
movaps %xmm0,0x30(%rsp)
movl   $0x0,0x40(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
lea    0x30(%rsp),%r13
mov    %r13,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %r14d,%edi
call   1090 <fileCalculateOffset@plt>
mov    0x2db5(%rip),%rcx
mov    (%rcx),%edx
mov    %ebp,%edi
mov    %eax,%esi
call   1050 <lseek@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x14,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x14,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1253 <fileWriteKV+0xe3>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"f%d\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@SEEK_SET = global i32 0
define i32 @fileWriteKV(i32 %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [5 x i8]
%13 = alloca [20 x i8]
%14 = alloca [20 x i8]
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%15 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 0
%16 = load i32, i32* %6
%17 = call i32 (i8*, i8*, ...) @sprintf(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [5 x i8], [5 x i8]* %12, i64 0, i64 0
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %18, i32 %19, i32 %22)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %4
store i32 -1, i32* %5
br label %61
27: ; preds = %4
store i32 0, i32* %11
br label %28
28: ; preds = %38, %27
%29 = load i32, i32* %11
%30 = icmp slt i32 %29, 20
br i1 %30, label %31, label %41
31: ; preds = %28
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %11
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %14, i64 0, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %31
%39 = load i32, i32* %11
%40 = add nsw i32 %39, 1
store i32 %40, i32* %11
br label %28
41: ; preds = %28
%42 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%43 = load i8*, i8** %9
%44 = call i32 (i8*, i8*, ...) @sprintf(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %43)
%45 = getelementptr inbounds [20 x i8], [20 x i8]* %14, i64 0, i64 0
%46 = load i8*, i8** %8
%47 = call i32 (i8*, i8*, ...) @sprintf(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %46)
%48 = load i32, i32* %10
%49 = load i32, i32* %7
%50 = call i32 @fileCalculateOffset(i32 %49)
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @lseek(i32 %48, i32 %50, i32 %51)
%53 = load i32, i32* %10
%54 = getelementptr inbounds [20 x i8], [20 x i8]* %14, i64 0, i64 0
%55 = call i32 @write(i32 %53, i8* %54, i32 20)
%56 = load i32, i32* %10
%57 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%58 = call i32 @write(i32 %56, i8* %57, i32 20)
%59 = load i32, i32* %10
%60 = call i32 @close(i32 %59)
store i32 0, i32* %5
br label %61
61: ; preds = %41, %26
%62 = load i32, i32* %5
ret i32 %62
}

LLVM IR (-O2):
@.str = [4 x i8] c"f%d\00"
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@SEEK_SET = global i32 0
define i32 @fileWriteKV(i32 %0, i32 %1, i8* nocapture %2, i8* nocapture %3) {
%5 = alloca [5 x i8]
%6 = alloca [20 x i8]
%7 = alloca [20 x i8]
%8 = getelementptr inbounds [5 x i8], [5 x i8]* %5, i64 0, i64 0
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %0)
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %14, %13
%16 = call i32 @open(i8* %8, i32 %12, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %29, label %18
18: ; preds = %4
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%21 = call i8* @strcpy(i8* %9, i8* dereferenceable(1) %3)
%22 = call i8* @strcpy(i8* %10, i8* dereferenceable(1) %2)
%23 = call i32 @fileCalculateOffset(i32 %1)
%24 = load i32, i32* @SEEK_SET
%25 = call i32 @lseek(i32 %16, i32 %23, i32 %24)
%26 = call i32 @write(i32 %16, i8* %10, i32 20)
%27 = call i32 @write(i32 %16, i8* %9, i32 20)
%28 = call i32 @close(i32 %16)
br label %29
29: ; preds = %4, %18
%30 = phi i32 [ 0, %18 ], [ -1, %4 ]
ret i32 %30
}