#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 */
int /*<<< orphan*/ failure(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ respond(int /*<<< orphan*/, char *, int);
char *strchr(char *, char);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
void putf(char *uri, int socketfd) {
  int len;
  FILE *fp;
  char *body = uri + strlen(uri) + 1;
  do {
    body = strchr(body, '\r') + 1;
  } while (body && strncmp(body, "\n\r\n", 3));
  len = strlen(body);
  if (!(fp = fopen(uri, "w")) || fprintf(fp, body, len) != len) {
    failure("500 Write failed", socketfd);
    return;
  }
  fclose(fp);
  respond(0, "text", socketfd);
}


x86 assembly (clang, -O0):
<putf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0xd,%esi
call   1070 <strchr@plt>
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x29(%rbp)
je     11f9 <putf+0x79>
mov    -0x20(%rbp),%rdi
lea    0xe1b(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11b4 <putf+0x34>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1247 <putf+0xc7>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1090 <fprintf@plt>
cmp    -0x10(%rbp),%eax
je     125b <putf+0xdb>
mov    -0xc(%rbp),%esi
lea    0xdb5(%rip),%rdi
call   1080 <failure@plt>
jmp    1275 <putf+0xf5>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0xc(%rbp),%edx
xor    %edi,%edi
lea    0xda7(%rip),%rsi
call   1040 <respond@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<putf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r15
call   1060 <strlen@plt>
cltq
lea    (%r15,%rax,1),%rbx
add    $0x1,%rbx
lea    0xe5c(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0xd,%esi
call   1070 <strchr@plt>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11b0 <putf+0x30>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %eax,%r12d
lea    0xe19(%rip),%rsi
mov    %r15,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     122f <putf+0xaf>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1090 <fprintf@plt>
cmp    %r12d,%eax
jne    122f <putf+0xaf>
mov    %rbp,%rdi
call   1050 <fclose@plt>
lea    0xdfa(%rip),%rsi
xor    %edi,%edi
mov    %r14d,%edx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <respond@plt>
lea    0xdd0(%rip),%rdi
mov    %r14d,%esi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <failure@plt>

LLVM IR (-O0):
@.str = [4 x i8] c"\0A\0D\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [17 x i8] c"500 Write failed\00"
@.str.3 = [5 x i8] c"text\00"
define void @putf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %8, i64 %11
%13 = getelementptr inbounds i8, i8* %12, i64 1
store i8* %13, i8** %7
br label %14
14: ; preds = %25, %2
%15 = load i8*, i8** %7
%16 = call i8* @strchr(i8* %15, i8 signext 13)
%17 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %17, i8** %7
br label %18
18: ; preds = %14
%19 = load i8*, i8** %7
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = load i8*, i8** %7
%23 = call i64 @strncmp(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%24 = icmp ne i64 %23, 0
br label %25
25: ; preds = %21, %18
%26 = phi i1 [ false, %18 ], [ %24, %21 ]
br i1 %26, label %14, label %27
27: ; preds = %25
%28 = load i8*, i8** %7
%29 = call i32 @strlen(i8* %28)
store i32 %29, i32* %5
%30 = load i8*, i8** %3
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %6
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %40
33: ; preds = %27
%34 = load i32*, i32** %6
%35 = load i8*, i8** %7
%36 = load i32, i32* %5
%37 = call i32 @fprintf(i32* %34, i8* %35, i32 %36)
%38 = load i32, i32* %5
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %43
40: ; preds = %33, %27
%41 = load i32, i32* %4
%42 = call i32 @failure(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %41)
br label %48
43: ; preds = %33
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %4
%47 = call i32 @respond(i32 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %46)
br label %48
48: ; preds = %43, %40
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"\0A\0D\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [17 x i8] c"500 Write failed\00"
@.str.3 = [5 x i8] c"text\00"
define void @putf(i8* %0, i32 %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = getelementptr inbounds i8, i8* %5, i64 1
br label %7
7: ; preds = %7, %2
%8 = phi i8* [ %6, %2 ], [ %10, %7 ]
%9 = tail call i8* @strchr(i8* %8, i8 signext 13)
%10 = getelementptr inbounds i8, i8* %9, i64 1
%11 = tail call i64 @strncmp(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %7
13: ; preds = %7
%14 = tail call i32 @strlen(i8* %10)
%15 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = tail call i32 @fprintf(i32* %15, i8* %10, i32 %14)
%19 = icmp eq i32 %18, %14
br i1 %19, label %22, label %20
20: ; preds = %17, %13
%21 = tail call i32 @failure(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %1)
br label %25
22: ; preds = %17
%23 = tail call i32 @fclose(i32* %15)
%24 = tail call i32 @respond(i32 0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %1)
br label %25
25: ; preds = %22, %20
ret void
}