#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 int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
scalar_t__ fwrite(char const *, int, size_t, int /*<<< orphan*/ *);
size_t strlen(char const *);
int strdump(const char *path, const char *value, int retry) {
  FILE *fp = fopen(path, "w");
  size_t size = strlen(value);
  if (fp != NULL) {
    size_t nbytes = 0;
    do {
      nbytes += fwrite(value, 1, size - nbytes, fp);
      if (errno != 0) {
        break;
      }
    } while (retry == 1 && nbytes < size);
    fclose(fp);
  }
  return errno;
}


x86 assembly (clang, -O0):
<strdump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11f5 <strdump+0xb5>
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rdx
sub    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e30(%rip),%rax
cmpq   $0x0,(%rax)
je     11bf <strdump+0x7f>
jmp    11ec <strdump+0xac>
jmp    11c4 <strdump+0x84>
xor    %eax,%eax
cmpl   $0x1,-0x14(%rbp)
mov    %al,-0x31(%rbp)
jne    11e1 <strdump+0xa1>
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
setb   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1187 <strdump+0x47>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<strdump>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
lea    0xea8(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %r12,%r12
je     11b3 <strdump+0x73>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    0x2e67(%rip),%r13
nopl   0x0(%rax)
mov    %rbx,%rdx
sub    %rbp,%rdx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r12,%rcx
call   1060 <fwrite@plt>
cmpq   $0x0,0x0(%r13)
jne    11ab <strdump+0x6b>
cmp    $0x1,%r14d
jne    11ab <strdump+0x6b>
add    %rax,%rbp
cmp    %rbx,%rbp
jb     1180 <strdump+0x40>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@errno = global i64 0
define i32 @strdump(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i8*, i8** %5
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %8
%14 = load i32*, i32** %7
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %42
16: ; preds = %3
store i64 0, i64* %9
br label %17
17: ; preds = %37, %16
%18 = load i8*, i8** %5
%19 = load i64, i64* %8
%20 = load i64, i64* %9
%21 = sub i64 %19, %20
%22 = load i32*, i32** %7
%23 = call i64 @fwrite(i8* %18, i32 1, i64 %21, i32* %22)
%24 = load i64, i64* %9
%25 = add i64 %24, %23
store i64 %25, i64* %9
%26 = load i64, i64* @errno
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %17
br label %39
29: ; preds = %17
br label %30
30: ; preds = %29
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i64, i64* %9
%35 = load i64, i64* %8
%36 = icmp ult i64 %34, %35
br label %37
37: ; preds = %33, %30
%38 = phi i1 [ false, %30 ], [ %36, %33 ]
br i1 %38, label %17, label %39
39: ; preds = %37, %28
%40 = load i32*, i32** %7
%41 = call i32 @fclose(i32* %40)
br label %42
42: ; preds = %39, %3
%43 = load i64, i64* @errno
%44 = trunc i64 %43 to i32
ret i32 %44
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@errno = global i64 0
define i32 @strdump(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = icmp eq i32* %4, null
br i1 %6, label %21, label %7
7: ; preds = %3
%8 = icmp eq i32 %2, 1
br label %9
9: ; preds = %7, %16
%10 = phi i64 [ %13, %16 ], [ 0, %7 ]
%11 = sub i64 %5, %10
%12 = tail call i64 @fwrite(i8* %1, i32 1, i64 %11, i32* %4)
%13 = add i64 %12, %10
%14 = load i64, i64* @errno
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = icmp ult i64 %13, %5
%18 = select i1 %8, i1 %17, i1 false
br i1 %18, label %9, label %19
19: ; preds = %9, %16
%20 = tail call i32 @fclose(i32* %4)
br label %21
21: ; preds = %19, %3
%22 = load i64, i64* @errno
%23 = trunc i64 %22 to i32
ret i32 %23
}