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

/* Variables and functions */
int /*<<< orphan*/ EX_OSERR;
int UINT32_MAX;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
scalar_t__ randomly(int);
char *strndup(char *, size_t);
void pick_line(FILE *fh, ssize_t *counter, char **chosen,
               ssize_t *chosen_length) {
  char *line = NULL;
  char *pick = NULL;
  size_t linesize;
  ssize_t linelen;
  ssize_t line_number = *counter;
  while ((linelen = getline(&line, &linesize, fh)) != -1) {
    if (randomly(line_number) == 0) {
      if (pick)
        free(pick);
      if (!(pick = strndup(line, linesize)))
        err(EX_OSERR, "could not copy line");
      *chosen = pick;
      *chosen_length = linelen;
    }
    if (line_number < UINT32_MAX)
      line_number++;
  }
  *counter = line_number;
}


x86 assembly (clang, -O0):
<pick_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <getline@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
je     1228 <pick_line+0xd8>
mov    -0x40(%rbp),%edi
call   1040 <randomly@plt>
cmp    $0x0,%rax
jne    1208 <pick_line+0xb8>
cmpq   $0x0,-0x30(%rbp)
je     11c4 <pick_line+0x74>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <strndup@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11f4 <pick_line+0xa4>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1060 <err@plt>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%eax
mov    0x2de6(%rip),%rcx
cmp    (%rcx),%eax
jge    1223 <pick_line+0xd3>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1181 <pick_line+0x31>
mov    -0x40(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pick_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x20(%rsp)
mov    %rdx,0x18(%rsp)
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    %rsi,0x10(%rsp)
mov    (%rsi),%ebp
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1070 <getline@plt>
cmp    $0xffffffff,%eax
je     122c <pick_line+0xdc>
mov    %eax,%r12d
xor    %r15d,%r15d
mov    %rsp,%r13
lea    0x8(%rsp),%r14
jmp    11e6 <pick_line+0x96>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x18(%rsp),%rax
mov    %r15,(%rax)
mov    0x20(%rsp),%rax
mov    %r12d,(%rax)
xor    %eax,%eax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%ebp
setl   %al
add    %eax,%ebp
mov    %r13,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1070 <getline@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     122c <pick_line+0xdc>
mov    %ebp,%edi
call   1040 <randomly@plt>
test   %rax,%rax
jne    11c0 <pick_line+0x70>
test   %r15,%r15
je     11ff <pick_line+0xaf>
mov    %r15,%rdi
call   1030 <free@plt>
mov    (%rsp),%rdi
mov    0x8(%rsp),%rsi
call   1050 <strndup@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11b0 <pick_line+0x60>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
call   1060 <err@plt>
jmp    11b0 <pick_line+0x60>
mov    0x10(%rsp),%rax
mov    %ebp,(%rax)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EX_OSERR = global i32 0
@.str = [20 x i8] c"could not copy line\00"
@UINT32_MAX = global i32 0
define void @pick_line(i32* %0, i32* %1, i8** %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8**
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i8** %2, i8*** %7
store i32* %3, i32** %8
store i8* null, i8** %9
store i8* null, i8** %10
%14 = load i32*, i32** %6
%15 = load i32, i32* %14
store i32 %15, i32* %13
br label %16
16: ; preds = %50, %4
%17 = load i32*, i32** %5
%18 = call i32 @getline(i8** %9, i64* %11, i32* %17)
store i32 %18, i32* %12
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %51
20: ; preds = %16
%21 = load i32, i32* %13
%22 = call i64 @randomly(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %43
24: ; preds = %20
%25 = load i8*, i8** %10
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %10
%29 = call i32 @free(i8* %28)
br label %30
30: ; preds = %27, %24
%31 = load i8*, i8** %9
%32 = load i64, i64* %11
%33 = call i8* @strndup(i8* %31, i64 %32)
store i8* %33, i8** %10
%34 = icmp ne i8* %33, null
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = load i32, i32* @EX_OSERR
%37 = call i32 @err(i32 %36, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %35, %30
%39 = load i8*, i8** %10
%40 = load i8**, i8*** %7
store i8* %39, i8** %40
%41 = load i32, i32* %12
%42 = load i32*, i32** %8
store i32 %41, i32* %42
br label %43
43: ; preds = %38, %20
%44 = load i32, i32* %13
%45 = load i32, i32* @UINT32_MAX
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* %13
%49 = add nsw i32 %48, 1
store i32 %49, i32* %13
br label %50
50: ; preds = %47, %43
br label %16
51: ; preds = %16
%52 = load i32, i32* %13
%53 = load i32*, i32** %6
store i32 %52, i32* %53
ret void
}

LLVM IR (-O2):
@EX_OSERR = global i32 0
@.str = [20 x i8] c"could not copy line\00"
@UINT32_MAX = global i32 0
define void @pick_line(i32* %0, i32* nocapture %1, i8** nocapture writeonly %2, i32* nocapture writeonly %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%8 = bitcast i64* %6 to i8*
%9 = load i32, i32* %1
%10 = call i32 @getline(i8** %5, i64* %6, i32* %0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %39, label %12
12: ; preds = %4, %31
%13 = phi i32 [ %37, %31 ], [ %10, %4 ]
%14 = phi i32 [ %36, %31 ], [ %9, %4 ]
%15 = phi i8* [ %32, %31 ], [ null, %4 ]
%16 = call i64 @randomly(i32 %14)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %12
%19 = icmp eq i8* %15, null
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = call i32 @free(i8* %15)
br label %22
22: ; preds = %20, %18
%23 = load i8*, i8** %5
%24 = load i64, i64* %6
%25 = call i8* @strndup(i8* %23, i64 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @EX_OSERR
%29 = call i32 @err(i32 %28, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %27, %22
store i8* %25, i8** %2
store i32 %13, i32* %3
br label %31
31: ; preds = %30, %12
%32 = phi i8* [ %25, %30 ], [ %15, %12 ]
%33 = load i32, i32* @UINT32_MAX
%34 = icmp slt i32 %14, %33
%35 = zext i1 %34 to i32
%36 = add nsw i32 %14, %35
%37 = call i32 @getline(i8** %5, i64* %6, i32* %0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %12
39: ; preds = %31, %4
%40 = phi i32 [ %9, %4 ], [ %36, %31 ]
store i32 %40, i32* %1
ret void
}