#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 fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *http_request_line(int, char *, char *, size_t *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
int main(int argc, char **argv) {
  if (argv[1] == NULL) {
    printf("Usage: ./bug1 [s|l]\n");
    return 1;
  }
  FILE *fp;
  if (strcmp(argv[1], "s") == 0)
    fp = fopen("small_input.txt", "r");
  else if (strcmp(argv[1], "l") == 0)
    fp = fopen("large_input.txt", "r");
  else {
    printf("%s is not a valid argument\n", argv[1]);
    return 2;
  }
  int fd = fileno(fp);
  static char env[8192];
  static size_t env_len;
  char reqpath[2048];
  const char *errmsg;
  if ((errmsg = http_request_line(fd, reqpath, env, &env_len)))
    printf("http_request_line: %s", errmsg);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1192 <main+0x42>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    127c <main+0x12c>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xec8(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    11cc <main+0x7c>
lea    0xe5e(%rip),%rdi
lea    0xe67(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    122d <main+0xdd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe4c(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
jne    1206 <main+0xb6>
lea    0xe38(%rip),%rdi
lea    0xe2d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    1228 <main+0xd8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    127c <main+0x12c>
jmp    122d <main+0xdd>
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x820(%rbp),%rsi
lea    0x2e16(%rip),%rdx
lea    0x4e0f(%rip),%rcx
call   1070 <http_request_line@plt>
mov    %rax,-0x828(%rbp)
cmp    $0x0,%rax
je     127c <main+0x12c>
mov    -0x828(%rbp),%rsi
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x800,%rsp
mov    0x8(%rsi),%rdi
test   %rdi,%rdi
je     11b3 <main+0x53>
mov    %rsi,%rbx
lea    0xed9(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11c6 <main+0x66>
mov    0x8(%rbx),%rdi
lea    0xe82(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11cf <main+0x6f>
mov    0x8(%rbx),%rsi
lea    0xe7f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x2,%eax
jmp    121a <main+0xba>
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    121a <main+0xba>
lea    0xe33(%rip),%rdi
jmp    11d6 <main+0x76>
lea    0xe3e(%rip),%rdi
lea    0xe33(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rdi
call   1060 <fileno@plt>
lea    0x2e6f(%rip),%rdx
lea    0x4e68(%rip),%rcx
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <http_request_line@plt>
test   %rax,%rax
je     1218 <main+0xb8>
lea    0xe32(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Usage: ./bug1 [s|l]\0A\00"
@.str.1 = [2 x i8] c"s\00"
@.str.2 = [16 x i8] c"small_input.txt\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"l\00"
@.str.5 = [16 x i8] c"large_input.txt\00"
@.str.6 = [28 x i8] c"%s is not a valid argument\0A\00"
@main.env = internal global [8192 x i8] zeroinitializer
@main.env_len = internal global i64 0
@.str.7 = [22 x i8] c"http_request_line: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [2048 x i8]
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %48
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %23, i32** %6
br label %38
24: ; preds = %16
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i64 @strcmp(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32* @fopen(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %31, i32** %6
br label %37
32: ; preds = %24
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %35)
store i32 2, i32* %3
br label %48
37: ; preds = %30
br label %38
38: ; preds = %37, %22
%39 = load i32*, i32** %6
%40 = call i32 @fileno(i32* %39)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = getelementptr inbounds [2048 x i8], [2048 x i8]* %8, i64 0, i64 0
%43 = call i8* @http_request_line(i32 %41, i8* %42, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @main.env, i64 0, i64 0), i64* @main.env_len)
store i8* %43, i8** %9
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i8*, i8** %9
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %14, %32, %45, %38
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"s\00"
@.str.2 = [16 x i8] c"small_input.txt\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"l\00"
@.str.5 = [16 x i8] c"large_input.txt\00"
@.str.6 = [28 x i8] c"%s is not a valid argument\0A\00"
@main.env = internal global [8192 x i8] zeroinitializer
@main.env_len = internal global i64 0
@.str.7 = [22 x i8] c"http_request_line: %s\00"
@str = [20 x i8] c"Usage: ./bug1 [s|l]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %29
9: ; preds = %2
%10 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %9
%13 = load i8*, i8** %4
%14 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %17)
br label %29
19: ; preds = %12, %9
%20 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %9 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), %12 ]
%21 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%22 = tail call i32 @fileno(i32* %21)
%23 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%24 = call i8* @http_request_line(i32 %22, i8* %23, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @main.env, i64 0, i64 0), i64* @main.env_len)
%25 = icmp eq i8* %24, null
br i1 %25, label %28, label %26
26: ; preds = %19
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0), i8* %24)
br label %28
28: ; preds = %26, %19
br label %29
29: ; preds = %16, %28, %7
%30 = phi i32 [ 1, %7 ], [ 0, %28 ], [ 2, %16 ]
ret i32 %30
}